Android:播放Rtsp视频流的两种方式

一.SurfaceView + Mediaplayer

XML 中添加SurfaceView:

XML 复制代码
<SurfaceView
    android:id="@+id/surface_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Activity代码:

java 复制代码
package com.android.rtsp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.android.rtsp.R;

public class RtspActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {

    private final String TAG = this.getClass().getSimpleName();

    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rtsp);

        surfaceView = findViewById(R.id.surface_view);
        SurfaceHolder holder = surfaceView.getHolder();
        holder.addCallback(sfvCallBack);
    }

    private SurfaceHolder.Callback sfvCallBack = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            playVideo();
        }

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

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        }
    };

    private void playVideo() {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            try {
                //公共Rtsp,后附多个目前可用的公共Rtsp
                mediaPlayer.setDataSource("rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream");
                mediaPlayer.setDisplay(surfaceView.getHolder());
                mediaPlayer.setOnPreparedListener(this);
                mediaPlayer.prepareAsync();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "Error loading video", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

二.VideoView

源码可以看到,VideoViewSurfaceView 的封装,并且实现了MediaPlayerControl 的方法

所以本质还是SurfaceView+MediaPlayer

XML 添加VideoView:

XML 复制代码
<VideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Activity代码:

java 复制代码
package com.android.rtsp;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.VideoView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.android.rtsp.R;

public class RtspActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getSimpleName();

    private VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rtsp);

        mVideoView = findViewById(R.id.video_view);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mVideoView != null) {
            videoConfig();
        }
    }

    private void videoConfig() {
        //公共Rtsp,后附多个目前可用的公共Rtsp
        mVideoView.setVideoURI(Uri.parse("rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream"));

        //本质还是SurfaceView+MediaPlayer
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.i(TAG, "onPrepared ... ........   ");
                mVideoView.requestFocus();
                mVideoView.start();
            }
        });

        //简化写法
        /*mVideoView.setOnPreparedListener(mp -> {
            mVideoView.requestFocus();
            mVideoView.start();
        });*/

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mVideoView.suspend();
    }
}

三.其他方式

网上也有很多自定义封装的viewplayer,也能方便快捷的实现Rtsp视频流的播放

可自行查阅

四.公共rtsp地址

目前暂时可用的Rtsp地址:

rtsp、rtmp测试地址_rtsp测试地址-CSDN博客

2025 rtsp测试拉流地址 - 简书

相关推荐
大熊的瓜地21 小时前
Android automotive 框架
android·android car
私人珍藏库21 小时前
[Android] Alarm Clock Pro 11.1.0一款经典简约个性的时钟
android·时钟
lzptouch1 天前
数据预处理(音频/图像/视频/文字)及多模态统一大模型输入方案
人工智能·音视频
消失的旧时光-19431 天前
ScheduledExecutorService
android·java·开发语言
小糖学代码1 天前
MySQL:14.mysql connect
android·数据库·mysql·adb
casdfxx1 天前
捡到h3开发板,做了个视频小车(二),御游追风plus做遥控器
音视频
给大佬递杯卡布奇诺1 天前
FFmpeg 基本API avcodec_send_packet函数内部调用流程分析
c++·ffmpeg·音视频
怪兽20141 天前
请谈谈什么是同步屏障?
android·面试
帅锅锅0071 天前
SeLinux 全面详解
android·linux
只想搞钱的肥仔1 天前
Android thermal (5)_cooling device(下)
android