使用MediaCodec+SurfaceView播放视频(无音频)

public class MediaCodecUtil1 {
    private String mFilePath;
    private MediaCodec mMediaCodec;
    private MediaExtractor mMediaExtractor;
    private Surface mSurface;
    private boolean mIsAvailable;
    private ByteBuffer[] mInputBuffers;
    private ByteBuffer[] mOutputBuffers;

    public MediaCodecUtil1(String filePath, Surface surface) {
        mFilePath = filePath;
        mSurface = surface;
    }

    private void init() {
        mIsAvailable = false;
        mMediaExtractor = new MediaExtractor();
        try {
            mMediaExtractor.setDataSource(mFilePath);
            int trackCount = mMediaExtractor.getTrackCount();
            for (int i = 0; i < trackCount; i++) {
                MediaFormat mediaFormat = mMediaExtractor.getTrackFormat(i);
                String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
                if (mime.startsWith("video/")) {
                    mMediaExtractor.selectTrack(i);
                    mMediaCodec = MediaCodec.createDecoderByType(mime);
                    mMediaCodec.configure(mediaFormat, mSurface, null, 0);
                    mIsAvailable = true;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        init();
        if (mIsAvailable) {
            mMediaCodec.start();
            mInputBuffers = mMediaCodec.getInputBuffers();
            mOutputBuffers = mMediaCodec.getOutputBuffers();
            new Thread(new EncoderThread()).start();
        }
    }

    private class EncoderThread implements Runnable {
        @Override
        public void run() {
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            long startTime = System.currentTimeMillis();
            while (mIsAvailable) {
                int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);
                if (inputBufferIndex >= 0) {
                    ByteBuffer inputBuffer = mInputBuffers[inputBufferIndex];
                    inputBuffer.clear();
                    int sampleSize = mMediaExtractor.readSampleData(inputBuffer, 0);
                    if (sampleSize > 0) {
                        mMediaExtractor.advance();
                        mMediaCodec.queueInputBuffer(inputBufferIndex, 0, sampleSize, mMediaExtractor.getSampleTime(), 0);
                    }
                }

                int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
                if (outputBufferIndex >= 0) {
                    long sleepTime = bufferInfo.presentationTimeUs / 1000 - (System.currentTimeMillis() - startTime);
                    if (sleepTime > 0) {
                        SystemClock.sleep(sleepTime);
                    }
                    ByteBuffer outBuffer = mOutputBuffers[outputBufferIndex];
                    mMediaCodec.releaseOutputBuffer(outputBufferIndex, true);
                }
            }
            mMediaExtractor.release();
            mMediaCodec.stop();
            mMediaCodec.release();
            Log.i("==", "播放完成");
        }
    }

    public void stop() {
        mIsAvailable = false;
    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leilu000/article/details/80341061
个人分类: 音视频
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭