在Android上播放rtsp流

时间:2013-01-05 16:20:14

标签: android video-streaming audio-streaming rtsp

我需要为两个视频音频播放android rtsp流。但无论我做什么,他们都不会开始。视频的rstp流采用.mp4格式,音频采用.mp3格式。在VLC中播放时,流工作正常,并显示320X240的分辨率。但是这里只是给出了我在底部发布的错误。另请告诉我如何流式播放mp3,因为android-sdk中的API演示中没有教程。该视频与Vitamio Library完美搭配。但我不知道如何定制它。

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();
}

错误: 01-05 21:10:59.640:ERROR / MediaPlayerDemo(15989):视频宽度(0)或高度(0)无效

2 个答案:

答案 0 :(得分:1)

将onVideoSizeChanged(...)函数更改为: -

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);
    }

答案 1 :(得分:0)

public void onVideoSizeChanged(MediaPlayer mp, int width, int height)
{
    if(width == 0 || height == 0) {
        return;
    }

    this.width = width;
    this.height = height;
    aspectRatio = width/height;

    startVideoPlayback();
}

我建议你这样做。

在onPrepared方法中,将startVideoPlayback()替换为mediaPlayer.start()