Android MedieCodec硬解碼mp3,wma

來源:互聯網
上載者:User

標籤:

1MedieCodec支援4.1以上系統使用,是GoogleSDK的一個類。

2能夠對mp3,wma格式音頻檔案解碼

3解碼前,需要MediaExactor提取檔案資訊,包括檔案類型,採樣率,


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];// 20mMediaCodec mMediaCodec;MediaExtractor mediaExtractor;MediaFormat mMediaFormat;final int TIMEOUT_US = 1000;BufferInfo info;boolean sawOutputEOS = false;boolean sawInputEOS = false;ByteBuffer[] codecInputBuffers;ByteBuffer[] codecOutputBuffers;/** * 解碼音頻檔案,返回最後解碼的資料 *  * @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 decodeData;}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();}}}}


Android MedieCodec硬解碼mp3,wma

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.