赞
踩
因为公司项目中的一个功能是直播,虽然我之前从来没有接触过这玩意但是也不得不硬着头皮上了,坑爹的是直播的协议流还不是http而是RTMP和RTSP,于是乎这一周一直在网上看各种教程从SDL+ffmpeg到ijkplayer鬼知道我到底经历了些什么,直到遇到了GSYVideoPlayer。
我就不对这个库多做介绍了总之就是很好很强大,对于我们这些小白来说很轻松的就能实现我们想要的功能以下是官网地址:https://github.com/CarGuo/GSYVideoPlayer
我在此只放如何直播rtsp和rtmp的相关代码其他的需要功能大家可以去官网看文档,文档写的很详细但是要耐着性子看下去。
实现步骤:
implementation 'com.shuyu:gsyVideoPlayer-java:6.0.1'
//是否需要ExoPlayer模式
implementation 'com.shuyu:GSYVideoPlayer-exo2:6.0.1'
//更多ijk的编码支持
implementation 'com.shuyu:gsyVideoPlayer-ex_so:6.0.1'
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
android:id="@+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer>
</android.support.constraint.ConstraintLayout>
package com.example.dadao.gsyvideoplayer; import android.content.pm.ActivityInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import com.shuyu.gsyvideoplayer.GSYVideoManager; import com.shuyu.gsyvideoplayer.model.VideoOptionModel; import com.shuyu.gsyvideoplayer.player.IjkPlayerManager; import com.shuyu.gsyvideoplayer.player.PlayerFactory; import com.shuyu.gsyvideoplayer.utils.OrientationUtils; import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer; import java.util.ArrayList; import java.util.List; import tv.danmaku.ijk.media.player.IjkMediaPlayer; public class MainActivity extends AppCompatActivity { public StandardGSYVideoPlayer videoPlayer; OrientationUtils orientationUtils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //PlayerFactory.setPlayManager(IjkPlayerManager.class); init(); } private void init() { videoPlayer = (StandardGSYVideoPlayer)findViewById(R.id.videoPlayer); /**此中内容:优化加载速度,降低延迟*/ VideoOptionModel videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "rtsp_transport", "tcp"); List<VideoOptionModel> list = new ArrayList<>(); list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "rtsp_flags", "prefer_tcp"); list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "allowed_media_types", "video"); //根据媒体类型来配置 list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "timeout", 20000); list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "buffer_size", 1316); list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "infbuf", 1); // 无限读 list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzemaxduration", 100); list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 10240); list.add(videoOptionModel); videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1); list.add(videoOptionModel); // 关闭播放器缓冲,这个必须关闭,否则会出现播放一段时间后,一直卡主,控制台打印 FFP_MSG_BUFFERING_START videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "packet-buffering", 0); list.add(videoOptionModel); GSYVideoManager.instance().setOptionModelList(list); String source1 = "rtsp://192.168.1.101/11"; videoPlayer.setUp(source1, true, ""); videoPlayer.getTitleTextView().setVisibility(View.GONE); videoPlayer.startPlayLogic(); } @Override protected void onPause() { super.onPause(); videoPlayer.onVideoPause(); } @Override protected void onResume() { super.onResume(); videoPlayer.onVideoResume(); } @Override protected void onDestroy() { super.onDestroy(); GSYVideoManager.releaseAllVideos(); if (orientationUtils != null) orientationUtils.releaseListener(); } @Override public void onBackPressed() { //先返回正常状态 if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { videoPlayer.getFullscreenButton().performClick(); return; } //释放所有 videoPlayer.setVideoAllCallBack(null); super.onBackPressed(); } }
所有步骤结束,不需要添加什么so库更不需要自己去编译so库!
测试结果:
设备:荣耀play
结束
参考链接:
基于IJKPlayer(兼容系统MediaPlayer与EXOPlayer2),实现了多功能的视频播放器
九个完美的Android播放器开源项目【精华】
IjkPlaye开源库
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。