Android原生的播放器VideoView
常用方法:
- videoView.start(); //開(kāi)始播放
- videoView.pause(); //暫停
- videoView.stopPlayback(); //停止播放
- videoView.isPlaying(); //獲取是否在播放狀態(tài)
- videoView.setVideoURI(Uri uri); //設(shè)置視頻播放uri
- videoView.setVideoPath(String path); //設(shè)置視頻播放路徑
- videoView.seekTo(int msec); //跳轉(zhuǎn)到設(shè)置時(shí)間
- videoView.getCurrentPosition(); //獲取當(dāng)前播放時(shí)間
- videoView.getDuration(); //獲取該視頻播放時(shí)長(zhǎng)
- videoView.setMediaController(MediaController controller); //設(shè)置播放控制器
- videoView.setOnPreparedListener(MediaPlayer.OnPreparedListener listener); //加載完成監(jiān)聽(tīng)
- videoView.setOnCompletionListener(MediaPlayer.OnCompletionListener listener); //設(shè)置播放完成監(jiān)聽(tīng)
- videoView.setOnErrorListener(MediaPlayer.OnErrorListener listener); //播放失敗監(jiān)聽(tīng)
代碼
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播放器的使用
下面介紹如何快速使用這款功能強(qiáng)大的播放器:
添加so庫(kù)
添加依賴
加入demo中必要的類
使用ijkplayer播放視頻
1.ijkplayer官方提供的下載地址 https://github.com/Bilibili/ijkplayer 是沒(méi)有經(jīng)過(guò)編譯的,我們這里使用別人編譯好的so庫(kù)文件就可以了(自己編譯也可以)。
- 添加與so庫(kù)同版本的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();
}
//使用滑動(dòng)控制的話解開(kāi)注釋
@Override
public boolean onTouchEvent(MotionEvent event) {
if (player.gestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
/**
* 可左半屏滑動(dòng)控制亮度 右半屏控制音量 雙擊切換比例 (無(wú)提示)
*/
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庫(kù)同版本的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測(cè)試視頻");
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
特點(diǎn):
- 自帶全屏播放放和浮層小窗口播放
- 可以完全自定義UI
- 手勢(shì)修改進(jìn)度和音量
- 重力感應(yīng)自動(dòng)全屏
- 支持https和rtmp
1.添加類庫(kù)
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.設(shè)置視頻地址厨幻、縮略圖地址雕崩、標(biāo)題
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.** { *; }
播放效果:
功能相對(duì)復(fù)雜定制型強(qiáng)可以接入騰訊的播放器 https://cloud.tencent.com/document/product/266/7938
播放效果: