当前位置:   article > 正文

Android之播放本地视频和Url视频方法_android 播放本地视频

android 播放本地视频

一、播放本地视频文件

根据文件路径在浏览器中播放,可用于视频预览等场景

效果:

用浏览器播放本地视频

文件路径例子:
/storage/emulated/0/Android/data/com.custom.jfrb/files/Movies/1697687179497.mp4

File file = new File("文件路径");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Uri contentUri = FileProvider.getUriForFile(UploadVideoActivity.this, getContext().getPackageName() + ".provider", file);
        intent.setDataAndType(contentUri, "video/*");
} else {
        intent.setDataAndType(Uri.fromFile(file), "video/*");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

二、播放Url视频(GSYVideoPlayer类的使用)

效果:

播放Url视频

1、导入依赖

implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer:v8.1.7-release-jitpack'
  • 1

2、xml文件

<com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
            android:id="@+id/video_player"
            android:layout_width="match_parent"
            android:layout_height="220dp" />
  • 1
  • 2
  • 3
  • 4

3、Activity中调用

(1) 绑定控件

@InjectView(id = R.id.video_player)
StandardGSYVideoPlayer mVideoPlayer;
  • 1
  • 2

(2)播放视频
视频Url例子:https://recordcdn-qk.jfdaily.com/upload/vod/user1694515885005019/1697678251135382/preview/video.mp4

//播放横屏视频
 boolean setUp = mVideoPlayer.setUp("视频Url", true, "名字");
//设置封面
//        if (setUp) {
//            Glide.with(this).load(productItem.getCapture()).into((ImageView) mVideoPlayer.getThumbImageView());
//        }
//隐藏自带的标题和返回键
mVideoPlayer.getTitleTextView().setVisibility(View.GONE);
 mVideoPlayer.getBackButton().setVisibility(View.GONE);
 
//全屏按键
mVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
             mVideoPlayer.startWindowFullscreen(ProductDetailActivity.this,false,true);
       }
});
GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT);
//根据视频尺寸,自动选择竖屏全屏或者横屏全屏
mVideoPlayer.setAutoFullWithSize(true);
//全屏动画
mVideoPlayer.setShowFullAnimation(true);
//循环播放
mVideoPlayer.setLooping(true);
//开始播放
mVideoPlayer.startPlayLogic();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

(3)相关生命周期中添加操作

@Override
    public void onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)){
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoPlayer.onVideoResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoPlayer.onVideoPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoADManager.releaseAllVideos();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

三、使用腾讯播放器播放视频Url(无UI集成)

1、导入依赖

官方文档:腾讯播放器,无UI集成(Android),SDK指引
版本可更换

compile 'com.tencent.liteav:LiteAVSDK_Professional:11.7.0.13946'
  • 1

2、腾讯播放器初始化

private void TXVideoPlayerInit(){
        String licenceURL = ......; // 获取到的 licence url
        String licenceKey = ......; // 获取到的 licence key
        TXLiveBase.getInstance().setLicence(this, licenceURL, licenceKey);
        TXLiveBase.setListener(new TXLiveBaseListener() {
            @Override
            public void onLicenceLoaded(int result, String reason) {
                Log.d(TAG, "腾讯播放器初始化:onLicenceLoaded: result:" + result + ", reason:" + reason);
            }
        });

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3、使用

(1)xml文件中放入控件
<com.tencent.rtmp.ui.TXCloudVideoView
                android:id="@+id/video_player"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
  • 1
  • 2
  • 3
  • 4
(2)Activity中调用
@InjectView(id = R.id.video_player)
TXCloudVideoView mVideoPlayer;

TXVodPlayer mVodPlayer;
  • 1
  • 2
  • 3
  • 4

初始化:

mVodPlayer = new TXVodPlayer(mContext);
 //关联 player 对象与视频渲染 view
mVodPlayer.setPlayerView(mVideoPlayer);
  • 1
  • 2
  • 3

播放Url:

mVodPlayer.startVodPlay(“视频Url”);
  • 1

停止播放、恢复播放方法,更多其他方法可参考官方文档:点播场景

if(mVodPlayer != null)
{
	mVodPlayer.pause();
}
  • 1
  • 2
  • 3
  • 4
if(mVodPlayer != null)
{
	mVodPlayer.resume();
}
  • 1
  • 2
  • 3
  • 4
(3)生命周期中需要补全的
    @Override
    protected void onDestroy() {
        mVodPlayer.stopPlay(true); // true 代表清除最后一帧画面
        mVideoPlayer.onDestroy();
        super.onDestroy();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

四、持续更新中…

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/879241
推荐阅读
相关标签
  

闽ICP备14008679号