android 播放rtsp流的三种方式
程序员文章站
2022-07-03 14:53:29
使用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