Android music programming: controls the app's volume and Playback

Source: Internet
Author: User

Recognize audio streams

The first step to creating an excellent audio experience is to understand the type of audio stream that your application will use.

The Android system maintains independent audio channels for playing music, alarms, notifications, incoming call ringtones, system sounds, and call calls.) volume, and DTMF tone keyboard dialing ).

In this way, the user can independently control the volume of each stream.

Most streams are limited to system events. Therefore, unless your application changes the alarm clock, STREAM_MUSIC audio streams are almost certainly used.

Use the hardware volume button to control the application volume

By default, press the volume key to control the volume of the audio stream that is currently focused. If your application is not currently playing music or is not running, the volume key will adjust the volume of the vibrator.

If you have a game or music app, when a user clicks the volume key, even if they are currently browsing the song or are not staying in the position of the current music game, they still want to control the volume of games or music.

You can modify the current audio stream volume by receiving a key message from the volume control key. However, this is not necessary. Android provides a convenient setVolumeControlStream () method to automatically adjust the volume based on the specified audio stream.

First, determine the audio stream that your application will use, and then set it as the current focus audio stream through the setVolumeControlStream method.

This method should be called early and only once in the lifecycle. Therefore, it should be called in OnCreate () of Activity or Fragment.

This will ensure that as long as your application is visible, the volume control function is what the user expects (that is, to control the volume of the current application ).

Sample Code:

 
 
  1. setVolumeControlStream(AudioManager.STREAM_MUSIC);  

From now on, pressing the volume key on the device will affect the audio stream of the sample "Music" you specified.

Use hardware playback control buttons to control audio playback of applications

Many mobile phones and wireless headsets have media playback control buttons, such as play, pause, stop, and skip. Every time you press these hardware keys, the System Broadcasts the ACTION_MEDIA_BUTTON action.

To respond to the Click Event of the media button, you need to register BroadcastReceiver to listen for broadcasts.

Sample Code:

 
 
  1. <receiver android:name=".RemoteControlReceiver"> 
  2.   <intent-filter> 
  3.      <action android:name="android.intent.action.MEDIA_BUTTON"   /> 
  4.   </intent-filter> 
  5. </receiver> 

The aggreger must know the specific information of which key is pressed. The Intent includes the EXTRA_KEY_EVENT key value, which can be used to obtain the value of the KeyEvent type, KEYCODE_MEDIA _ * Static constants in KeyEvent indicate all media button types, such as KEYCODE_MEDIA_PLAY_PAUSE and KEYCODE_MEDIA_NEXT.

Sample Code:

 
 
  1. Public class RemoteControlReceiver extends BroadcastReceiver {
  2. @ Override
  3. Public void onReceive (Context context, Intent intent ){
  4. If (Intent. ACTION_MEDIA_BUTTON.equals (intent. getAction ())){
  5. KeyEvent event = (KeyEvent) intent. getParcelableExtra (Intent. EXTRA_KEY_EVENT );
  6. If (KeyEvent. KEYCODE_MEDIA_PLAY = event. getKeyCode ()){
  7. // Handle key press.
  8. // Process the playback key message
  9. }
  10. }
  11. }
  12. }

Because multiple applications may receive and respond to media button messages, you must programmatically control when your application should accept media button events.

In your application, you can use AudioManager to register and cancel the event receiver of the registration media button. When registering, you can use your dedicated event receiver.

Sample Code:

AudioManager am = mContext. getSystemService (Context. AUDIO_SERVICE );... // Start listening for button presses to Start monitoring am. registerMediaButtonEventReceiver (RemoteControlReceiver );... // Stop listening for button presses cancel monitoring am. unregisterMediaButtonEventReceiver (RemoteControlReceiver );

In general, when an application loses focus or is not displayed on the screen or hidden in the background, it should cancel most of its listening devices, such as in the onStop () callback function ).

However, it is not that simple for a media playback application. In fact, the most important thing is that when your application is in an invisible state, at this time, it cannot be controlled by the UI on the screen, but the most typical response to the media playback control button is playing music in the background ).

Therefore, a better method is to register and deregister the event receiver of the media button when your application gets or loses the audio focus, not only depends on the interface status of the application.

For details, refer to the next course.

Reference Abstract:

Https://developer.android.com/training/managing-audio/volume-playback.html

Http://blog.zhourunsheng.com/2011/12/android-%E9%9F%B3%E4%B9%90%E7%BC%96%E7%A8%8B%E4%B8%93%E9%A2%98%E4%B9%8B%E6%8E%A7%E5%88%B6%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%9A%84%E9%9F%B3%E9%87%8F%E5%92%8C%E6%92%AD%E6%94%BE/

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.