user3566211
user3566211

Reputation: 87

Unable to stream RTSP video in Android MediaPlayer

I'm trying to stream a video from an RTSP server. I want to run the video inside my app. I have tried three different approaches and only one of them seems to work:

  1. Using VideoView (does not work).
  2. Using native player (works).
  3. Using MediaPlayer (does not work).

I want to display the video on MediaPlayer but unable to do it.

Here is the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    /* 1. Using VideoView, error: "Cannot play video"
    VideoView vidView = (VideoView) findViewById(R.id.myVideo);

    String vidAdress = "rtsp://10.42.0.91:5554/camera";
    Uri vidUri = Uri.parse(vidAdress);

    vidView.setVideoURI(vidUri);

    vidView.start();

    */

    //Using native player (implicit intent), works.
    String mediaURL = "rtsp://10.42.0.91:5554/camera";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mediaURL));
    startActivity(intent);

    /* Using MediaPlayer (doesn't work). Error: "Cannot create MediaPlayer"
and setDataSource.
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try
    {
        mediaPlayer.setDataSource("rtsp://10.42.0.91:5554/camera");
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
    */

http:10.42.0.91:8080/playlist.m3u looks like this:

#EXTM3U

#PLAYLIST:RTSP Camera Server #EXTINF:-1, Active camera rtsp://10.42.0.91:5554/camera #EXTINF:319, 3/3/16 PM 3:08: 35 MB rtsp://10.42.0.91:5554/record160303_1500 #EXTINF:1047, 3/3/16 PM 2:15: 106.4 MB rtsp://10.42.0.91:5554/record160303_1400 #EXTINF:1364, All records 141.4 MB rtsp://10.42.0.91:5554/record #EXTINF:-1, Back camera rtsp://10.42.0.91:5554/back #EXTINF:-1, Front camera rtsp://10.42.0.91:5554/front

Upvotes: 1

Views: 2667

Answers (1)

Matthijs
Matthijs

Reputation: 71

I've just finished a basic application that can display RTSP H.264(Even high profile) in the Android MediaPlayer. I was experimenting and researching how I should implement it, when I came across this video. It is quite easy to implement and source code is also provided. I use Android version 5.1.1, it doesn't seem to work on 4.4 and below. My goal is to make it work on 4.1 and up, so I'll edit or comment when I got that working.

I hope this explains everything you need. Just tell me if it still does not work and I'll try to help you out.

Upvotes: 0

Related Questions