`

[Android Training视频系列] 8.1 Controlling Your App’s Volume and Playback

阅读更多
主要内容:
1 鉴别使用的是哪个音频流
2 使用物理音量键控制应用程序的音量
3 使用物理播放控制键来控制应用程序的音频播放
视频讲解:http://www.eyeandroid.com/thread-15877-1-1.html
控制应用程序的音量和音频的播放
       一个好的用户体验是可预期可控的。如果应用程序是在播放音频,那么显然我们需要做到能够通过硬件按钮、软件按钮、蓝牙耳麦等来控制音量。
       同样的,我们需要能够监听Media Button发出的播放、停止、暂停、跳过、上一首等动作,并且在应用程序所使用的Audio Stream上进行对应的响应。
       鉴别使用的是哪个音频流
       首先需要知道的是我们的应用程序会使用到哪些音频流。
       Android为播放音乐、闹铃、通知铃、来电声音、系统声音,打电话声音与DTMF频道分别维护了一个隔离的音频流。这是我们能够控制不同音频的前提。
       这些音频流中大多数都是被系统限制的,不能胡乱使用。除了你的应用程序是需要做替换闹铃操作,几乎其他的播放音频操作都是使用"STREAM_MUSIC"音频流。
       使用硬件音量键来控制应用程序的音量
       默认情况下,按下音量控制键会调节当前被激活的音频流,如果此时你的应用程序没有任何声音在播放,则会调节铃声的音量。
       如果你的应用程序是一个游戏或者音乐程序,需要在不管是否目前正在播放歌曲或者游戏是否发出声音,按硬件的音量键都会使其音量得到调节。
       我们需要监听音量键是否被按下,Android提供了setVolumeControlStream()的方法来直接控制指定的音频流的音量。
       在鉴别出应用程序会使用哪个音频流之后,需要在Activity或者Fragment的onCreate中就调用setVolumeControlStream,这样能确保不管应用程序是否可见,音频控制功能都以用户的预期工作。
setVolumeControlStream(AudioManager.STREAM_MUSIC);
       使用硬件的播放控制按键来控制应用程序音频播放
       媒体播放按钮,例如播放, 暂停, 停止, 跳过, 上一首等功能同样可以在一些线控,耳麦或者其他无线控制设备上实现。无论用户按下上面任何设备上的控制按钮,系统都会广播一个带有ACTION_MEDIA_BUTTON的Intent。
       为了响应那些操作,需要像下面一样注册一个BroadcastReceiver在Manifest文件中。    
<receiver android:name=".RemoteControlReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>
       Receiver需要判断这个广播是来自哪个按钮的操作,Intent在EXTRA_KEY_EVENT中包含了KEY信息,同样KeyEvent类包含了一系列KEYCODE_MEDIA_*的静态变量来表示不同的媒体按钮,例如KEYCODE_MEDIA_PLAY_PAUSE 和 KEYCODE_MEDIA_NEXT.
       下面的代码演示如何获取按下的媒体按键以及如何对应地响应:     
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.
            }
        }
    }
}
       因为可能有多个程序都同样监听了这些控制按钮,那么必须在代码中特意控制当前哪个Receiver会进行响应。
       下面的例子显示了如何使用AudioManager来注册监听与取消监听,通过这种方式,当Receiver被注册上时,它将是唯一响应Broadcast的Receiver。
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
...
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
       通常,当应用程序变得不活跃或者不可见时(像是在onStop())回调期间),应用程序应该取消注册大多数的Receiver。但是在媒体播放的时候并没有那么简单,因为我们需要在后台播放歌曲的时候同样能够进行响应。一个比较好的注册与取消监听的方法是当程序获取与失去音频焦点的时候进行操作,这个内容会在后面的课程中详细讲解。
视频讲解:http://www.eyeandroid.com/thread-15877-1-1.html
1
0
分享到:
评论

相关推荐

    Windows 8.1 Apps with XAML and C# Unleashed

    It is in full color, the content is laid out in an easy to read style, the author's writing style makes it easy to read, and the content is all valuable. There is no fluff like you find in a lot of ...

    SAP ERP Financial Accounting and Controlling-Configuration and Use Management

    In this introduction, I explain the meaning of the acronym SAP, the origin of SAP, and the concept of SAP implementation. We will go further to look at Enterprise Resource Plan

    Mastering Android Game Development with Unity

    Finally you will polish your game with stats, sounds, and Social Networking, followed by testing the game on Android devices and then publishing it on Google Play, Amazon, and OUYA Stores. ...

    Professional Android 4 Application Development 源代码

    Chapter 1: Hello, Android A Little Background What Android Isn't Android: An Open Platform for Mobile Development Native Android Applications Android SDK Features Introducing the Open Handset Alliance...

    Arduino.Android.Blueprints

    The first few projects of this book will focus on displaying tweets within your app by connecting to Twitter, controlling basic functions of the Arduino board, and connecting an Arduino-powered ...

    professional.Android.Application.Development.2009.pdf

    Chapter 1: Hello, Android 1 A Little Background 2 The Not So Distant Past 2 The Future 3 What It Isn’t 3 An Open Platform for Mobile Development 4 Native Android Applications 4 Android SDK ...

    Android代码-AirUnlock

    Using android phone to establish a connection with your Mac via Bluetooth low-energy (BLE), controlling Mac lock state (Lock or Unlock). Features Store the password in Mac system keychain. Send the ...

    7011-financial accounting and controlling

    【7011财务会计与控制】:这个主题主要涵盖了企业内部的财务管理和生产计划方面的内容,特别是关于Master Production Scheduling (MPS)和Material Requirements Planning (MRP)的运用。 MPS,即主生产排程,是企业...

    Arduino and LEGO Projects

    Whether you want to impress your friends, annoy the cat, or just kick back and bask in the awesomeness of your creations, Arduino and LEGO Projects shows you just what you need and how to put it all ...

    Robot.Programming.A.Guide.to.Controlling.Autonomous.Robots

    Robot Programming: A Guide to Controlling Autonomous Robots takes the reader on an adventure through the eyes of Midamba, a lad who has been stranded on a desert island and must find a way to program ...

    Windows Runtime via C#

    deploy, and secure app packages Understand how apps are activated and the process model controlling their execution Study the rich features available when working with files and folders Explore how ...

    developing android application with adobe air.part1

    Packaging Your Application As an APK File and Installing It on the Device Testing and Debugging Mobile Utility Applications Installing AIR on an Android Device via a Server Other Tools Conclusion ...

    Developer Android API Components(官网API Components离线PDF)

    - **控制应用对设备的可用性(Controlling Your App's Availability to Devices)**:开发者可以通过在AndroidManifest.xml中设置minSdkVersion和maxSdkVersion属性来控制应用在不同版本Android设备上的可用性。...

    developing android application with adobe air.part4.rar

    Packaging Your Application As an APK File and Installing It on the Device Testing and Debugging Mobile Utility Applications Installing AIR on an Android Device via a Server Other Tools Conclusion ...

    developing android application with adobe air.part2.rar

    Packaging Your Application As an APK File and Installing It on the Device Testing and Debugging Mobile Utility Applications Installing AIR on an Android Device via a Server Other Tools Conclusion ...

    developing android application with adobe air.part3.rar

    Packaging Your Application As an APK File and Installing It on the Device Testing and Debugging Mobile Utility Applications Installing AIR on an Android Device via a Server Other Tools Conclusion ...

    Controlling Playback-crx插件

    "Controlling Playback-crx插件"是一款专为解决在线媒体播放控制问题而设计的浏览器扩展。这款插件主要面向英语用户,它允许用户在浏览含有视频的HTML页面时,仍能方便地对页面中的音乐或电影进行播放控制,即使用户...

Global site tag (gtag.js) - Google Analytics