Android Audio Development--how to play a frame of audio

Source: Internet
Author: User

This article focuses on how to play a frame of audio data on the Android platform. Before reading this article, it is recommended to read "Android Audio Development (1): Fundamentals", because in the audio development process, often involves these basic knowledge, after mastering these important concepts, the development process of many parameters and processes will be easier to understand.

Android SDK provides 3 sets of audio playback APIs, namely: Mediaplayer,soundpool,audiotrack, the difference between them can see this article: "Intro to the three Android Audio APIs", In simple terms, MediaPlayer is more suitable for long-time playback of local music files or online streaming resources in the background; Soundpool is suitable for playing relatively short audio clips, such as game sounds, key sounds, ringtone clips, etc., it can play multiple audio simultaneously; And the Audiotrack is closer to the bottom, providing a very powerful control that supports low-latency playback, suitable for streaming media and VoIP voice telephony scenarios.

Audio development, more widely used is not limited to playing local files or audio clips, so this article focuses on how to audiotrack API to play audio data (note that the audio played using Audiotrack must be decoded PCM data).

1. Audiotrack Workflow

First, let's look at the Audiotrack workflow:

(1) Configure parameters to initialize the internal audio playback buffer

(2) Start playback

(3) Need a thread, constantly to the audiotrack buffer "write" audio data, note that this process must be timely, otherwise there will be "underrun" error, this error in audio development is more common, meaning that the application layer does not "feed" the audio data in a timely manner, Causes the internal audio playback buffer to be empty.

(4) Stop playback, release resources

2. Parameter configuration of Audiotrack

Above is the Audiotrack constructor prototype, mainly by the constructor to configure the relevant parameters, the following one by one explanation (again, it is recommended to read "Android Audio Development (1): Basic knowledge"):

(1) Streamtype

This parameter represents which audio management strategy is used by the current application, and when the system has multiple processes that need to play audio, this management policy determines the final presentation, and the optional value of the parameter is defined in the Audiomanager class as a constant, mainly including:

Stream_vocie_call: Telephone Voice

Stream_system: System Sound

Stream_ring: Ringtones

Stream_musci: The Sound of Music

Stream_alarm: Warning Sound

Stream_notification: Notification Sound

(2) Samplerateinhz

Sampling rate, from the audiotrack source of the "Audioparamcheck" function can be seen, this sampling rate range must be between 4000hz~192000hz.

(3) Channelconfig

The configuration of the number of channels, the optional value is defined in the form of constants in the Audioformat class, commonly used is Channel_in_mono (single channel), Channel_in_stereo (dual channel)

(4) Audioformat

This parameter is used to configure "data bit width", the optional value is also defined in the form of constants in the Audioformat class, commonly used is encoding_pcm_16bit (16bit), Encoding_pcm_8bit (8bit), note that The former is guaranteed to be compatible with all Android phones.

(5) Buffersizeinbytes

This is the most difficult to understand and the most important parameter, it is configured Audiotrack internal audio buffer size, the value of the buffer can not be less than one frame "audio frame" size, and the previous article introduced, the size of a frame audio frame is calculated as follows:

int size = Sample rate x bit width x sample time X number of channels

Sampling time generally take 2.5ms~120ms between, by the manufacturer or the specific application decision, we can actually infer that the shorter the sampling time of each frame, the resulting delay should be smaller, of course, fragmented data will be more.

In Android development, the Audiotrack class provides a function that helps you determine this buffersizeinbytes, with the following prototype:

int getminbuffersize (int samplerateinhz, int channelconfig, int audioformat);

Different manufacturers of the underlying implementation is not the same, but nothing is based on the above formula to get a frame size, the size of the audio buffer must be a frame size of 2~n times, interested friends can continue to delve into the source code exploration.

In practical development, it is strongly recommended that the function calculate the buffersizeinbytes that need to be passed in, instead of manually calculating it yourself.

(6) mode

Audiotrack provides two modes of play, one is static, one is streaming, the former needs to write all the data to the playing buffer at once, simple and efficient, usually used for playing ringtones, system reminders of audio clips; The latter is the uninterrupted writing of audio data at a certain time interval, which in theory can be used for any audio playback scenario.

The optional values are defined as constants in the Audiotrack class, one is Mode_static, the other is Mode_stream, and the corresponding values are passed in according to the specific application.

4. Sample Code

I have simply encapsulated the interface of the Audiotrack class and provided a Audioplayer class that can be downloaded to my github: https://github.com/Jhuster/Android/blob/master/Audio/ Audioplayer.java

Here's a copy:

1 ImportAndroid.util.Log;2 ImportAndroid.media.AudioFormat;3 ImportAndroid.media.AudioManager;4 ImportAndroid.media.AudioTrack;5  6  Public classAudioplayer {7      8     Private Static FinalString TAG = "Audioplayer";9  Ten     Private Static Final intDefault_stream_type =Audiomanager.stream_music; One     Private Static Final intDefault_sample_rate = 44100; A     Private Static Final intDefault_channel_config =Audioformat.channel_in_stereo; -     Private Static Final intDefault_audio_format =Audioformat.encoding_pcm_16bit; -     Private Static Final intDefault_play_mode =Audiotrack.mode_stream; the               -     Private Booleanmisplaystarted =false; -     Private intMminbuffersize = 0; -     PrivateAudiotrack Maudiotrack;  +       -      Public BooleanStartplayer () { +         returnStartplayer (Default_stream_type,default_sample_rate,default_channel_config,default_audio_format); A     } at       -      Public BooleanStartplayer (intStreamtype,intSamplerateinhz,intChannelconfig,intAudioformat) { -           -         if(misplaystarted) { -LOG.E (TAG, "Player already started!")); -             return false; in         } -           toMminbuffersize =audiotrack.getminbuffersize (Samplerateinhz,channelconfig,audioformat); +         if(Mminbuffersize = =audiotrack.error_bad_value) { -LOG.E (TAG, "Invalid parameter!")); the             return false; *         } $LOG.D (TAG, "getminbuffersize =" +mminbuffersize+ "bytes!"));Panax Notoginseng           -Maudiotrack =NewAudiotrack (streamtype,samplerateinhz,channelconfig,audioformat,mminbuffersize,default_play_mode); the         if(maudiotrack.getstate () = =audiotrack.state_uninitialized) { +LOG.E (TAG, "Audiotrack Initialize fail!")); A             return false; the         }             +           -misplaystarted =true; $           $LOG.D (TAG, "Start Audio player success!")); -           -         return true; the     } -      Wuyi      Public intgetminbuffersize () { the         returnmminbuffersize; -     } Wu       -      Public voidStopplayer () { About           $         if(!misplaystarted) { -             return; -         } -           A         if(maudiotrack.getplaystate () = =audiotrack.playstate_playing) { + Maudiotrack.stop ();  the         } -           $ maudiotrack.release (); themisplaystarted =false; the              theLOG.D (TAG, "Stop Audio player success!")); the     } -       in      Public BooleanPlaybyte[] Audiodata,intOffsetinbytes,intsizeinbytes) { the           the         if(!misplaystarted) { AboutLOG.E (TAG, "Player not started!")); the             return false; the         } the           +         if(sizeInBytes <mminbuffersize) { -LOG.E (TAG, "audio data is not enough!")); the             return false;Bayi         } the           the         if(Maudiotrack.write (audiodata,offsetinbytes,sizeinbytes)! =sizeinbytes) {                 -LOG.E (TAG, "Could not write all of the samples to the audio device!")); -         }                                    the                                                      the Maudiotrack.play (); the           theLOG.D (TAG, "OK, played" +sizeinbytes+ "bytes!"); -           the         return true; the     } the}

Original: http://ticktick.blog.51cto.com/823160/1750593

Android Audio Development--how to play a frame of audio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.