Android原生的播放器VideoView
常用方法:
- videoView.start(); //开始播放
- videoView.pause(); //暂停
- videoView.stopPlayback(); //停止播放
- videoView.isPlaying(); //获取是否在播放状态
- videoView.setVideoURI(Uri uri); //设置视频播放uri
- videoView.setVideoPath(String path); //设置视频播放路径
- videoView.seekTo(int msec); //跳转到设置时间
- videoView.getCurrentPosition(); //获取当前播放时间
- videoView.getDuration(); //获取该视频播放时长
- videoView.setMediaController(MediaController controller); //设置播放控制器
- videoView.setOnPreparedListener(MediaPlayer.OnPreparedListener listener); //加载完成监听
- videoView.setOnCompletionListener(MediaPlayer.OnCompletionListener listener); //设置播放完成监听
- videoView.setOnErrorListener(MediaPlayer.OnErrorListener listener); //播放失败监听
代码
public class MediaActivity extends AppCompatActivity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media);
videoView = findViewById(R.id.videoView);
videoView.setVideoPath(Contents.VIDEO_URL);
videoView.setMediaController(new MediaController(this));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});
}
}
播放效果:
Android Ijkplayer播放器的使用
下面介绍如何快速使用这款功能强大的播放器:
添加so库
添加依赖
加入demo中必要的类
使用ijkplayer播放视频
1.ijkplayer官方提供的下载地址 https://github.com/Bilibili/ijkplayer 是没有经过编译的,我们这里使用别人编译好的so库文件就可以了(自己编译也可以)。
- 添加与so库同版本的ijkplayer-java依赖
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'
3. 在官方提供的ijkplayer下载包有该播放器的demo,其中example\src\main\java\tv\danmaku\ijk\media\example\widget\media包中的一些类是我们需要的
- 使用ijkplayer播放视频
<com.example.videodemo.media.IjkVideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="200dp" />
public class BilibiliActivity extends AppCompatActivity {
private PlayerManager player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bilibili);
//initVideo();
IjkVideoView mVideoView = findViewById(R.id.video_view);
mVideoView.setAspectRatio(IRenderView.AR_ASPECT_FIT_PARENT);
mVideoView.setVideoURI(Uri.parse(Contents.VIDEO_URL));
mVideoView.start();
}
//使用滑动控制的话解开注释
@Override
public boolean onTouchEvent(MotionEvent event) {
if (player.gestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
/**
* 可左半屏滑动控制亮度 右半屏控制音量 双击切换比例 (无提示)
*/
private void initVideo() {
player = new PlayerManager(this);
player.setFullScreenOnly(true);
player.live(true);
player.setScaleType(PlayerManager.SCALETYPE_WRAPCONTENT);
player.playInFullScreen(true);
player.setPlayerStateListener(new PlayerManager.PlayerStateListener() {
@Override
public void onComplete() {
Log.e(" player status :", "complete");
}
@Override
public void onError() {
Log.e(" player status :", "error");
}
@Override
public void onLoading() {
Log.e(" player status :", "loading");
}
@Override
public void onPlay() {
Log.e(" player status :", "play");
}
});
player.play(Contents.VIDEO_URL);
IjkVideoView videoView = player.getVideoView();
videoView.setOnInfoListener((iMediaPlayer, i, i1) -> {
switch (i) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
break;
}
return false;
});
}
}
播放效果:
GSYVideoPlayer的使用
1.GSYVideoPlayer官方提供的下载地址 [https://github.com/CarGuo/GSYVideoPlayer
- 添加与so库同版本的ijkplayer-java依赖
implementation 'com.shuyu:GSYVideoPlayer:7.1.8'
- 使用
<com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
android:id="@+id/standardPlayer"
android:layout_width="match_parent"
android:layout_height="200dp" />
public class GSYActivity extends AppCompatActivity {
private StandardGSYVideoPlayer standardPlayer;
private Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gsyactivity);
standardPlayer=findViewById(R.id.standardPlayer);
btnStart=findViewById(R.id.btnStart);
standardPlayer.setUp(Contents.VIDEO_URL, false, "GSY测试视频");
btnStart.setOnClickListener(view -> {
standardPlayer.startPlayLogic();
});
standardPlayer.getFullscreenButton().setOnClickListener(view -> {
standardPlayer.startWindowFullscreen(this, false, true);
});
}
@Override
protected void onPause() {
super.onPause();
standardPlayer.onVideoPause();
}
@Override
protected void onResume() {
super.onResume();
standardPlayer.onVideoResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
GSYVideoManager.releaseAllVideos();
}
@Override
public void onBackPressed() {
//释放所有
standardPlayer.setVideoAllCallBack(null);
super.onBackPressed();
}
播放效果:
饺子视频播放器
1.JZVideo官方提供的下载地址 https://github.com/Jzvd/JZVideo
特点:
- 自带全屏播放放和浮层小窗口播放
- 可以完全自定义UI
- 手势修改进度和音量
- 重力感应自动全屏
- 支持https和rtmp
1.添加类库
implementation 'cn.jzvd:jiaozivideoplayer:7.7.0'
2.添加布局
<cn.jzvd.JzvdStd
android:id="@+id/jz_video"
android:layout_width="match_parent"
android:layout_height="200dp" />
3.设置视频地址、缩略图地址、标题
MyJzvdStd jzvdStd = (MyJzvdStd) findViewById(R.id.jz_video);
jzvdStd.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4"
, "追龙");
jzvdStd.posterImageView.setImage("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");
4.在Activity中
public class JiaoZiActivity extends AppCompatActivity {
private JzvdStd jzvdStd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jiaozi);
jzvdStd = findViewById(R.id.jiaoZiPlayer);
jzvdStd.setUp(Contents.VIDEO_URL
, "追龙", Jzvd.SCREEN_NORMAL);
jzvdStd.startVideo();
}
@Override
public void onBackPressed() {
if (JzvdStd.backPress()) {
return;
}
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
JzvdStd.releaseAllVideos();
}
}
5.在AndroidManifest.xml中
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait" /> <!-- or android:screenOrientation="landscape"-->
6.在proguard-rules.pro中按需添加
-keep public class cn.jzvd.JZMediaSystem {*; }
-keep public class cn.jzvd.demo.CustomMedia.CustomMedia {*; }
-keep public class cn.jzvd.demo.CustomMedia.JZMediaIjk {*; }
-keep public class cn.jzvd.demo.CustomMedia.JZMediaSystemAssertFolder {*; }
-keep class tv.danmaku.ijk.media.player.** {*; }
-dontwarn tv.danmaku.ijk.media.player.*
-keep interface tv.danmaku.ijk.media.player.** { *; }
播放效果:
功能相对复杂定制型强可以接入腾讯的播放器 https://cloud.tencent.com/document/product/266/7938
播放效果: