Android MediaPlayer重播问题

时间:2013-01-10 16:41:39

标签: android video android-mediaplayer rtsp

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

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

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

1 个答案:

答案 0 :(得分:1)

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