Android easyplayer has functions such as automatic stop and recovery of sound, one key mute, etc

Android easyplayer has functions such as automatic stop and recovery of sound, one key mute, etc

When we develop the player, we may need to mute or reduce the volume. For example, when a music player is playing in the background, if there is another system notification sound, the player may reduce the volume and turn it up after the system sound is finished; If there is an incoming call, the player may pause the music until the call is over. Also, for example, when we play a video on a certain occasion, unexpectedly, it is very loud and will attract a lot of attention (very embarrassing). At this time, we may need the function of one key mute. How should we implement these functions?

The class of Android playing sound is audiotrack. The player will first demux the audio stream and then decode it. Then, the audio PCM data will be written to the audio device through audiotrack class, so as to make sound through microphone or speaker.

In order to realize sound control conveniently, we need to operate from the top layer of the application (because the bottom layer may have been abstracted into a library), that is, we need to start with audiotrack. Let's take a look at some of audiotrack's APIs.

With these APIs, we can meet our needs. It's very simple to implement.

First, let's do the one button mute function. We can make a switch button. The initial state of this button is to display the current playing state: playing audio or not playing audio. The playback state can be obtained by calling getplaystate(); Then press the button, and then play or pause according to the playback status.

The code is as follows:

Note that the flush interface is called after pause and before play. This ensures that the "old data" not played during pause will not be played when switching from pause to play.

Next, when audio resources are occupied by other processes (lose focus), we can automatically reduce the sound or stop the sound; The playback function is resumed when the audio resource is released (regained focus).

We need to judge the current audio resource status through AudioManager and get a callback when the audio focus changes. Its key API interfaces include:

In combination with the above API description, refer to the following codes and explanations:

So far, we have realized the functions of automatic stop and recovery of easyplayer sound and one key mute. It looks troublesome, doesn't it? In fact, it's easy to make an app, but it's not easy to take into account all kinds of situations if you want to do well. We should pay more attention to the implementation of system app or some official demo of Google. They often seem to have simple functions, which will make us feel that "if I do it, a few lines of code can be done." however, they have a large amount of code, because they take into account all kinds of details. Often, the vast majority of apps we develop can only be regarded as semi-finished products, and there is room for continuous optimization.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>