android 播放rtsp流的三种方式
程序员文章站
2022-04-06 08:09:42
使用VideoView
- 使用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();
}
}
本文地址:https://blog.csdn.net/huangxiaoguo1/article/details/109628952
推荐阅读
-
Android实现音乐播放进度条传递信息的两种方式(在service和activity中)
-
Android自定义view的三种方式解析
-
基于VLC的Unity视频播放器(支持本地视频和rtmp、rtsp等视频流)
-
Android设置Activity全屏的三种方式
-
【Android】播放音频的几种方式介绍
-
Web下无插件播放rtsp视频流的方案及各家优秀内容资源整理
-
Android自定义字体样式Typeface的三种技术方案:Java代码的setTypeface和xml的fontFamily配置方式
-
srs ffmpeg拉取rtsp流,推送rtmp流到srs,vlc播放花屏、srs网页播放器播放跳秒/跳帧的问题。
-
视频流媒体播放器EasyPlayer-RTSP-Android 如何随意切换播放视频流?
-
RTSP播放器网页web无插件直播流媒体音视频播放器EasyPlayer-RTSP-Android解码获取视频帧的方法