Android rtsp流视频播放失败(传输摄像头画面)
在PC上的虚拟机Ubuntu系统中使用Gstreamer+rtsp推流,在Windows PC上直接安装VLC Media Player输入rtsp流地址可以成功接收rtsp流,但是在PC上Andriod Studio开发的客户端(使用数据线连接发布运行到手机上)中无法接收,甚至都ping不到对应的rtsp://182.168.xx.xx:端口/test,具体代码和报错如下:
代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.videolan.nodeMediatest0">
<uses-feature android:name="android.hardware.camera.any" />
<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.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="org.videolan.nodeMediatest0.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package org.videolan.nodeMediatest0;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import cn.nodemedia.NodePlayer;
import cn.nodemedia.NodePlayerView;
public class MainActivity extends AppCompatActivity {
NodePlayerView nodePlayerView;
NodePlayer nodePlayer;
String address = "rtsp://192.168.1.106:8554/test";
@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(address);
// 检测网络是否连通
boolean isConnect = checkNetwork();
System.out.println("================ "+ isConnect); //输出为false,但是在PC端直接用VLC Media Player是可以访问的
//设置视频是否开启
nodePlayer.setVideoEnable(true);
nodePlayer.start();
}
//检测是网络是否连通
public boolean checkNetwork(){
Runtime runtime = Runtime.getRuntime(); // 获取Runtime实例
try {
System.out.println("正在检测网络是否连通......");
Process ipProcess = runtime.exec("ping -c 5 rtsp://192.168.1.106:8554/test"); //-c 指定ping的次数
int res = ipProcess.waitFor();// 获取检测结果
return (res == 0); // 返回检测结果,res为0,则网络正常,否则网络异常
}catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return false;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<cn.nodemedia.NodePlayerView
android:id="@+id/nodePlayer"
android:layout_width="match_parent"
android:layout_height="250dp" />
</LinearLayout>
报错:
首先你要保证 ubuntu系统使用的网络和手机网络要在同一个网段,这样才能保证内网访问