Android中的背景音频与MediaSessionCompat

               

Background Audio in Android With MediaSessionCompat

One of the most popular uses for mobile devices is playing back audio through music streaming services, downloaded podcasts, or any other number of audio sources. While this is a fairly common feature, it's hard to implement, with lots of different pieces that need to be built correctly in order to give your user the full Android experience. 移动设备最流行播放音频,下载播客或任何其他音频源是通过音乐流服务。虽然这是一个相当常见的功能,它很难实现,有很多不同的部分需要正确构建,以给您的用户完整的Android体验。

In this tutorial you will learn about MediaSessionCompat from the Android support library, and how it can be used to create a proper background audio service for your users.在本教程中,您将从Android支持库中了解MediaSessionCompat,以及如何使用它为用户创建合适的背景音频服务。

The first thing you will need to do is include the Android support library into your project. This can be done by adding the following line into your module's build.gradle file under the dependencies node.您需要做的第一件事是将Android支持库包含到您的项目中。这可以通过在模块的build.gradle文件中的dependencies节点下添加以下行来完成。

After you have synced your project, create a new Java class. For this example I will call the classBackgroundAudioService. This class will need to extend MediaBrowserServiceCompat. We will also implement the following interfaces: MediaPlayer.OnCompletionListener and AudioManager.OnAudioFocusChangeListener.同步您的项目后,创建一个新的Java类。对于这个例子,我将调用classBackgroundAudioService。这个类将需要扩展MediaBrowserServiceCompat。我们还将实现以下接口:MediaPlayer.OnCompletionListener和AudioManager.OnAudioFocusChangeListener。

Now that your MediaBrowserServiceCompat implementation is created, let's take a moment to updateAndroidManifest.xml before returning to this class. At the top of the class, you will need to request theWAKE_LOCK permission.现在您的MediaBrowserServiceCompat实现已创建,让我们花一点时间来updateAndroidManifest.xml,然后返回到此类。在类的顶部,您需要请求WAKE_LOCK权限。

Next, within the application node, declare your new service with the following intent-filter items. These will allow your service to intercept control buttons, headphone events and media browsing for devices, such as Android Auto (although we won't do anything with Android Auto for this tutorial, some basic support for it is still required by MediaBrowserServiceCompat).接下来,在应用程序节点中,使用以下意向过滤器项目声明您的新服务。这些将允许您的服务拦截控制按钮,耳机事件和媒体浏览设备,如Android Auto(虽然我们不会做任何事情Android Auto本教程,MediaBrowserServiceCompat仍然需要一些基本的支持)。

Finally, you will need to declare the use of the MediaButtonReceiver from the Android support library. This will allow you to intercept media control button interactions and headphone events on devices running KitKat and earlier.最后,您需要从Android支持库声明使用MediaButtonReceiver。这将允许您在运行KitKat4.0及更早版本的设备上拦截媒体控制按钮交互和耳机事件。

Now that your AndroidManifest.xml file is finished, you can close it. We're also going to create another class named MediaStyleHelper, which was written by Ian Lake, Developer Advocate at Google, to clean up the creation of media style notifications.现在您的AndroidManifest.xml文件已完成,您可以关闭它。我们还将创建另一个名为MediaStyleHelper的类,由Google的开发者支持者Ian Lake撰写,用于清除媒体样式通知的创建。

猜你喜欢

转载自blog.csdn.net/hfuuhgcc/article/details/87874184