編輯:關於Android編程
1MedieCodec支持4.1以上系統使用,是谷歌SDK的一個類。
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];// 20m MediaCodec 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 block e.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 once buf.clear(); // ** MUST DO!!! OTHERWISE THE NEXT TIME YOU GET THIS // SAME BUFFER BAD THINGS WILL HAPPEN if (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, // offset sampleSize, presentationTimeUs, sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0); if (!sawInputEOS) { // Log.i(LOG_TAG, "extractor.advance()"); mediaExtractor.advance(); } } } }
這幾個月一直在忙項目上的事情,所以已經有一段時間不寫博客,抽時間整理下最近的收藏夾,感覺還是有一些新的知識點可以分享的。先從最近的說起,近期項目上需要支持視頻播放功能,雖
Android調試工具 ADBADB Android Debug Bridge。ADB工具位於Android SDK安裝目錄的platform-tools子目錄。主要功能
之前有很多朋友都問過我,在Android系統中怎樣才能實現靜默安裝呢?所謂的靜默安裝,就是不用彈出系統的安裝界面,在不影響用戶任何操作的情況下不知不覺地將程序裝好。雖說這
VR即Virtual Reality虛擬現實。虛擬現實技術是一種可以創建和體驗虛擬世界的計算機仿真系統它利用計算機生成一種模擬環境是一種多源信息融合的交互式的三維動態視景