我正在使用mediacodec和mediaextractor播放编码视频。现在我正在尝试为玩家实施暂停和恢复。 这是我运行播放器线程的方法代码
public void run() {
int frameCount = 0;
if (decoder == null) {
Log.e("DecodeActivity", "Can't find video info!");
return;
}
while (!mPlayer.isInterrupted()) {
if (!isEOS) {
try {
inIndex = decoder.dequeueInputBuffer(10000);
} catch (IllegalStateException e) {
// Ignore the exception and break.
break;
}
if (inIndex >= 0) {
ByteBuffer buffer = inputBuffers[inIndex];
int sampleSize = 0;
if(frameCount != 0) {
try {
System.out.println("buffer len = "+ buffer.capacity() + buffer);
sampleSize = extractor.readSampleData(buffer, 0);
System.out.println("read a sample......................");
} catch (RuntimeException re) {
re.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
// Ignore the exception.
}
System.out.println("ENC LENGTH"+sampleSize);
}
++frameCount;
if (sampleSize < 0) {
// We shouldn't stop the play back at this point, just pass the EOS
// flag to decoder, we will get it again from the
// dequeueOutputBuffer
Log.d("DecodeActivity", "InputBuffer BUFFER_FLAG_END_OF_STREAM");
System.out.println("DecodeActivity InputBuffer BUFFER_FLAG_END_OF_STREAM");
decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
isEOS = true;
} else {
try {
decoder.queueInputBuffer(inIndex, 0, sampleSize, extractor.getSampleTime(), 0);
extractor.advance();
} catch (IllegalStateException e) {
// Ignore the exception.
}
}
}
}
int outIndex = 0;
try {
outIndex = decoder.dequeueOutputBuffer(info, 10000);
} catch (IllegalStateException e) {
// Ignore the exception and break.
break;
}
switch (outIndex) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
Log.d("DecodeActivity", "INFO_OUTPUT_BUFFERS_CHANGED");
outputBuffers = decoder.getOutputBuffers();
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
Log.d("DecodeActivity", "New format " + decoder.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
Log.d("DecodeActivity", "dequeueOutputBuffer timed out!");
break;
default:
ByteBuffer buffer = outputBuffers[outIndex];
Log.v("DecodeActivity", "We can't use this buffer but render it due to the API limit, " + buffer);
// We use a very simple clock to keep the video FPS, or the video
// playback will be too fast
while (info.presentationTimeUs / 1000 > System.currentTimeMillis() - startMs) {
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
try {
System.out.println("Diaplayed a frame................");
decoder.releaseOutputBuffer(outIndex, true);
sleep(35);
} catch (IllegalStateException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
// All decoded frames have been rendered, we can stop playing now
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
Log.d("DecodeActivity", "OutputBuffer BUFFER_FLAG_END_OF_STREAM");
//finish();
}
}
}
使用的所有变量都是全局声明的。
这是我使用按钮暂停和恢复的代码
public void holdButton(View view) {
if(onHold == false) {
System.out.println("HoldCall................");
onHold = true;
stopPlayer();
decoder.stop();
} else {
System.out.println("ResumeCall................");
onHold = false;
initPlayerThread(sv.getHolder());
startPlayer();
}
}
public void initPlayerThread(SurfaceHolder holder) {
if (mPlayer == null) {
System.out.println("PlayerThread new.................");
mPlayer = new PlayerThread(holder.getSurface());
System.out.println("PlayerThread going to start.................");
} else {
mPlayer = null;
mPlayer = new PlayerThread(holder.getSurface(), 2);
System.out.println("PlayerThread started.................");
}
}
PlayerThread(holder.getSurface(), 2);
将启动解码器。 当我按下holdButton时它会暂停,但当再次按下时它应该恢复,但是在恢复时
extractor.readSampleData(buffer, 0);
这一行给出了分段错误。 有时按下holdButton暂停也会出现。
我无法弄清楚问题。
请帮助....................
...谢谢
这是恢复后的logcat信息。
11-24 23:55:02.969: D/DecodeActivity(1941): dequeueOutputBuffer timed out!
11-24 23:55:02.969: I/System.out(1941): read a sample......................
11-24 23:55:02.981: I/System.out(1941): buffer len = 65536java.nio.DirectByteBuffer[position=0,limit=65536,capacity=65536]
11-24 23:55:02.981: I/System.out(1941): read a sample......................
11-24 23:55:02.989: I/System.out(1941): ENC LENGTH552
11-24 23:55:03.000: I/System.out(1941): HoldCall................
11-24 23:55:03.021: D/DecodeActivity(1941): dequeueOutputBuffer timed out!
11-24 23:55:03.040: I/System.out(1941): read a sample......................
11-24 23:55:03.049: D/DecodeActivity(1941): dequeueOutputBuffer timed out!
11-24 23:55:03.239: I/Choreographer(1941): Skipped 32 frames! The application may be doing too much work on its main thread.
11-24 23:55:10.209: I/System.out(1941): ResumeCall................
11-24 23:55:10.269: I/OMXClient(1941): Using client-side OMX mux.
11-24 23:55:10.359: I/System.out(1941): PlayerThread started.................
11-24 23:55:10.409: I/System.out(1941): in run......................
11-24 23:55:10.419: I/System.out(1941): read a sample......................
11-24 23:55:10.429: I/System.out(1941): buffer len = 65536java.nio.DirectByteBuffer[position=0,limit=65536,capacity=65536]
11-24 23:55:10.449: A/libc(1941): Fatal signal 11 (SIGSEGV) at 0x4a3bd000 (code=2), thread 2005 (Thread-101)
我无法理解这个原因................