0

当我使用 Android 的 MediaPlayer 类重播完成的视频时,我遇到了一些问题。

  1. 我查看了MediaClass Android SDK和状态图。据我所知,一旦视频完成播放,您应该能够调用 start() 并让它从头开始。
  2. 但是,当我重播 RSTP 文件时,这些是我得到的错误:

01-10 11:36:49.138: E/MediaPlayer(4821): 错误 (-2147483648, 0)
01-10 11:36:49.146: E/MediaPlayer(4821): 错误 (1, -1)
01-10 11 :36:49.154:E / MediaPlayer(4821):错误(-2147483648,0)
01-10 11:36:49.162:E / MediaPlayer(4821):尝试在没有有效媒体播放器的情况下调用getDuration
01-10 11:36: 49.162: E/MediaPlayer(4821): 错误 (-38, 0)
01-10 11:36:49.169: E/MediaPlayer(4821): 错误 (1,-1)
01-10 11:36:49.185: E/媒体播放器(4821):错误(-38,0)

3.最后一个问题发生在我重播网站上的 MPG4 时。再次点击播放后,视频不显示,并且搜索栏移动不同步。

下面是我的活动代码。

public class CustomMediaPlayer extends Activity implements MediaPlayerControl,
        OnCompletionListener, OnErrorListener, OnInfoListener,
        OnPreparedListener, OnSeekCompleteListener, OnVideoSizeChangedListener,
        SurfaceHolder.Callback, OnBufferingUpdateListener {




    // TODO Remove
    String TAG = "VAC";
    public static final String FILE_PATH = "rtsp://v4.cache2.c.youtube.com/CjYLENy73wIaLQkQ7cBxuSy88hMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJitofTlp8vjUAw=/0/0/0/video.3gp";

    //

    PowerManager pm;
    PowerManager.WakeLock wl;

    SpeechRecognizer sr;
    ImageView talkImageview;

    Toast toast;

    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;

    MediaPlayer mediaPlayer;
    MediaController controller;

    ProgressBar progressBar;

    Display currentDisplay;

    int videoWidth = 0;
    int videoHeight = 0;
    boolean readyToPlay = false;

    Handler handler;

    int bufferedPercentage;

    boolean finishedPlaying = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_player);

        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, new String());
        wl.acquire();

        preInitWidgets();
    }

    private void preInitWidgets() {
        toast = Toast.makeText(this, "", Toast.LENGTH_LONG);
        progressBar = (ProgressBar) findViewById(R.id.VideoPlayerProgressBar);
        talkImageview = (ImageView) findViewById(R.id.VideoPlayerTalkImageView);

        sr = SpeechRecognizer.createSpeechRecognizer(this);
        sr.setRecognitionListener(new listener());

        handler = new Handler();

        surfaceView = (SurfaceView) findViewById(R.id.VideoPlayerSurfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mediaPlayer = new MediaPlayer();
        mediaPlayerInit();
        currentDisplay = getWindowManager().getDefaultDisplay();
        controller = new MediaController(this);

    }

    void mediaPlayerInit() {

        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnInfoListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnSeekCompleteListener(this);
        mediaPlayer.setOnVideoSizeChangedListener(this);
        mediaPlayer.setOnBufferingUpdateListener(this);

        try {
            mediaPlayer.setDataSource(FILE_PATH);

            // mediaPlayer.setDataSource("http://www.blk-burn.com/sandbox/sj.mp4");
        } catch (Exception e) {
            toast.setText("Failed To Load Video");
            toast.show();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlayer.setDisplay(holder);

        try {
            mediaPlayer.prepareAsync();
        } catch (Exception e) {
            toast.setText("Failed to Prepare");
            toast.show();
        }

        progressBar.setVisibility(View.VISIBLE);

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onVideoSizeChanged(android.media.MediaPlayer mp, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSeekComplete(android.media.MediaPlayer mp) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int bufferedPercent) {
        this.bufferedPercentage = bufferedPercent;

    }

    @Override
    public void onPrepared(android.media.MediaPlayer mp) {
        videoWidth = mp.getVideoWidth();
        videoHeight = mp.getVideoHeight();

        if (videoWidth > currentDisplay.getWidth()
                || videoHeight > currentDisplay.getHeight()) {
            float heightRatio = (float) videoHeight
                    / (float) currentDisplay.getHeight();
            float widthRatio = (float) videoWidth
                    / (float) currentDisplay.getWidth();

            if (heightRatio > 1 || widthRatio > 1) {
                if (heightRatio > widthRatio) {
                    videoHeight = (int) Math.ceil((float) videoHeight
                            / (float) heightRatio);
                    videoWidth = (int) Math.ceil((float) videoWidth
                            / (float) heightRatio);
                } else {
                    videoHeight = (int) Math.ceil((float) videoHeight
                            / (float) widthRatio);
                    videoWidth = (int) Math.ceil((float) videoWidth
                            / (float) widthRatio);
                }
            }
        }

        surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth,
                videoHeight));

        surfaceView.setLayoutParams(new LinearLayout.LayoutParams(videoWidth,
                videoHeight));
        controller.setMediaPlayer(this);
        controller
                .setAnchorView(findViewById(R.id.VideoPlayerSurfaceViewLinearLayout));
        controller.setEnabled(true);
        controller.show();

        mp.start();

        // TODO - Get Catch Phrase

        talkImageview.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);

        // startListeningForVoice();

    }

    @Override
    public boolean onInfo(android.media.MediaPlayer mp, int what, int extra) {
        // TODO
        return false;
    }

    @Override
    public boolean onError(android.media.MediaPlayer mp, int what, int extra) {
        if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
            toast.setText("Media Server Died");
            toast.show();
        } else if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
            toast.setText("Unknown Error");
            toast.show();
        }
        return false;
    }

    @Override
    public void onCompletion(android.media.MediaPlayer mp) {



    }

    public boolean canPause() {
        return true;
    }

    public boolean canSeekBackward() {
        return true;
    }

    public boolean canSeekForward() {
        return true;
    }

    public int getBufferPercentage() {
        return bufferedPercentage;

    }

    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    public int getDuration() {
        return mediaPlayer.getDuration();
    }

    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    public void pause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    public void seekTo(int pos) {
        mediaPlayer.seekTo(pos);
    }

    public void start() {
        mediaPlayer.start();

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (controller.isShowing()) {
                controller.hide();
            } else {
                controller.show();
            }
        }
        return false;
    }

    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.release();
        wl.release();

    }





}
4

1 回答 1

1

您的 setDataSource 是这里的问题。Android 不支持 rtsp 流。我尝试了 rtsp 几次都没有运气。如果您尝试从 http 流式传输,如果支持视频格式,它肯定可以工作。看看这里的 Vitamio 库http://vitamio.org/。它支持rtsp流以及android不支持的许多其他音频和视频格式。

于 2013-02-20T09:22:05.810 回答