ijkplayer是Bilibili基于ffmpeg开发并开源的轻量级视频播放器,支持播放本地网络视频,也支持流媒体播放。
compile 'com.squareup:otto:1.3.8' compile 'tv.danmaku.ijk.media:ijkplayer-java:0.8.4' compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.4' compile 'tv.danmaku.ijk.media:ijkplayer-armv5:0.8.4' compile 'tv.danmaku.ijk.media:ijkplayer-arm64:0.8.4' compile 'tv.danmaku.ijk.media:ijkplayer-x86:0.8.4' compile 'tv.danmaku.ijk.media:ijkplayer-x86_64:0.8.4'还有权限:
<uses-permission android:name="android.permission.INTERNET" />
接下来就是具体实现代码:
XML很简单 一个播放的SurfaceView 一个暂停按钮 一个播放按钮:
<SeekBar android:max="100" android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@+id/surfaceView" /> <LinearLayout android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" android:layout_below="@+id/seekbar" android:layout_alignParentStart="true"> <Button android:id="@+id/buttonOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnClick" android:text="暂停" /> <Button android:id="@+id/buttonTwo" android:layout_width="wrap_content" android:onClick="btnClick" android:layout_height="wrap_content" android:text="播放" /> LinearLayout> <SurfaceView android:id="@+id/surfaceView" android:layout_width="480dp" android:layout_height="400dp" android:layout_alignParentTop="true" android:layout_alignParentStart="true" />
需要实现的接口:
public class MainActivity extends Activity implements View.OnClickListener, SurfaceHolder.Callback, IMediaPlayer.OnPreparedListener, IMediaPlayer.OnCompletionListener,IMediaPlayer.OnSeekCompleteListener, IMediaPlayer.OnBufferingUpdateListener,SeekBar.OnSeekBarChangeListener
资源URI
private String mUriTwo = "http://covertness.qiniudn.com/android_zaixianyingyinbofangqi_test_baseline.mp4";
声明一些变量
IjkMediaPlayer mediaPlayer; private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private Button mButtonone; private Button mButtontwo; private SeekBar seekbar; int currentP; boolean isSeek; boolean isprd;
实例化控件和一些监听器
mButtonone =(Button)findViewById(R.id.buttonOne); mButtontwo =(Button)findViewById(R.id.buttonTwo); seekbar = (SeekBar) findViewById(R.id.seekbar); seekbar.setOnSeekBarChangeListener(this); mButtonone.setOnClickListener(this); mButtontwo.setOnClickListener(this); surfaceView= (SurfaceView)findViewById(R.id.surfaceView); surfaceHolder=surfaceView.getHolder(); surfaceHolder.addCallback(this);
mediaPlayer=new IjkMediaPlayer();//直接new个IjkMediaPlayer 就行 try { mediaPlayer.setDataSource(mUriTwo); } catch (IOException e) { e.printStackTrace(); }
//mediaPlayer准备工作 mediaPlayer.setOnPreparedListener(this); //MediaPlayer完成 mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnBufferingUpdateListener(this);
@Override public void surfaceCreated(SurfaceHolder holder) { //连接ijkPlayer 和surfaceHOLDER mediaPlayer.setDisplay(holder); //开启异步准备 mediaPlayer.prepareAsync(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mediaPlayer.isPlaying()){ mediaPlayer.pause(); } } /** * 当异步开启后 执行这个方法 * @param iMediaPlayer */ @Override public void onPrepared(IMediaPlayer iMediaPlayer) { isprd = true; //设置seekbar的长度为视频播放的长度 seekbar.setMax((int)iMediaPlayer.getDuration()); //开启异步 handler.post(runnable); //开启异步后视频会自动播放 所以需要暂停 if (mediaPlayer.isPlaying()){ mediaPlayer.pause(); } }
Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { //加个开关 在刷进度条的时候先判断是不是正在拖动 if (isSeek) return; //获取当前视频播放的位置 int cur = (int)mediaPlayer.getCurrentPosition(); if(cur > 0 && cur != currentP){ currentP = cur; seekbar.setProgress(currentP);//设置进度条 } //每50毫秒刷新一下进度条 handler.postDelayed(runnable,50); } };
接下来是拖动Seekbar设置:
@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { //一个开关 当开始拖动时改变成true isSeek = true; //判断是不是正在播放 如果正在播放就暂停 if (mediaPlayer.isPlaying()){ mediaPlayer.pause(); } } @Override public void onStopTrackingTouch(SeekBar seekBar) { //一个开关 当开始拖动完成时改变乘false isSeek = false; //设置到当前拖动进度条的位置 然后start mediaPlayer.seekTo(seekBar.getProgress()); mediaPlayer.start(); }播放暂停按钮:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonOne: mediaPlayer.pause(); break; case R.id.buttonTwo: if (!isprd){ return; } if (!mediaPlayer.isPlaying()){ mediaPlayer.start(); } break; } }