Playback PCM data on Android

Source: Internet
Author: User

Android MediaPlayer contains audio and video playback capabilities, and on Android interface, music and video two applications are called MediaPlayer implementations. MediaPlayer is based on the Opencore (PacketVideo) library, and in order to build a MediaPlayer program, the upper layer also contains inter-process communication, which is based on the binder mechanism in the Android base library. However, this class can only operate on full audio files, not directly on pure PCM audio data. What if we decode the PCM data sources and how do we play them? Yes, it is the class with Audiotrack (MediaPlayer also calls the class for a real Audio streaming operation) below this demo demonstrates how to use audiotrack not verbose, The following class is the first to make a simple package for audiotrack:
public class Myaudiotrack {int mfrequency;                                        sampling rate int Mchannel;                                        channel int msampbit;                                        Sampling accuracy Audiotrack maudiotrack;                public myaudiotrack (int frequency, int channel, int sampbit) {mfrequency = frequency;                Mchannel = Channel;        Msampbit = Sampbit;                } public void Init () {if (maudiotrack! = null) {release ();                                                                                                         }//gets the minimum buffer size of the build object int minbufsize = Audiotrack.getminbuffersize (mfrequency,                                                                                                        Mchannel,                                            Msampbit);    Stream_alarm: Warning//STREAM_MUSCI: Musical sound, such as music, etc.//                Stream_ring: Ringtones//Stream_system: System sound//Stream_vocie_call: Telephone Sound                                 Maudiotrack = new Audiotrack (Audiomanager.stream_music, Mfrequency,                                Mchannel, Msampbit, Minbufsize, Audiotrack.mode_stream);//Audiotrack There are two categories of mode_static and Mode_stream. Stream means that the user writes the data once in the application through write to Audiotrack. This is the same as when we send data in the socket, the application layer obtains data from somewhere, for example by encoding and decoding the PCM data, and then write to Audiotrack. The disadvantage of this way is always in the Java layer and native layer interaction, the efficiency loss is large.                                Static means that when you start creating, you put the audio data in a fixed buffer and then pass it directly to audiotrack,//. The follow-up will not have to write again. AudiotrackThe data in this buffer will be played by itself.                                                                                This method is suitable for low memory usage such as ringtones, and for sounds with higher latency requirements.                Maudiotrack.play ();                                                                       }public void Release () {if (maudiotrack! = null) {maudiotrack.stop ();                Maudiotrack.release ();  }} public void Playaudiotrack (byte []data, int offset, int length) {if (data = = NULL ||                Data.length = = 0) {return;                } try {maudiotrack.write (data, offset, length); } catch (Exception e) {//Todo:handle Exception log.i ("Myaudiotrack", "Cat                Ch exception ... "); }} public int getprimeplaysize () {int minbufsize = audiotrack.getminbuffersize (mFrEquency, Mchannel, msampbit);        return minbufsize * 2; }}

Maudiotrack.write (data, offset, length), this function is the key to write data to the hardware playback!

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.