1.10 Android自制简单音视频播放器

  • A+
所属分类:安卓 音视频

Android 音视频的代码好多都属于native层,Nupalyer和AweSomePlayer控制流程复杂, 学习难度比较大,作为app人员来说接触的机会又比较少,导致想学习知识的人门槛比较高。

所以这边通过上层的控制,让大家慢慢熟悉播放器的原理。

因此将非常简单的音视频播放器,拿出来练习,源码提供大家参考。

本app采用上层的MediaCodec 、 MediaExtractor 将音视频播放,播放显示器采用常用的SurfaceView,声音输出采用AudioTrack,步骤大概如下:

1、在xml中创建SurfaceView 获取并添加监听。

2、开启线程

3、将输入源给分离器MediaExtractor,MediaExtractor差分数据和解析出相关信息

4、通过分离器MediaExtractor分里的信息分别创建视频解码器mVideoMediaCodec,和音频就解码器mAudioMediaCodec

5、启动两个解码器,

6、先读取分离器中的数据,判断是音频数据还是视频数据。

7、分别将音视频数据送入相应的解码器

8、解析出相应的数据,分别输出数据

9、视频音频完美播放

 

调用方式

package com.eebbk.sampleplayer;

import android.media.MediaFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.eebbk.sampleplayer.content.SamplePlayer;

public class SamplePlayerActivity extends AppCompatActivity {

private SurfaceView mSurfaceView ;
int mWidth;
int mHeight;
SamplePlayer mPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_sample_audio_video);
mPlayer = new SamplePlayer();
mPlayer.setDataSource("/sdcard/15.mp4");
MediaFormat format =mPlayer.getVideoFormat();
if ( format !=null) {
mWidth = format.getInteger(MediaFormat.KEY_WIDTH);
mHeight = format.getInteger(MediaFormat.KEY_HEIGHT);
}
mSurfaceView = (SurfaceView)findViewById(R.id.surface);
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mHeight != 0 && mWidth != 0) {
mSurfaceView.getHolder().setFixedSize(mHeight, mWidth);
}
mPlayer.setSurface(holder.getSurface());
mPlayer.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
});
}

}

 

播放器源码

package com.eebbk.sampleplayer.content;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.util.Log;
import android.view.Surface;

import java.nio.ByteBuffer;

/**
* Created by djh on 17-5-11.
*/
public class SamplePlayer {
private final String TAG = "SamplePlayer";
private MediaCodec mVideoMediaCodec;
private MediaExtractor mExtractor;
private Surface mSurface;
private int mVideoTrackIndex = -1;
private int mAudioTrackIndex = -1;
private MediaFormat mVideoFormatCodec = null;
private MediaFormat mAudioFormatCodec = null;
private Thread mThread;
private long startMs;
private boolean mHasVideo;
private boolean mHasAudio;
private final long kTimeOutUs = 10000;
private MediaCodec mAudioMediaCodec;
private AudioTrack mAudioTrack;

public SamplePlayer() {
mExtractor = new MediaExtractor();
mHasVideo = false;
mHasAudio = false;
}

public void setDataSource(String url) {
try {
mExtractor.setDataSource(url);
int tackNum = mExtractor.getTrackCount();
for (int i = 0; i < tackNum; i++) {
MediaFormat format = mExtractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
mVideoFormatCodec = format;
mVideoTrackIndex = i;
mExtractor.selectTrack(i);
mHasVideo = true;
}
if (mime.startsWith("audio/")) {

mExtractor.selectTrack(i);

int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
int audioMinBufSize = AudioTrack.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
//创建音频输出环境
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, audioMinBufSize, AudioTrack.MODE_STREAM);
mAudioTrack.play();

mAudioTrackIndex = i;
mAudioFormatCodec = format;
mHasAudio = true;
}
}

} catch (Exception e) {
e.printStackTrace();
}

if (!mHasAudio && !mHasVideo) {
Log.e(TAG, "wenjian wuxiao end !");
return;
}

}

public void setSurface(Surface surface) {
mSurface = surface;
}

public void start() {
startMs = System.currentTimeMillis();
mThread = new Thread(new Runnable() {
@Override
public void run() {
decode();
}
});
mThread.start();

}

public MediaFormat getVideoFormat() {
return mVideoFormatCodec;
}

public void decode() {
ByteBuffer[] videoInputBuffers = null;

ByteBuffer[] audioInputBuffers = null;
ByteBuffer[] audioOutputBuffers = null;
if (mHasVideo) {
String mime = mVideoFormatCodec.getString(MediaFormat.KEY_MIME);
try {
mVideoMediaCodec = MediaCodec.createDecoderByType(mime);
} catch (Exception e) {
e.printStackTrace();
}
if (mVideoMediaCodec == null) {
Log.d(TAG, "mVideoMediaCodec == null");
return;
}
mVideoMediaCodec.configure(mVideoFormatCodec, mSurface, null, 0);
mVideoMediaCodec.start();
videoInputBuffers = mVideoMediaCodec.getInputBuffers();
}

if (mHasAudio) {
String mime = mAudioFormatCodec.getString(MediaFormat.KEY_MIME);
try {
mAudioMediaCodec = MediaCodec.createDecoderByType(mime);
} catch (Exception e) {
e.printStackTrace();
}
if (mAudioMediaCodec == null) {
Log.d(TAG, "mVideoMediaCodec == null");
return;
}
mAudioMediaCodec.configure(mAudioFormatCodec, null, null, 0);
mAudioMediaCodec.start();
audioInputBuffers = mAudioMediaCodec.getInputBuffers();
audioOutputBuffers = mAudioMediaCodec.getOutputBuffers();
}

while (true) {
try {
int index = mExtractor.getSampleTrackIndex();
if (mHasVideo) {
if (index == mVideoTrackIndex) {
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
int inputBufIndex = mVideoMediaCodec.dequeueInputBuffer(kTimeOutUs);
if (inputBufIndex >= 0) {
ByteBuffer dstBuf = videoInputBuffers[inputBufIndex];
int sampleSize = mExtractor.readSampleData(dstBuf, 0);
if (sampleSize < 0) {
mVideoMediaCodec.queueInputBuffer(inputBufIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
break;
} else {
mVideoMediaCodec.queueInputBuffer(inputBufIndex, 0, sampleSize, mExtractor.getSampleTime(), 0);
mExtractor.advance();
}
}
int res = mVideoMediaCodec.dequeueOutputBuffer(info, kTimeOutUs);

if (res >= 0) {
int outputBufIndex = res;
mVideoMediaCodec.releaseOutputBuffer(outputBufIndex, true);

}
}

}

if (mHasAudio) {
if (index == mAudioTrackIndex) {
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
int inputBufIndex = mAudioMediaCodec.dequeueInputBuffer(kTimeOutUs);
if (inputBufIndex >= 0) {
ByteBuffer dstBuf = audioInputBuffers[inputBufIndex];

int sampleSize = mExtractor.readSampleData(dstBuf, 0);//从分离器拿数据
if (sampleSize < 0) {
mAudioMediaCodec.queueInputBuffer(inputBufIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
} else {
long mediatime = mExtractor.getSampleTime();
//将数据送入解码器
mAudioMediaCodec.queueInputBuffer(inputBufIndex, 0, sampleSize, mediatime, 0);
mExtractor.advance();
}
}
//从解码器输出
int res = mAudioMediaCodec.dequeueOutputBuffer(info, kTimeOutUs); //将数据从解码器拿出来
if (res == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
audioOutputBuffers = mAudioMediaCodec.getOutputBuffers();

Log.d(TAG, "INFO_OUTPUT_BUFFERS_CHANGED:");
}
if (res >= 0) {
int outputBufIndex = res;
ByteBuffer buf = audioOutputBuffers[outputBufIndex];
final byte[] pcmData = new byte[info.size];
buf.get(pcmData);
buf.clear();
if (pcmData.length > 0) {
//对音频数据pcm进行输出
mAudioTrack.write(pcmData, 0, pcmData.length);
}
//告诉显示器释放并显示这个内容
mAudioMediaCodec.releaseOutputBuffer(outputBufIndex, true);
}
}
}

} catch (RuntimeException e) {
e.printStackTrace();
}
}
if (mVideoMediaCodec != null) {
mVideoMediaCodec.stop();
mVideoMediaCodec.release();
mVideoMediaCodec = null;
}

if (mAudioMediaCodec != null) {
mAudioMediaCodec.stop();
mAudioMediaCodec.release();
mAudioMediaCodec = null;
}

if (mExtractor != null) {
mExtractor.release();
mExtractor = null;
}
}
}

 

 

Android 音视频学习基础--1.1 音视频基础知识
Android 音视频学习基础--1.2 需要认识的一些工具
Android 音视频学习基础--1.3 主流的开源项目
Android 音视频学习基础--1.4 ffmpeg pcm输出
Android 音视频学习基础--1.5 ffmpeg yuv输出
Android 音视频学习基础--1.6 ffmpeg 简单视频播放器
Android 音视频学习基础--1.7 Android最简单的音频播放器
Android 音视频学习基础--1.8 Android最简单的音频播放器
Android 音视频学习基础--1.9 Android最简单的视频播放器
Android 音视频学习基础--1.10 Android自制简单音视频播放器

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: