gpt4 book ai didi

android - 尝试使用 Jelly Beans 新的低级媒体 API 构建 "Hello, world!"媒体播放器 Activity

转载 作者:太空狗 更新时间:2023-10-29 15:15:08 26 4
gpt4 key购买 nike

我正在尝试测试新的低级媒体 API 功能、MediaExtractor 和 MediaCodec。我正在遵循本指南:

http://dpsm.wordpress.com/2012/07/28/android-mediacodec-decoded/

我把这个函数放在一起,其中 songsList.get(songIndex).get("songPath") 是另一个函数给出的 mp3 文件的路径。

    /**
* Attempts at API level 16 implementation
*
* MediaExtractor, MediaCodec and AudioTrack to play audiofiles
*
* */

public void JBPlay(int songIndex) {
String LOG_TAG="JB";

//AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.sample);
//AssetFileDescriptor sampleFD = getAssets().openFd(songsList.get(songIndex).get("songPath"));
// getResources().openRawResourceFD(songsList.get(songIndex).get("songPath"));

//Log.d("FD: ", sampleFD.toString());

MediaExtractor extractor;
MediaCodec codec;
ByteBuffer[] codecInputBuffers;
ByteBuffer[] codecOutputBuffers;

extractor = new MediaExtractor();
extractor.setDataSource(songsList.get(songIndex).get("songPath"));
//extractor.setDataSource(sampleFD.getFileDescriptor(),
// sampleFD.getStartOffset(), sampleFD.getLength());


Log.d(LOG_TAG, String.format("TRACKS #: %d", extractor.getTrackCount()));
MediaFormat format = extractor.getTrackFormat(0);
String mime = format.getString(MediaFormat.KEY_MIME);
Log.d(LOG_TAG, String.format("MIME TYPE: %s", mime));


codec = MediaCodec.createDecoderByType(mime);
codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */);
codec.start();
codecInputBuffers = codec.getInputBuffers();
codecOutputBuffers = codec.getOutputBuffers();

extractor.selectTrack(0); // <= You must select a track. You will read samples from the media from this track!


boolean sawInputEOS=false;
int inputBufIndex = codec.dequeueInputBuffer(1000);//1 second timeout ???
if (inputBufIndex >= 0) {
ByteBuffer dstBuf = codecInputBuffers[inputBufIndex];

int sampleSize = extractor.readSampleData(dstBuf, 0);
long presentationTimeUs = 0;
if (sampleSize < 0) {
sawInputEOS = true;
sampleSize = 0;
} else {
presentationTimeUs = extractor.getSampleTime();
}


codec.queueInputBuffer(inputBufIndex,
0, //offset
sampleSize,
presentationTimeUs,0);
//sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
if (!sawInputEOS) {
extractor.advance();
}
}

AudioTrack audioTrack = null;
BufferInfo info = null;
Boolean sawOutputEOS=false;
final int res = codec.dequeueOutputBuffer(info, 1000);
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) {
audioTrack.write(chunk, 0, chunk.length);
}
codec.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 = codec.getOutputBuffers();
} else if (res == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
final MediaFormat oformat = codec.getOutputFormat();
Log.d(LOG_TAG, "Output format has changed to " + oformat);
audioTrack.setPlaybackRate(oformat.getInteger(MediaFormat.KEY_SAMPLE_RATE));
}
// EDIT nov. 16: addition of play() causes crash!
//audioTrack.play()

}

它似乎没有抛出任何重大错误,但没有声音,并且在日志中有一条消息“找不到 libwvm.so”和“OMX_getExtensionIndex 失败”。在我尝试使用日志编写代码之前,也许有人有一些想法。我正在使用 Jelly Bean 4.1.2 版运行 AVD,但我无法从 Eclipse 进行调试,所以我只使用日志来捕获错误。

编辑十一月。 16: 我忘记了明显的 audioTrack.play() 函数但是这会导致崩溃,所以它再次被注释掉。

最佳答案

在我的代码中,我在构建 AudioTrack 之后得到了 audioTrack.play() 并且它可以工作。您确定您的设备上的音量已打开吗?起初我犯了那个错误,显然什么也听不到。您还可以使用 AudioManager 调高音量。

关于android - 尝试使用 Jelly Beans 新的低级媒体 API 构建 "Hello, world!"媒体播放器 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13387707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com