A good user experience can be predicted. If your app needs to play multimedia, then your users can use your app to control the volume of their devices using hardware or software, and Bluetooth headsets, headband headsets, etc. are essential to them. Similarly, buttons that provide playback, stop, pause, skip, and playback functions appropriately in which location are determined by their respective processing of the audio stream in your app. Control the volume of the app and the playback of multimedia files mainly have the following three aspects:
Confirm which audio Stream you need to use
The first step in building a predictable audio experience is to understand which audio stream your app needs to use.
The Android system offers a different set of audio streams for playing music, alarms, notifications, call prompts, system sounds, call volume, and dual tone multi-frequency tones. This mechanism provides the possibility for the app's users to control the volume with a separate stream.
Most streams are limited to system events, so unless your app is positioned on an alternative system's alarm clock, you'll basically use the Stream_music stream to play your audio at design time.
Use the hardware button to control the audio volume of your app
Typically, you control the volume of the audio stream that is being used by pressing the volume control button. If your app doesn't play any multimedia files, the ringtone size is adjusted when you press the Volume Control button.
If you have a game or music app installed, when the user presses the volume button they usually want to control the volume of the game or the music, even the interval of the song or a game scene without background music.
At this point you may try to monitor the volume button's click event and then adjust the volume of the audio stream used by your app. We do not recommend this method. The Android system provides a convenientSetvolumecontrolstream () method to adjust the volume of the audio stream you specify directly when you press the volume button.
After you have determined which audio stream your app will use, you should set it to the target of the volume stream. You should call this method early in your app's life cycle, because you only need to call the Setvolumecontrolstream () method once in the life cycle of the activity, recommended in onCreate () (Control your multimedia playback activity or fragment use) callback method is called when used. This ensures that the volume will be controlled by the user's expectations, regardless of whether your app is visible or not.
1 setvolumecontrolstream (audiomanager.stream_music);
When the Setvolumecontrolstream () method is called, pressing the device's volume button affects the volume of the audio stream you specify ("Music" in the example above). Whether the target activity or fragment is visible.
Use the Playback control button on your hardware to control the playback of app audio
Multimedia buttons are available on some connected networks or wireless phones, such as play, pause, stop, next and previous. Whenever a user presses any of the above hardware, the system sends a broadcast that intent contains ACtion_media_button 's action.
In response to the Click event of the Multimedia button, you need to register a broadcastreceiver in your manifest to listen for the Action_media_button ACTION.
1 <receiverandroid:name= ". Remotecontrolreceiver ">2 <intent-filter> 3 <actionandroid:name= "Android.intent.action.MEDIA_BUTTON"/>4 </intent-filter>5 </receiver>
The above receiver indicates that it needs to get to which button was clicked which led to the above broadcast. The extra KEY in intent has the corresponding information in EXTra_key_event , and a set of static variables is defined in the KeyEvent class keycode_media_* , they represent each of the multimedia buttons that may be clicked, such as keycode_media_play_pause and keycode_media_next.
The following code snippet shows how to get the Click event of a multimedia button and influence the playback of the multimedia based on that event.
1 publicclassremotecontrolreceiverextendsbroadcastreceiver{2 @Override3 publicvoid onreceive (Context context,intent Intent) {4 if(Intent.ACTION_MEDIA_BUTTON.equals (Intent.getaction ())) {5Keyeventevent=(keyevent) Intent.getparcelableextra (intent.extra_key_event);6 if(Keyevent.keycode_media_play = =Event.getkeycode ()) {7 //Handle Key Press.8 }9 }Ten } One}
Because many applications may listen to the Click event of a multimedia button, you need to listen and control the Click event of the Multimedia button in a coded manner when your app needs to listen for the Click event of the Multimedia button.
The following code can be used in your app, using Audiomanager to register and logout your multimedia button click event Receiver. When receiver is registered, your registered broadcast receiver is the only receiver that receives the multimedia button broadcast.
1Audiomanager am =Mcontext.getsystemservice (context.audio_service);2 ...3 4 //Start Listening for button presses5 Am.registermediabuttoneventreceiver (remotecontrolreceiver);6 ...7 8 //Stop Listening for button presses9 Am.unregistermediabuttoneventreceiver (remotecontrolreceiver);Ten
Typically, Android applications need to unregister their receivers when they become inactive or invisible (for example, when the OnStop () callback method is invoked). However, it is often not so easy for multimedia-playing apps, and when your app is running in the background, it often needs to respond to the click events of the Multimedia play button.
Registering a multimedia button event listening receiver when your application gains audio focus, logging off the Multimedia button event when the audio focus is lost listening receiver is a better solution. It will be explained in more detail in the next chapter.
1-Controls the volume of the app and the playback of multimedia files (managing Audio Playback)