Controlling Your App's Volume and Playback controls the App's Volume and Playback

Source: Internet
Author: User
Tags dtmf tones

A good user experience is a predictable one. if your app plays media it's important that your users can control the volume of your app using the hardware or software volume controls of their device, bluetooth headset, or headphones.

Similarly, where appropriate and available, the play, stop, pause, skip, and previous media playback keys shocould perform their respective actions on the audio stream used by your app.

 


Identify Which Audio Stream to Use
The first step to creating a predictable audio experience is understanding which audio stream your app will use.

Android maintains a separate audio stream for playing music, alarms, notifications, the incoming call ringer, system sounds, in-call volume, and DTMF tones. this is done primarily to allow users to control the volume of each stream independently.

Most of these streams are restricted to system events, so unless your app is a replacement alarm clock, you'll almost certainly be playing your audio using the STREAM_MUSIC stream.

Use Hardware Volume Keys to Control Your App's Audio Volume
By default, pressing the volume controls modify the volume of the active audio stream. If your app isn' t currently playing anything, hitting the volume keys adjusts the ringer volume.


If you 've got a game or music app, then chances are good that when the user hits the volume keys they want to control the volume of the game or music, even if they're currently between songs or there's no music in the current game location.

You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. resist the urge. android provides the handy setVolumeControlStream () method to direct volume key presses to the audio stream you specify.


Having identified the audio stream your application will be using, you shoshould set it as the volume stream target. you shocould make this call early in your app's lifecycle-because you only need to call it once during the activity lifecycle, you shocould typically call it within the onCreate () method (of the Activity or Fragment that controls your media ). this ensures that whenever your app is visible, the volume controls function as the user expects.


SetVolumeControlStream (AudioManager. STREAM_MUSIC );
From this point onwards, pressing the volume keys on the device affect the audio stream you specify (in this case "music") whenever the target activity or fragment is visible.

Use Hardware Playback Control Keys to Control Your App's Audio Playback
Media playback buttons such as play, pause, stop, skip, and previous are available on some handsets and connected or wireless headsets. whenever a user presses one of these hardware keys, the system broadcasts an intent with the ACTION_MEDIA_BUTTON action.

To respond to media button clicks, you need to register a BroadcastReceiver in your manifest that listens for this action broadcast as shown below.

<Explorer android: name = ". remotecontrolpolicer">
<Intent-filter>
<Action android: name = "android. intent. action. MEDIA_BUTTON"/>
</Intent-filter>
</Cycler>
The specified er implementation itself needs to extract which key was pressed to cause the broadcast. the Intent events des this under the EXTRA_KEY_EVENT key, while the KeyEvent class includes des a list KEYCODE_MEDIA _ * static constants that represents each of the possible media buttons, such as between and KEYCODE_MEDIA_NEXT.

The following snippet shows how to extract the media button pressed and affects the media playback accordingly.

Public class RemoteControlReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
If (Intent. ACTION_MEDIA_BUTTON.equals (intent. getAction ())){
KeyEvent event = (KeyEvent) intent. getParcelableExtra (Intent. EXTRA_KEY_EVENT );
If (KeyEvent. KEYCODE_MEDIA_PLAY = event. getKeyCode ()){
// Handle key press.
}
}
}
}
Because multiple applications might want to listen for media button presses, you must also programmatically control when your app shoud receive media button press events.

The following code can be used within your app to register and de-register your media button event processing er using the AudioManager. When registered, your broadcast guest er is the exclusive playback er of all media button broadcasts.


AudioManager am = mContext. getSystemService (Context. AUDIO_SERVICE );
...

// Start listening for button presses
Am. registerMediaButtonEventReceiver (RemoteControlReceiver );
...

// Stop listening for button presses
Am. unregisterMediaButtonEventReceiver (remotecontrolpolicer );
Typically, apps shocould unregister most of their receivers whenever they become inactive or invisible (such as during the onStop () callback ). however, it's not that simple for media playback apps-in fact, responding to media playback buttons is most important when your application isn't visible and therefore can't be controlled by the on-screen UI.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.