Android MedieCodec hard decoding mp3 and wma

Source: Internet
Author: User

Android MedieCodec hard decoding mp3 and wma

1 mediecodec supports more than 4.1 of systems and is a class of Google SDK.

2. decoding mp3 and wma audio files

3. Before decoding, The MediaExactor needs to extract the file information, including the file type, sampling rate,

 

 

Package com. dawin. mediacodec; import java. io. IOException; import java. nio. byteBuffer; import android. media. mediaCodec; import android. media. mediaExtractor; import android. media. mediaFormat; import android. media. mediaCodec. bufferInfo; public class MediaCodecTest {byte [] decodeData = new byte [1024*1024*20]; // 20 mMediaCodec; MediaExtractor mediaExtractor; MediaFormat mMediaFormat; final int TIMEO UT_US = 1000; BufferInfo info; boolean sawOutputEOS = false; boolean sawInputEOS = false; ByteBuffer [] codecInputBuffers; ByteBuffer [] codecOutputBuffers;/*** decodes audio files, return the final decoded data ** @ param url * @ return */public byte [] decode (String url) {url = ""; try {mediaExtractor. setDataSource (url);} catch (IOException e) {} mMediaFormat = mediaExtractor. getTrackFormat (0); String mime = mMediaFormat. getString (MediaFormat. KEY_MIME); try {mMediaCodec = MediaCodec. createDecoderByType (mime);} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} mMediaCodec. configure (mMediaFormat, null, null, 0); mMediaCodec. start (); codecInputBuffers = mMediaCodec. getInputBuffers (); codecOutputBuffers = mMediaCodec. getOutputBuffers (); info = new BufferInfo (); mediaExtractor. selectTrack (0); input (); output (); return deco DeData;} private void output () {final int res = mMediaCodec. dequeueOutputBuffer (info, TIMEOUT_US); if (res> = 0) {int outputBufIndex = res; ByteBuffer buf = codecOutputBuffers [outputBufIndex]; final byte [] chunk = new byte [info. size]; buf. get (chunk); // Read the buffer all at oncebuf. clear (); // ** must do !!! Otherwise the next time you get this // same buffer bad things will HAPPENif (chunk. length> 0) {System. arraycopy (chunk, 0, decodeData, 0, chunk. length); // mAudioTrack. write (chunk, 0, chunk. length);} mMediaCodec. releaseOutputBuffer (outputBufIndex, false/* render */); if (info. flags & MediaCodec. BUFFER_FLAG_END_OF_STREAM )! = 0) {sawOutputEOS = true;} else if (res = MediaCodec. INFO_OUTPUT_BUFFERS_CHANGED) {codecOutputBuffers = mMediaCodec. getOutputBuffers ();} else if (res = MediaCodec. INFO_OUTPUT_FORMAT_CHANGED) {final MediaFormat oformat = mMediaCodec. getOutputFormat (); // Log. d (LOG_TAG, "Output format has changed to" + oformat); // mAudioTrack. setPlaybackRate (oformat. getInteger (MediaFormat. KEY_SAMPLE_RATE) ;}} private Void input () {int inputBufIndex = mMediaCodec. dequeueInputBuffer (TIMEOUT_US); if (inputBufIndex> = 0) {ByteBuffer dstBuf = codecInputBuffers [inputBufIndex]; int sampleSize = mediaExtractor. readSampleData (dstBuf, 0); // Log. I (LOG_TAG, "sampleSize:" + sampleSize); long presentationTimeUs = 0; if (sampleSize <0 ){//. log. I (LOG_TAG, "Saw input end of stream! "); SawInputEOS = true; sampleSize = 0;} else {presentationTimeUs = mediaExtractor. getSampleTime (); // Log. I (LOG_TAG, "presentationTimeUs" + presentationTimeUs);} mMediaCodec. queueInputBuffer (inputBufIndex, 0, // offsetsampleSize, presentationTimeUs, sawInputEOS? MediaCodec. BUFFER_FLAG_END_OF_STREAM: 0); if (! SawInputEOS) {// Log. I (LOG_TAG, "extractor. advance ()"); mediaExtractor. advance ();}}}}


 

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.