Controlling Your App’s Volume and Playback 控制应用程序的音量和播放
来源:互联网 发布:京东模式与淘宝模式 编辑:程序博客网 时间:2024/05/22 14:23
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 should perform their respective actions on the audio stream used by your app.
http://blog.csdn.net/sergeycao
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 theSTREAM_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 handysetVolumeControlStream()
method to direct volume key presses to the audio stream you specify.
Having identified the audio stream your application will be using, you should set it as the volume stream target. You should make this call early in your app’s lifecycle—because you only need to call it once during the activity lifecycle, you should typically call it within the onCreate()
method (of the Activity
orFragment
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 many connected or wireless headsets. Whenever a user presses one of these hardware keys, the system broadcasts an intent with theACTION_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.
<receiver android:name=".RemoteControlReceiver"> <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter></receiver>
The receiver implementation itself needs to extract which key was pressed to cause the broadcast. TheIntent
includes this under the EXTRA_KEY_EVENT
key, while theKeyEvent
class includes a list KEYCODE_MEDIA_*
static constants that represents each of the possible media buttons, such asKEYCODE_MEDIA_PLAY_PAUSE
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 should receive media button press events.
The following code can be used within your app to register and de-register your media button event receiver using theAudioManager
. When registered, your broadcast receiver is the exclusive receiver of all media button broadcasts.
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);...// Start listening for button pressesam.registerMediaButtonEventReceiver(RemoteControlReceiver);...// Stop listening for button pressesam.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
Typically, apps should unregister most of their receivers whenever they become inactive or invisible (such as during theonStop()
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.
- Controlling Your App’s Volume and Playback 控制应用程序的音量和播放
- 19Controlling Your App’s Volume and Playback
- Managing Audio Playback之Controlling Your App’s Volume and Playback
- Android音乐编程:控制应用程序的音量和播放
- Android音乐编程:控制应用程序的音量和播放
- Android---控制多媒体应用程序的音量和播放
- Android Developers:控制你的应用程序音量和播放
- Android---控制多媒体应用程序的音量和播放
- Android Developers:控制你的应用程序音量和播放
- 管理媒体播放(1)控制应用程序的音量和播放
- 管理音频播放[Lesson 1 - 控制app的音量]
- 音量控制和音频播放
- directshow实现音频采集设备的输入音量控制和媒体文件播放音量控制
- 【Android Training - Multimedia】管理音频播放[Lesson 1 - 控制app的音量]
- 【Android Training - Multimedia】管理音频播放[Lesson 1 - 控制app的音量]
- 【Android Training - Multimedia】管理音频播放[Lesson 1 - 控制app的音量]
- Android官方开发文档Training系列课程中文版:管理音频播放之控制APP的音量与播放
- GL音乐播放器--代码实现<四>--进度控制和可隐藏的音量控制
- Hibernate Projections(投影、统计、不重复结果)
- Delphi Web Service(2)
- 移动互联万蓝海时代的到来
- Redis学习手册(开篇)
- ado调用存储过程
- Controlling Your App’s Volume and Playback 控制应用程序的音量和播放
- int a[5]={}, &a+1与(int*)a+1的区别
- 文件,文件夹问题
- ubuntu构建嵌入式nfs服务
- HDU 2037 今年暑假不AC
- 关于typedef的使用
- Android教程(八)-- 单选按钮RadioButton控件的使用
- 实现复数类的运算符重载
- js提交表单