android rtsp 直播 - "Can't play this video"
android rtsp live stream - "Can't play this video"
public class MainActivity extends Activity
{
VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv = (VideoView)findViewById(R.id.videoView);
Uri uri;
uri=Uri.parse("rtsp://user:pass@192.168.x.x:554/cam/realmonitor?channel=1&subtype=1");
vv.setVideoURI(uri);
vv.start();
}
}
我正在使用上面的代码,尝试播放我的 IP 直播摄像机流。使用的 URI 在 VLC 播放器中使用时有效。我的清单中也设置了 Internet 权限。但是当我 运行 我的模拟器或实时设备上的应用程序时,我收到 "Can't play this video" 错误。
logcat中弹出的错误是:
08-31 09:21:15.602 2487-2487/package D/MediaPlayer: Couldn't open file on client side, trying server side
08-31 09:21:15.612 2487-2503/package W/MediaPlayer: info/warning (701, 0)
08-31 09:21:15.666 2487-2502/package E/MediaPlayer: error (1, -2147483648)
08-31 09:21:15.725 2487-2487/package E/MediaPlayer: Error (1,-2147483648)
08-31 09:21:15.725 2487-2487/package D/VideoView: Error: 1,-2147483648
我也试过用媒体播放器设置它:
public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback
{
final static String USERNAME = "user";
final static String PASSWORD = "pass";
final static String RTSP_URL = "rtsp://192.168.x.x:554/cam/realmonitor?channel=1&subtype=00";
private MediaPlayer _mediaPlayer;
private SurfaceHolder _surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setBackgroundDrawableResource(android.R.color.black);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
_surfaceHolder = surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setFixedSize(320, 240);
}
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
@Override
public void surfaceCreated(SurfaceHolder sh) {
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setDisplay(_surfaceHolder);
Context context = getApplicationContext();
Map<String, String> headers = getRtspHeaders();
Uri source = Uri.parse(RTSP_URL);
try {
_mediaPlayer.setDataSource(context, source, headers);
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepareAsync();
}
catch (Exception e) {}
}
@Override
public void surfaceDestroyed(SurfaceHolder sh) {
_mediaPlayer.release();
}
private Map<String, String> getRtspHeaders() {
Map<String, String> headers = new HashMap<String, String>();
String basicAuthValue = getBasicAuthValue(USERNAME, PASSWORD);
headers.put("Authorization", basicAuthValue);
return headers;
}
private String getBasicAuthValue(String usr, String pwd) {
String credentials = usr + ":" + pwd;
int flags = Base64.URL_SAFE | Base64.NO_WRAP;
byte[] bytes = credentials.getBytes();
return "Basic " + Base64.encodeToString(bytes, flags);
}
@Override
public void onPrepared(MediaPlayer mp) {
_mediaPlayer.start();
}
}
以上代码取自here
但其中 none 有效。
我也试过 Vitamio 库但没有结果。我所做的一切似乎都没有用。
我是不是遗漏了什么小细节?我假设 URI 很好,因为它在 VLC 播放器中工作得很好。在某些时候,模拟器也给出了 "java.io.FileNotFoundException: No content provider",但不再是。
有一次,我在想可能是身份验证出了问题,所以我使用 VLC 流式传输 IP 摄像机,没有 user/pass,并尝试连接,但这也没有用,所以我假设它已成功验证。
我也试过将 url 作为 "rtsp://user:pass@192.168.x.x:554/" 传递,这在 VLC 播放器中也有效。但不在我的应用程序中。
我已经尝试了我能找到的一切,但没有成功。如果有人能够提供帮助,将不胜感激!
编辑1:VideoView已经能够成功播放"rtsp://mpv.cdn3.bigCDN.com:554/bigCDN/definst/mp4:bigbuckbunnyiphone_400.mp4"等预录片段
与vitamio和mediaplayer相同。
您正在尝试播放 VideoView
中的视频。很遗憾 VideoView 不支持尝试使用 RTSP 协议播放的视频。
您可以为 VLC Player 使用 LibVLC android 库 VLC Player. It works with RTSP url in rtsp://user:pass@192.168.x.x:554 format. Here is a simple and easy to use example MyLibVlc
public class MainActivity extends Activity
{
VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv = (VideoView)findViewById(R.id.videoView);
Uri uri;
uri=Uri.parse("rtsp://user:pass@192.168.x.x:554/cam/realmonitor?channel=1&subtype=1");
vv.setVideoURI(uri);
vv.start();
}
}
我正在使用上面的代码,尝试播放我的 IP 直播摄像机流。使用的 URI 在 VLC 播放器中使用时有效。我的清单中也设置了 Internet 权限。但是当我 运行 我的模拟器或实时设备上的应用程序时,我收到 "Can't play this video" 错误。
logcat中弹出的错误是:
08-31 09:21:15.602 2487-2487/package D/MediaPlayer: Couldn't open file on client side, trying server side
08-31 09:21:15.612 2487-2503/package W/MediaPlayer: info/warning (701, 0)
08-31 09:21:15.666 2487-2502/package E/MediaPlayer: error (1, -2147483648)
08-31 09:21:15.725 2487-2487/package E/MediaPlayer: Error (1,-2147483648)
08-31 09:21:15.725 2487-2487/package D/VideoView: Error: 1,-2147483648
我也试过用媒体播放器设置它:
public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback
{
final static String USERNAME = "user";
final static String PASSWORD = "pass";
final static String RTSP_URL = "rtsp://192.168.x.x:554/cam/realmonitor?channel=1&subtype=00";
private MediaPlayer _mediaPlayer;
private SurfaceHolder _surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setBackgroundDrawableResource(android.R.color.black);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
_surfaceHolder = surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setFixedSize(320, 240);
}
@Override
public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
@Override
public void surfaceCreated(SurfaceHolder sh) {
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setDisplay(_surfaceHolder);
Context context = getApplicationContext();
Map<String, String> headers = getRtspHeaders();
Uri source = Uri.parse(RTSP_URL);
try {
_mediaPlayer.setDataSource(context, source, headers);
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepareAsync();
}
catch (Exception e) {}
}
@Override
public void surfaceDestroyed(SurfaceHolder sh) {
_mediaPlayer.release();
}
private Map<String, String> getRtspHeaders() {
Map<String, String> headers = new HashMap<String, String>();
String basicAuthValue = getBasicAuthValue(USERNAME, PASSWORD);
headers.put("Authorization", basicAuthValue);
return headers;
}
private String getBasicAuthValue(String usr, String pwd) {
String credentials = usr + ":" + pwd;
int flags = Base64.URL_SAFE | Base64.NO_WRAP;
byte[] bytes = credentials.getBytes();
return "Basic " + Base64.encodeToString(bytes, flags);
}
@Override
public void onPrepared(MediaPlayer mp) {
_mediaPlayer.start();
}
}
以上代码取自here 但其中 none 有效。 我也试过 Vitamio 库但没有结果。我所做的一切似乎都没有用。
我是不是遗漏了什么小细节?我假设 URI 很好,因为它在 VLC 播放器中工作得很好。在某些时候,模拟器也给出了 "java.io.FileNotFoundException: No content provider",但不再是。
有一次,我在想可能是身份验证出了问题,所以我使用 VLC 流式传输 IP 摄像机,没有 user/pass,并尝试连接,但这也没有用,所以我假设它已成功验证。
我也试过将 url 作为 "rtsp://user:pass@192.168.x.x:554/" 传递,这在 VLC 播放器中也有效。但不在我的应用程序中。
我已经尝试了我能找到的一切,但没有成功。如果有人能够提供帮助,将不胜感激!
编辑1:VideoView已经能够成功播放"rtsp://mpv.cdn3.bigCDN.com:554/bigCDN/definst/mp4:bigbuckbunnyiphone_400.mp4"等预录片段 与vitamio和mediaplayer相同。
您正在尝试播放 VideoView
中的视频。很遗憾 VideoView 不支持尝试使用 RTSP 协议播放的视频。
您可以为 VLC Player 使用 LibVLC android 库 VLC Player. It works with RTSP url in rtsp://user:pass@192.168.x.x:554 format. Here is a simple and easy to use example MyLibVlc