Android-controls the volume and playback of multimedia applications

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/managing-audio/volume-playback.html

Good user experience is predictable. If your application needs to play multimedia, it is vital that you can use the hardware or software of your device to control the volume, such as Bluetooth headsets or microphones.

Similarly, when appropriate, your application should provide media playback operations such as playing, stopping, suspending, skipping, and forward on the media stream.

Identify the audio stream to be used

To create a predictable audio experience, you must first understand the volume stream that your application uses.

Android divides audio streams into playing music, warning sound, notification sound, telephone ringtone, system sound, call sound, and DTMF tone. This mainly aims to allow users to independently control the volume of each stream.

Most of these streams are limited by system events. Therefore, unless your application replaces the alarm clock, you can use stream_music stream to play your audio.

Use the hardware volume key to control the application's audio volume

By default, pressing the volume control key changes the volume of the audio stream being played. If your application does not currently play any audio, click the volume key to adjust the volume of the ringtone.

If you have a game program or music player, even if there is no music between two songs or the current game location, it is also a good time to use the volume control keys of game programs or music players to control the volume.

You may want to listen to the volume button and use this method to modify the volume of your audio stream. Given this idea, Android provides a convenient setvolumecontrolstream () method to direct the volume key to the audio stream you specified.

After you have determined the audio stream your application will use, you should set it as the target of the audio stream. You should call this method early in the application life cycle-because you only need to call this method once in the activity life cycle, usually you should () method (activity or fragment that controls media playback. This ensures that the volume control function is as expected no matter when your application is visible.

Setvolumecontrolstream (audiomanager. stream_music );

Starting from this moment, pressing the volume key on the volume device will affect the audio stream you specified, regardless of whether the target activity or fragment is visible.

The hardware playback control key is used to control the audio playback of the application.

On Some handheld devices and many wired or wireless headsets, there are some media play buttons, such as playing, pausing, stopping, skipping, or moving forward. No matter when you press one of these hardware buttons, the system will send an action_media_button operation intent broadcast.

To respond to the Click Event of the media button, you need to register a broadcastreceiver in your configuration file to listen to the broadcast of this operation type. The registration method is as follows:

<Receiverandroid: Name = ". remotecontrolpolicer">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. media_button"/>
</Intent-filter>
</Cycler>

The receiver extracts the broadcast from the key it needs. The intent object contains the broadcast information under the extra_key_event key, which contains the keyevent class, including the keycode_media _ * Static Constant list, which represents each possible media button, for example, keycode_media_play_pause and keycode_media_next.

The following code snippet shows how to extract the pressed media button, thus affecting media playback:

Publicclassremotecontrolreceiverextendsbroadcastreceiver {
@ Override
Public void onreceive (context, 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 complex applications may want to listen to media button push events, you must control the media button push events by programming when your applications receive them.

The following audiomanager code may be used by your application to register and reregister media button events. After registration, your broadcast receiver will become a proprietary receiver for 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, applications should unregister the receiver when they are idle or invisible (for example, during the onstop () callback. But it is actually not that simple for the media playback application, because it is the most important to respond to the media playback button when the application is invisible, so it cannot be controlled by the UI on the screen.

A better way to register and unregister a media button event receiver is when your application gets or loses the audio focus. This will be detailed in the next lesson.

Related Article

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.