Analysis of two methods of playing sound in Android Development

This paper describes two methods of playing sound in Android development. Share with you for your reference, as follows:

In Android, the addition of multimedia elements such as audio and video makes the user experience of the application better. It can be said that today's mobile phone has become not only a communication tool, but also a necessary product for entertainment and office.

Android provides a simple audio API. Mediaplayer is generally used to play audio, which is also the most common tool to play sound. There are a large number of examples of this tool on the Internet, so I will only make a brief introduction here.

There are three familiar methods for controlling playback behavior: start (), stop (), and pause ().

Through the static mediaplayer create (context, URI) method, you can obtain a newly created mediaplayer object.

During playback, there are several listeners that can monitor the playback process, such as:

Listening audio playback ends;

Listen for error events during playback;

Triggered when prepare() is called.

However, there are some problems when playing with mediaplayer. We know that mediaplayer will consume a lot of system resources during creation and destruction, and the creation and destruction time is relatively long. In addition, if we need to play many sounds at the same time, mediaplayer does not support it.

Therefore, we need a more lightweight sound playback tool.

Android provides another kind, called soundpool, which is suitable for playing sound effects that need to be played repeatedly but for a short time. It supports playing multiple sounds at the same time. These sounds will be loaded into the list at the beginning of the system. According to the ID of these sounds, we can call these sound effects.

Let's enter an example to see how soundpool works.

For example, now in a Gobang game, we need to play a sound when the pieces fall off the board. We can use soundpool because it takes a short time and needs to be played repeatedly, and we don't want the sound to take up too much resources.

Look at the code first:

The code is very simple. The first line declares a soundpool object, which usually appears as a member attribute of the class. The second line instantiates soundpool. The first parameter is the number of sounds that soundpool can support, which determines how large a buffer Android sets for it. The second parameter is the sound type, which is identified here as system sound. In addition, there is audiomanager.stream_ Ring and audiomanager.stream_ Music, etc. the system will mark different priorities and buffers according to different sounds. The final parameter is sound quality. The higher the quality, the better the sound effect, but it consumes more system resources.

In the third line, the system loads the sound for soundpool. The first parameter is the context parameter and the second parameter is the ID of the sound. Generally, we save the sound information in the raw folder of res, as shown in the following figure.

The third parameter is the priority of sound. When multiple sounds conflict and cannot be played at the same time, the system will give priority to the higher priority.

The fourth line is playing. The first parameter is ID, which is the order in which it is put into soundpool. For example, now collide.wav is the first, so its ID is 1. The second and third parameters are volume control of left and right channels. The fourth parameter is priority. Since there is only one sound, priority is not important here. The fifth parameter is whether to play circularly, 0 is not circularly, and - 1 is circularly. The last parameter is the playback ratio, from 0.5 to 2, generally 1, indicating normal playback.

More readers interested in Android related content can view the special topics of this site: summary of Android multimedia operation skills (audio, video, recording, etc.), introduction and advanced tutorial of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android database operation skills Summary of Android file operation skills, summary of Android resource operation skills and summary of Android control usage

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>