android 播放rtsp流的三种方式

android物联 专栏收录该内容
7 篇文章 0 订阅
  • 使用VideoView
 <VideoView
            android:id="@+id/surface_view"
            android:layout_width="250dp"
            android:layout_height="250dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
 	private String uri = "rtsp://192.168.1.10:554/user=admin_password=nTBCS19C_channel=1_stream=0.sdp?real_stream";

    @Override
    protected void attachedToWindow() {
        binding.videoSurface.setVideoURI(Uri.parse(uri));
        binding.videoSurface.setOnPreparedListener(mp -> {
            binding.videoSurface.requestFocus();
           binding.videoSurface.start();
       });
       
    }
    
   @Override
    protected void onDestroyFrame() {
        super.onDestroyFrame();
        binding.videoSurface.suspend(); 
    }

  • 使用SurfaceView+MediaPlayer
 <SurfaceView
            android:id="@+id/surface_view"
            android:layout_width="250dp"
            android:layout_height="250dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
 	private String uri = "rtsp://192.168.1.10:554/user=admin_password=nTBCS19C_channel=1_stream=0.sdp?real_stream";

    @Override
    protected void onDestroyFrame() {
        super.onDestroyFrame();
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();//停止音频的播放
        }
        mediaPlayer.release();//释放资源
    }

    private MediaPlayer mediaPlayer;

    @Override
    protected void attachedToWindow() {
        mediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(uri));
        binding.surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mediaPlayer.setDisplay(holder);
                //播放
                mediaPlayer.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }

  • 使用开源库NodeMediaClient-Android

附上链接 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

项目gradle中


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

dependencies {
    compile 'com.github.NodeMedia:NodeMediaClient-Android:2.8.4'
}
<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 MainActivity extends AppCompatActivity {

    private NodePlayerView nodePlayerView;
    private NodePlayer nodePlayer;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nodePlayer.stop();
        nodePlayer.release();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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("rtsp://192.168.1.10:554/user=admin_password=nTBCS19C_channel=1_stream=0.sdp?real_stream");
        //设置视频是否开启
        nodePlayer.setVideoEnable(true);
        nodePlayer.setBufferTime(0);
        nodePlayer.setMaxBufferTime(0);
        nodePlayer.start();
    }

}

  • 1
    点赞
  • 5
    评论
  • 11
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值