Search code examples
androidvideo-streamingaudio-streamingrtsp

play rtsp stream on Android


I need to play android rtsp stream for both video audio. But no matter what I do they just don't start. The rstp stream for video is in .mp4 format and for audio it is in .mp3. The stream works fine when played in VLC and it shows the resolution which is 320X240. But here it just give the error which I have posted at the bottom. Also please tell how to stream an mp3 as there is no tutorial in the API Demos in android-sdk. The video works fine with Vitamio Library. But I don't know how to customize it.

mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

//onPrepared(...) Function
public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
} 

//startVideoPlayback() Function 
    private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setFixedSize(mVideoWidth, mVideoHeight);
    mMediaPlayer.start();
}

ERROR: 01-05 21:10:59.640: ERROR/MediaPlayerDemo(15989): invalid video width(0) or height(0)


Solution

  • Change the onVideoSizeChanged(...) Function into:-

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            Log.d("onVideoSizeChanged: (%dx%d)", width, height);
            mVideoWidth = mp.getVideoWidth();
            mVideoHeight = mp.getVideoHeight();
            mVideoAspectRatio = mp.getVideoAspectRatio();
            if (mVideoWidth != 0 && mVideoHeight != 0)
                setVideoLayout(mVideoLayout, mAspectRatio);
        }