Android Studio 用NodeMedia播放RTSP视频

项目需要用Android 播放RTSP视频,用Android Studio自带的videoView 发现延时很大,达10秒左右,用NodeMedia播放RTSP视频,可设置缓存时间,将时延降至毫秒。

1、在settings.gradle中在下图位置加入

maven { url 'https://jitpack.io' }

 2、菜单File->Project structrue,在弹出的对计算机框中依次点击Dependencies->All Dependencies->"+"号->1 Library Dependency.

 在弹出的对话框中输入

com.github.nodemedia:nodemediaclient-android:2.9.20

然后依次点击下图的1、2、3

3、xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <cn.nodemedia.NodePlayerView
        android:id="@+id/play_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="MissingClass" />
</androidx.constraintlayout.widget.ConstraintLayout>

4、代码

public class MainActivity extends AppCompatActivity {
    private NodePlayerView nodePlayerView;
    private NodePlayer nodePlayer;
    private String address = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";
//可修改成自己的地址
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nodePlayerView = findViewById(R.id.play_surface);
        //设置渲染器类型
        nodePlayerView.setRenderType(NodePlayerView.RenderType.SURFACEVIEW);
        //设置视频画面缩放模式
        nodePlayerView.setUIViewContentMode(NodePlayerView.UIViewContentMode.ScaleAspectFit);

        nodePlayer = new NodePlayer(this);
        //设置播放视图
        nodePlayer.setPlayerView(nodePlayerView);
        //设置RTSP流使用的传输协议,支持的模式有:
        nodePlayer.setRtspTransport(NodePlayer.RTSP_TRANSPORT_TCP);//设置传输
        nodePlayer.setInputUrl(address);
        nodePlayer.setVideoEnable(true);//设置视频启用
        nodePlayer.setBufferTime(100);//设置缓冲时间
        nodePlayer.setMaxBufferTime(200);//设置最大缓冲时间
        nodePlayer.start();
    }
}

5、权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

猜你喜欢

转载自blog.csdn.net/jichengw/article/details/128218331
今日推荐