IjkPlayerView代码
时间: 2023-07-02 13:07:38 浏览: 117
ijkplayer源码
IjkPlayerView是一个基于IjkPlayer的Android视频播放器视图,下面是一个简单的代码示例:
1.在build.gradle文件中添加依赖:
```
implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
implementation 'tv.danmaku.ijk.media:ijkplayer-exo:0.8.8'
```
2.创建IjkPlayerView类:
```
public class IjkPlayerView extends FrameLayout implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaPlayer mMediaPlayer;
private ImageView mPlayPauseView;
private ProgressBar mProgressBar;
private TextView mTimeView;
private boolean mIsPrepared;
private boolean mIsPlaying;
private boolean mIsSurfaceCreated;
private boolean mIsFullScreen;
public IjkPlayerView(Context context) {
this(context, null);
}
public IjkPlayerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IjkPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.view_ijk_player, this, true);
mSurfaceView = findViewById(R.id.surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mPlayPauseView = findViewById(R.id.play_pause_view);
mProgressBar = findViewById(R.id.progress_bar);
mTimeView = findViewById(R.id.time_view);
mPlayPauseView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mIsPlaying) {
pause();
} else {
play();
}
}
});
}
public void play(String url) {
if (TextUtils.isEmpty(url)) {
return;
}
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnErrorListener(this);
}
try {
mMediaPlayer.setDataSource(url);
mMediaPlayer.prepareAsync();
mProgressBar.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
}
}
public void play() {
if (mMediaPlayer != null && mIsPrepared) {
mMediaPlayer.start();
mPlayPauseView.setImageResource(R.drawable.ic_pause);
mIsPlaying = true;
}
}
public void pause() {
if (mMediaPlayer != null && mIsPrepared) {
mMediaPlayer.pause();
mPlayPauseView.setImageResource(R.drawable.ic_play);
mIsPlaying = false;
}
}
public void stop() {
if (mMediaPlayer != null && mIsPrepared) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
mIsPrepared = false;
mIsPlaying = false;
mSurfaceView.setVisibility(View.GONE);
mProgressBar.setVisibility(View.GONE);
mTimeView.setVisibility(View.GONE);
mPlayPauseView.setVisibility(View.GONE);
}
}
public void toggleFullScreen() {
mIsFullScreen = !mIsFullScreen;
if (mIsFullScreen) {
((Activity) getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
((Activity) getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.ijk_player_height)));
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mIsSurfaceCreated = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mMediaPlayer != null && mIsSurfaceCreated && mIsPrepared) {
mMediaPlayer.setDisplay(mSurfaceHolder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mIsSurfaceCreated = false;
}
@Override
public void onPrepared(MediaPlayer mp) {
mIsPrepared = true;
mProgressBar.setVisibility(View.GONE);
mSurfaceView.setVisibility(View.VISIBLE);
mTimeView.setVisibility(View.VISIBLE);
mPlayPauseView.setVisibility(View.VISIBLE);
mMediaPlayer.setDisplay(mSurfaceHolder);
play();
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
int videoWidth = mp.getVideoWidth();
int videoHeight = mp.getVideoHeight();
if (videoWidth != 0 && videoHeight != 0) {
float ratio = Math.min((float) getWidth() / videoWidth, (float) getHeight() / videoHeight);
int newWidth = (int) (videoWidth * ratio);
int newHeight = (int) (videoHeight * ratio);
LayoutParams layoutParams = (LayoutParams) mSurfaceView.getLayoutParams();
layoutParams.width = newWidth;
layoutParams.height = newHeight;
mSurfaceView.setLayoutParams(layoutParams);
}
}
@Override
public void onCompletion(MediaPlayer mp) {
mIsPlaying = false;
mPlayPauseView.setImageResource(R.drawable.ic_play);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mProgressBar.setVisibility(View.GONE);
Toast.makeText(getContext(), "播放出错", Toast.LENGTH_SHORT).show();
return false;
}
}
```
3.创建view_ijk_player.xml布局文件:
```
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="@dimen/ijk_player_height"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/play_pause_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_play"/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:textColor="#ffffff"
android:textSize="12sp"
android:text="00:00/00:00"/>
</merge>
```
使用时可以在布局文件中添加IjkPlayerView,然后调用play方法即可播放视频,调用stop方法停止播放。可以在Activity中添加如下代码:
```
private IjkPlayerView mPlayerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayerView = findViewById(R.id.player_view);
mPlayerView.play("http://xxx.mp4");
}
@Override
protected void onStop() {
super.onStop();
mPlayerView.stop();
}
```
以上代码仅供参考,实际应用中还需要根据需求进行修改和优化。
阅读全文