first commit

This commit is contained in:
zhongjin
2021-11-20 20:27:30 +08:00
commit 63f43af5b2
44 changed files with 1480 additions and 0 deletions
@@ -0,0 +1,269 @@
package net.spacen.xxh_video;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import android.widget.VideoView;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePlayerObserver;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import okhttp3.OkHttpClient;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
public class MainActivity extends AppCompatActivity {
public final String TAG = MainActivity.class.getSimpleName();
private Context mContext;
private V2TXLivePlayer mLivePlayer; //直播拉流的视频播放器
private TXCloudVideoView mVideoView;
private VideoView vv_view;
private SurfaceView sv_view;
private IjkMediaPlayer ijkMediaPlayer;
private String mPlayURL = "";
private boolean mIsPlaying = false;
private boolean mFetching = false; //是否正在获取视频源,测试专用
private OkHttpClient mOkHttpClient = null; //获取超低时延视频源直播地址
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
MyMqttService.setCLIENTID(getAndroidID());
MyMqttService.startService(this); //开启服务
EventBus.getDefault().register(this);
Toast.makeText(this, getAndroidID() + " 哈哈,我成功启动了!", Toast.LENGTH_LONG).show();
Log.e("AutoRun","哈哈,我成功启动了!");
//mPlayerView 即 step1 中添加的界面 view
mVideoView = (TXCloudVideoView) findViewById(R.id.video_view);
vv_view = (VideoView) findViewById(R.id.vv_view);
mContext = this;
//创建 player 对象
mLivePlayer = new V2TXLivePlayerImpl(mContext);
//关键 player 对象与界面 view
mLivePlayer.setRenderView(mVideoView);
String flvUrl = "rtmp://v.dl8.tv/show/mini";
//自动模式
mLivePlayer.setCacheParams(1.0f, 5.0f);
//极速模式
//mLivePlayer.setCacheParams(1.0f, 1.0f);
//流畅模式
//mLivePlayer.setCacheParams(5.0f, 5.0f);
//设置完成之后再启动播放
mLivePlayer.startPlay(flvUrl);
}
@Override
public void onBackPressed() {
stopPlay();
super.onBackPressed();
}
@Override
public void onDestroy() {
super.onDestroy();
destroy();
}
private void stopPlay() {
if (!mIsPlaying) {
return;
}
if (mLivePlayer != null) {
mLivePlayer.setObserver(null);
mLivePlayer.stopPlay();
}
mIsPlaying = false;
//处理UI相关操作
onPlayStop();
}
private void destroy() {
if (mOkHttpClient != null) {
mOkHttpClient.dispatcher().cancelAll();
}
if (mLivePlayer != null) {
mLivePlayer.stopPlay();
mLivePlayer = null;
}
if (mVideoView != null) {
mVideoView.onDestroy();
mVideoView = null;
}
}
public void onPlayStop() {
// mButtonPlay.setBackgroundResource(R.drawable.liveplayer_play_start_btn);
// mLayoutRoot.setBackgroundResource(R.drawable.liveplayer_content_bg);
// mImageRoot.setVisibility(View.VISIBLE);
// mLogInfoWindow.clear();
// stopLoadingAnimation();
}
private class MyPlayerObserver extends V2TXLivePlayerObserver {
@Override
public void onWarning(V2TXLivePlayer player, int code, String msg, Bundle extraInfo) {
Log.w(TAG, "[Player] onWarning: player-" + player + " code-" + code + " msg-" + msg + " info-" + extraInfo);
}
@Override
public void onError(V2TXLivePlayer player, int code, String msg, Bundle extraInfo) {
Log.e(TAG, "[Player] onError: player-" + player + " code-" + code + " msg-" + msg + " info-" + extraInfo);
}
@Override
public void onSnapshotComplete(V2TXLivePlayer v2TXLivePlayer, Bitmap bitmap) {
}
@Override
public void onVideoPlayStatusUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayStatus status, V2TXLiveDef.V2TXLiveStatusChangeReason reason, Bundle bundle) {
Log.i(TAG, "[Player] onVideoPlayStatusUpdate: player-" + player + ", status-" + status + ", reason-" + reason);
switch (status) {
case V2TXLivePlayStatusLoading:
//startLoadingAnimation();
break;
case V2TXLivePlayStatusPlaying:
//stopLoadingAnimation();
Bundle params = new Bundle();
params.putString(TXLiveConstants.EVT_DESCRIPTION, mContext.getResources().getString(R.string.liveplayer_warning_checkout_res_url));
//mLogInfoWindow.setLogText(null, params, LogInfoWindow.CHECK_RTMP_URL_OK);
break;
case V2TXLivePlayStatusStopped:
if (reason == V2TXLiveDef.V2TXLiveStatusChangeReason.V2TXLiveStatusChangeReasonRemoteOffline) {
stopPlay();
}
default:
break;
}
}
@Override
public void onAudioPlayStatusUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayStatus status, V2TXLiveDef.V2TXLiveStatusChangeReason reason, Bundle bundle) {
Log.i(TAG, "[Player] onAudioPlayStatusUpdate: player-" + player + ", status-" + status + ", reason-" + reason);
switch (status) {
case V2TXLivePlayStatusLoading:
//startLoadingAnimation();
break;
case V2TXLivePlayStatusPlaying:
//stopLoadingAnimation();
break;
case V2TXLivePlayStatusStopped:
if (reason == V2TXLiveDef.V2TXLiveStatusChangeReason.V2TXLiveStatusChangeReasonRemoteOffline) {
stopPlay();
}
default:
break;
}
}
@Override
public void onPlayoutVolumeUpdate(V2TXLivePlayer player, int volume) {
// Log.i(TAG, "onPlayoutVolumeUpdate: player-" + player + ", volume-" + volume);
}
@Override
public void onStatisticsUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayerStatistics statistics) {
Bundle netStatus = new Bundle();
netStatus.putInt(TXLiveConstants.NET_STATUS_VIDEO_WIDTH, statistics.width);
netStatus.putInt(TXLiveConstants.NET_STATUS_VIDEO_HEIGHT, statistics.height);
int appCpu = statistics.appCpu / 10;
int totalCpu = statistics.systemCpu / 10;
String strCpu = appCpu + "/" + totalCpu + "%";
netStatus.putCharSequence(TXLiveConstants.NET_STATUS_CPU_USAGE, strCpu);
netStatus.putInt(TXLiveConstants.NET_STATUS_NET_SPEED, statistics.videoBitrate + statistics.audioBitrate);
netStatus.putInt(TXLiveConstants.NET_STATUS_AUDIO_BITRATE, statistics.audioBitrate);
netStatus.putInt(TXLiveConstants.NET_STATUS_VIDEO_BITRATE, statistics.videoBitrate);
netStatus.putInt(TXLiveConstants.NET_STATUS_VIDEO_FPS, statistics.fps);
netStatus.putInt(TXLiveConstants.NET_STATUS_AUDIO_CACHE, 0);
netStatus.putInt(TXLiveConstants.NET_STATUS_VIDEO_CACHE, 0);
netStatus.putInt(TXLiveConstants.NET_STATUS_V_SUM_CACHE_SIZE, 0);
netStatus.putInt(TXLiveConstants.NET_STATUS_V_DEC_CACHE_SIZE, 0);
netStatus.putString(TXLiveConstants.NET_STATUS_AUDIO_INFO, "");
Log.d(TAG, "Current status, CPU:" + netStatus.getString(TXLiveConstants.NET_STATUS_CPU_USAGE) +
", RES:" + netStatus.getInt(TXLiveConstants.NET_STATUS_VIDEO_WIDTH) + "*" + netStatus.getInt(TXLiveConstants.NET_STATUS_VIDEO_HEIGHT) +
", SPD:" + netStatus.getInt(TXLiveConstants.NET_STATUS_NET_SPEED) + "Kbps" +
", FPS:" + netStatus.getInt(TXLiveConstants.NET_STATUS_VIDEO_FPS) +
", ARA:" + netStatus.getInt(TXLiveConstants.NET_STATUS_AUDIO_BITRATE) + "Kbps" +
", VRA:" + netStatus.getInt(TXLiveConstants.NET_STATUS_VIDEO_BITRATE) + "Kbps");
//mLogInfoWindow.setLogText(netStatus, null, 0);
}
}
private String getAndroidID() {
return Settings.Secure.getString( getContentResolver(), Settings.Secure.ANDROID_ID);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onGetMessage(MessageWrap message) {
// getBinding().tvMessage.setText(message.message);
Log.i(TAG, "收到消息: " + new String(message.message));
Toast.makeText(getApplicationContext(), "EventBus: " + new String(message.message), Toast.LENGTH_LONG).show();
String url = new String(message.message);
if (url.substring(url.length()-4).equals("m3u8")){
mLivePlayer.stopPlay();
mVideoView.setVisibility(View.GONE);
vv_view.setVisibility(View.VISIBLE);
vv_view.setVideoPath(url);
vv_view.start();
} else {
vv_view.stopPlayback();
vv_view.setVisibility(View.GONE);
mVideoView.setVisibility(View.VISIBLE);
mLivePlayer.startPlay(url);
}
}
}