Android Advanced Audio App

Source: Internet
Author: User
Tags dtmf tones

When it comes to audio applications, the first thing to think about is the music player. Some players can play streaming media, and some can play local music files. With the evolution of the Android platform, more advanced audio APIs are needed. Fortunately, Google has added this API to support low-latency audio streaming and recording.


The Android Audio API offers some advanced features that developers can integrate into their own applications. With these APIs, it is now easier to implement VoIP applications, build custom streaming music clients, and achieve low-latency gaming sound. In addition, there are APIs for text-to-speech and speech recognition that allow users to interact directly with audio and applications without the need for a user interface or touch technology.


Low Latency Audio

Android has four APIs to play audio (counting five of MIDI words) and three APIs for recording. The following is a brief introduction to these APIs, as well as some advanced usage examples.


① audio playback API

Music playback uses MediaPlayer by default. This class is ideal for playing music or video, playing streaming resources, and playing local files. Each mediaplayer has an associated state machine that is required to track these states in the application. Developers can use the MediaPlayer class API to embed music or video playback functions in their own applications without additional processing or consideration of latency requirements.


The second option is the Soundpool class, which provides low-latency support for playing sound and other short audio, such as playing game sounds with Soundpool. However, it does not support audio streaming, so it is not suitable for applications that require real-time audio streaming, such as VoIP.


The third option is the Audiotrack class, which allows the audio stream to be buffered into the hardware, supporting low-latency playback, and even for streaming media scenarios. The Audiotrack class typically provides low enough latency to be used in VoIP or similar applications.


The following code shows how to use Audiotrack in a VoIP app:

Public classAudiotrackdemo {Private Final intmminbuffersize;Private FinalAudiotrackMaudiotrack; PublicAudiotrackdemo () { This.mminbuffersize= Audiotrack.getminbuffersize(16000, Audioformat.Channel_out_mono, Audioformat.Encoding_pcm_16bit); This.Maudiotrack=NewAudiotrack (Audiomanager.Stream_music, 16000, Audioformat.Channel_out_mono, Audioformat.Encoding_pcm_16bit, this.mminbuffersize*2, Audiotrack.Mode_stream);    }Public voidPlaypcmpacket(byte[] pcmdata) {if( This.Maudiotrack!=NULL&& This.Maudiotrack. getState () = = Audiotrack.state_initialized) {if( This.Maudiotrack. getplaybackrate ()! = Audiotrack.playstate_playing) { This.Maudiotrack. Play ();            } This.Maudiotrack. Write (Pcmdata, 0, Pcmdata.length);        }    }Public voidStopplayback() {if( This.Maudiotrack!=NULL) { This.Maudiotrack. Stop (); This.Maudiotrack. Release ();        }    }}

First, determine the minimum buffer size for the audio stream. To do this, you need to know the sampling rate, whether the data is mono or stereo, and if 8-bit or 16-bit PCM encoding is used. Audiotrack.getminbuffersize () is then called as a parameter with the sample rate and sample size, which returns the minimum buffer size of the Audiotrack instance as a byte.


Next, create the Audiotrack instance using the correct parameters as needed. The first parameter is the type of audio, and different applications use different values. For VoIP applications, Stream_voice_call is used, and stream_music is used for streaming music applications.


The specific options are:

Stream_alarm: The sound of the phone alarm
Stream_music: The Sound of Mobile music
The sound of STREAM_DTMF:DTMF tones
Stream_ring: The sound of the telephone ringing
Stream_notfication: System-prompted sound
Stream_system: Sound of the system
Stream_voice_call: Voice phone voice


Second, third, the fourth parameter will vary depending on the usage scenario. These parameters represent sample rate, stereo or mono, and sample size, respectively. In general, a VoIP application uses 16KHZ 16-bit mono, while a conventional music CD may use 44.1KHZ of 16-bit stereo. A 16-bit stereo high sample rate requires a larger buffer and more data transfer, but the sound quality is better. All Android devices support PCM to play 8 or 16-bit stereo at 8khz,16khz,44.1khz sample rate.


The buffer size parameter should be a multiple of the minimum buffer, depending on the specific requirements, sometimes factors such as network latency can also affect the buffer size.

You should avoid using an empty buffer at all times, because it can cause playback to fail.


The last parameter determines whether to send only one audio data (mode_static) or a continuous send data stream (Mode_stream). The first case involves sending the entire audio clip at once. For continuous audio streaming, you can send PCM data of any size block, which can be used to process streaming music or VoIP calls.


② Recording API

When it comes to recording audio, the first thing to consider is the API Mediarecorder. Similar to MediaPlayer, you need to track the internal state of the Mediarecorder class in your app code. Because Mediarecorder can only save recordings to a file, it is not suitable for recording streaming media.

If you need to record streaming media, you can use Audiorecord, which is very similar to the code you just showed.


The following example shows how to create an Audiorecord instance to record an audio sample of 16-bit mono 16KHZ:

Public classAudiorecorddemo {Private FinalAudiorecordMaudiorecord;Private Final intmminbuffersize;Private BooleanMdorecord=false; PublicAudiorecorddemo () { This.mminbuffersize= Audiotrack.getminbuffersize(16000, Audioformat.Channel_out_mono, Audioformat.Encoding_pcm_16bit); This.Maudiorecord=NewAudiorecord (Mediarecorder.audiosource.voice_communication, 16000, Audioformat.Channel_in_mono, Audioformat.Encoding_pcm_16bit, this.mminbuffersize*2);    }Public voidWriteaudiotostream(OutputStream Stream) { This.Mdorecord=true; This.Maudiorecord. Startrecording ();byte[] buffer=New byte[ This.mminbuffersize*2]; while( This.Mdorecord){intbytewritten= This.Maudiorecord. Read (buffer,0,Buffer.length);Try{Stream.Write (buffer,0,Bytewritten);            }Catch(IOException e) { This.Mdorecord=false;            }        } This.Maudiorecord. Stop (); This.Maudiorecord. Release ();    }Public voidstoprecording(){ This.Mdorecord=false;    }}

Because they are very similar to the audiotrack creation process, they can be easily combined when using VoIP or similar applications.


Believe that the people who have studied multimedia are not unfamiliar with the sampling rate and so on, if the lack of knowledge in this area, can be properly supplemented after looking at this code.

If you look at the serious person will find this article only introduced three playback API and two recording API, said four of the three? In fact, the last one is not one or two sentence can be said clearly, but also need to list a separate article to explain--OPENSL es please follow this blog will be explained.

Android Advanced Audio App

Related Article

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.