Android rtsp流视频 播放

之前用的是 vitamio 为了播放flv格式的流视频,但是却播不了rtsp格式的流视频,于是上网扒了半天,各有各的方法,试完发现都不很行,还好再gitbub上找到了一个专门做rtsp的开源产品

附上链接 https://github.com/NodeMedia/NodeMediaClient-Android

附上demo地址 https://github.com/NodeMedia/QLive-Android

文档地址 https://github.com/NodeMedia/NodeMediaClient-Android/blob/2.x/docs/NodePlayer_API_CN.md

流程很简单,使用很方便

记录一下 :

studio 导包

项目gradle中

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

app gradle 中

dependencies {
    compile 'com.github.NodeMedia:NodeMediaClient-Android:2.7.2'
}

xml文件附上:

<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=".Main2Activity">

    <cn.nodemedia.NodePlayerView
        android:id="@+id/nodePlayer"
        android:layout_width="match_parent"
        android:layout_height="250dp" />

</LinearLayout>

主要代码:

public class Main2Activity extends AppCompatActivity {

    private NodePlayerView nodePlayerView;
    private NodePlayer nodePlayer;
   
    String url2 = "换成你的url";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        nodePlayerView =findViewById(R.id.nodePlayer);
        //设置渲染器类型
        nodePlayerView.setRenderType(NodePlayerView.RenderType.SURFACEVIEW); 
        //设置视频画面缩放模式
        nodePlayerView.setUIViewContentMode(NodePlayerView.UIViewContentMode.ScaleToFill);

        nodePlayer =new NodePlayer(this);
        //设置播放视图
        nodePlayer.setPlayerView(nodePlayerView);
        //设置RTSP流使用的传输协议,支持的模式有: 
        nodePlayer.setRtspTransport(NodePlayer.RTSP_TRANSPORT_TCP);
        nodePlayer.setInputUrl(url2);
        //设置视频是否开启
        nodePlayer.setVideoEnable(true);
        nodePlayer.start();
    }
}

 注意:setRtspTransport 这个方法,每个人的传输协议不同 ,根据自己情况修改,不设置可能不会显示

 其他 根据个人情况进行设置,文档,代码都已经附上链接,好好阅读,定能成功

猜你喜欢

转载自blog.csdn.net/jiexiao4151/article/details/106197204