导入依赖包:
// 饺子视频
implementation 'cn.jzvd:jiaozivideoplayer:7.7.0'
//腾讯短视屏
implementation 'com.tencent.liteav:LiteAVSDK_Professional:latest.release'
添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
首先,饺子视频:
mainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<cn.jzvd.JzvdStd
android:layout_width="match_parent"
android:layout_height="500dp"
android:id="@+id/jzvd" />
</LinearLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import cn.jzvd.JzvdStd;
public class MainActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
super.onDestroy();
// 释放所有资源
JzvdStd.releaseAllVideos();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取播放器
JzvdStd jzvdStd = findViewById(R.id.jzvd);
// 设置播放器的视频url,视频标题,有播放按钮的模式
jzvdStd.setUp("https://zkw.oss-cn-beijing.aliyuncs.com/video/4329fee0bb96438bb9914a753f8154da.mp4", "啊啊啊", JzvdStd.SCREEN_NORMAL);
// 设置视频封面的图片填充方式为填满整个容器
jzvdStd.posterImageView.setScaleType(ImageView.ScaleType.FIT_XY);
// 设置视频封面图片
Glide.with(this).load("https://zkw.oss-cn-beijing.aliyuncs.com/video/4329fee0bb96438bb9914a753f8154da.mp4?x-oss-process=video/snapshot,t_7000,f_jpg").into(jzvdStd.posterImageView);
// 开始播放视频
jzvdStd.startVideo();
}
}
效果图如下:
应用打开后,就开始播放了。不过需要注意的是,当退出应用的时候,要释放视频资源,不然应用退出后,视频依然在播放......还有声音......
然后,腾讯视频:
activity_test.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.TestActivity">
<com.tencent.rtmp.ui.TXCloudVideoView
android:id="@+id/tXCloudVideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" />
</LinearLayout>
TestActivity.java:
package com.example.supermarket.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import com.example.supermarket.R;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;
public class TestActivity extends AppCompatActivity {
private TXVodPlayer vodPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
vodPlayer = new TXVodPlayer(this);
vodPlayer.setRenderRotation(TXLiveConstants.RENDER_ROTATION_PORTRAIT);
TXCloudVideoView txCloudVideoView = findViewById(R.id.tXCloudVideoView);
vodPlayer.setPlayerView(txCloudVideoView);
vodPlayer.setMute(false);// 开启声音
vodPlayer.setLoop(true);
vodPlayer.startVodPlay("https://zkw.oss-cn-beijing.aliyuncs.com/video/4329fee0bb96438bb9914a753f8154da.mp4");
}
}
@Override
public void onDestroy() {
super.onDestroy();
JzvdStd.releaseAllVideos();
}
@Override
public void onResume() {
super.onResume();
if (JzvdStd!= null) {
JzvdStd.startVideo();
}
if (vodPlayer!=null){
vodPlayer.resume();
}
}
@Override
public void onPause() {
super.onPause();
if (JzvdStd != null) {
JzvdStd.goOnPlayOnPause();
}
if (vodPlayer!=null){
vodPlayer.pause();
}
}