视频播放器不会播放,只有空白屏幕

时间:2013-06-26 11:34:29

标签: android video stream video-streaming media-player

Hy,我有一个视频播放器的代码,现在我在互联网上找了这个错误,在StackOverflow上,有人说这个错误,是因为路径不正确。但我尝试了多个链接,似乎没有工作。 这是我的应用活动代码:

 public class VideoPlayerActivity extends Activity implements Callback, OnBufferingUpdateListener, OnPreparedListener, OnCompletionListener, OnVideoSizeChangedListener, OnErrorListener, OnSeekCompleteListener {

MediaPlayer mMediaPlayer;
Button startButton;
Button stopButton;
SurfaceView mPreview;
private SurfaceHolder holder;
public String TAG = "VideoPlayerActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_player_layout);
    startButton = (Button) findViewById(R.id.start);
    stopButton = (Button) findViewById(R.id.stop);
    mMediaPlayer = new MediaPlayer();
    startButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mMediaPlayer != null) {
                if (mMediaPlayer.isPlaying()) {
                    Log.d(TAG, "pause");
                    mMediaPlayer.pause();
                    startButton.setText("Start");
                } else {
                    Log.d(TAG, "Start");
                    setVideoPlayback();
                }
            }
        }
    });

}

@Override
protected void onDestroy() {
    Log.d(TAG, "Ondestroy");
    releaseMediaPlayer();
    super.onDestroy();
}

private void releaseMediaPlayer() {
    Log.d(TAG, "releasemediaplayer");
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

private void startVideoPlayback() {
    Log.d(TAG, "StartVideoPlayback");
    if (holder.getSurface() == null) {
        Log.d(TAG, "holder sufrace null");
        setSurfaceHolder();
    }
    if (mMediaPlayer.isPlaying()) {
        mMediaPlayer.pause();
    } else {
        mMediaPlayer.start();
        startButton.setText("Pause");
    }

}

private void playVideo(Uri path_to_play) {
    Log.d(TAG, "playvideo");
    if ((path_to_play == null) || path_to_play.equals("")) {
        Log.d(TAG, "pathtoplay is null");
        return;
    }
    releaseMediaPlayer();
    mMediaPlayer = new MediaPlayer();
    // mMediaPlayer = MediaPlayer.create(this, path_to_play);
    try {
        setSurfaceHolder();

        try {
            Log.d(TAG, "pathtoplay is " + path_to_play);
            mMediaPlayer.setDataSource(path_to_play.toString());
            if ((holder != null) && (holder.getClass() != null) && holder.getSurface().isValid()) {
                Log.d(TAG, "set holder to mediaplayer");
                mMediaPlayer.setDisplay(holder);
            } else {
                setSurfaceHolder();
                Log.d(TAG, "setsurface holder from play video");
            }
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.setOnSeekCompleteListener(this);
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            Log.e(TAG, "playvideo exception:" + e);
        }
        try {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (NullPointerException e) {
            Log.e(TAG, "playvideo exception2:" + e);
        }

    } catch (Exception e) {
        Log.e(TAG, "playvideo exception3:" + e);
    }

}

@Override
public void onSeekComplete(MediaPlayer arg0) {
    Log.d(TAG, "@onSeekComplete");

    try {
        if (holder.getSurface().isValid()) {
            Log.d("error", "@onSeekComplete is valid");
            if (mMediaPlayer != null) {
                mMediaPlayer.setDisplay(holder);
            }
        } else {
            Log.d(TAG, "@onSeekComplete else");
            setSurfaceHolder();
            if (mMediaPlayer != null) {
                mMediaPlayer.setDisplay(holder);
            }
        }
        mMediaPlayer.start();

    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    }
}

@SuppressLint("NewApi")
private void setSurfaceHolder() {
    Log.d(TAG, "set surface holder");
    mPreview = (SurfaceView) findViewById(R.id.surface);
    mPreview.setVisibility(View.INVISIBLE);
    mPreview.setFitsSystemWindows(true);
    setDisplayForHolder();
}

public void setDisplayForHolder() {
    Log.d(TAG, "set displat for surface holder");
    if (mPreview == null) {
        Log.d(TAG, "mpreview null");
        return;
    } else if (mPreview.getHolder() == null) {
        Log.d(TAG, "mpreview.getholder = null");
        return;
    }
    mPreview.getHolder().setFixedSize(720, 480);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mPreview.setVisibility(View.VISIBLE);

}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.e(TAG, "MediaPlayer.onError() WHAT: " + what + "EXTRA: " + extra);
    if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
        mMediaPlayer.release();
        mMediaPlayer = null;
        Log.e(TAG, "Media Error, Server Died " + extra);
    } else if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
        Log.e(TAG, "Media Error, Error Unknown " + extra);
    }
    return false;
}

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.d(TAG, "onvideoChanged");
    if ((width == 0) || (height == 0)) {
        mp.release();
        setVideoPlayback();
        startVideoPlayback();
        return;
    }
}

@Override
public void onCompletion(MediaPlayer arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPrepared(MediaPlayer arg0) {
    Log.d(TAG, "onprepared");
    startVideoPlayback();
}

@Override
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    Log.d(TAG, "surface changed");
    Handler han = new Handler();
    han.postDelayed(new Runnable() {

        @Override
        public void run() {
            setVideoPlayback();
        }
    }, 1000);

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

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

}

private void setVideoPlayback() {
    Log.d(TAG, "set video playback");
    releaseMediaPlayer();
    String path = "http://www.youtube.com/watch?v=U4dK9yodrsk";
    Uri url = Uri.parse(path);
    playVideo(url);
    startButton.setText("Pause");
}

}

现在Logcat说了以下内容:

  06-26 14:31:45.528: D/VideoPlayerActivity(15678): Start
  06-26 14:31:45.528: D/VideoPlayerActivity(15678): set video playback
  06-26 14:31:45.528: D/VideoPlayerActivity(15678): releasemediaplayer
  06-26 14:31:45.528: D/MediaPlayer(15678): release() in
  06-26 14:31:45.538: D/MediaPlayer(15678): release() out
  06-26 14:31:45.538: D/VideoPlayerActivity(15678): playvideo
  06-26 14:31:45.538: D/VideoPlayerActivity(15678): releasemediaplayer
  06-26 14:31:45.538: D/VideoPlayerActivity(15678): set surface holder
  06-26 14:31:45.538: D/VideoPlayerActivity(15678): set displat for surface holder
  06-26 14:31:45.548: D/VideoPlayerActivity(15678): surface changed
  06-26 14:31:45.548: D/VideoPlayerActivity(15678): pathtoplay is http://www.youtube.com/watch?v=U4dK9yodrsk
  06-26 14:31:45.548: I/MediaPlayer(15678): This is not a sprint project
  06-26 14:31:45.548: D/MediaPlayer(15678): [DLNA]AP force disable DLNA: FALSE
  06-26 14:31:45.558: D/MediaPlayer(15678): [DLNA]Force disable DLNA: 
  06-26 14:31:45.568: D/VideoPlayerActivity(15678): set holder to mediaplayer
  06-26 14:31:45.568: D/MediaPlayer(15678): [DLNA]setDisplay
  06-26 14:31:45.568: I/MediaPlayer(15678): setLPAflag() in
  06-26 14:31:45.568: I/MediaPlayer(15678): mContext is null, can't getMirrorDisplayStatus!!!
  06-26 14:31:45.568: I/MediaPlayer(15678): setLPAflag() out
  06-26 14:31:46.409: E/MediaPlayer(15678): error (1, -2147483648)
  06-26 14:31:46.409: D/MediaPlayer(15678): Mediaplayer receives message, message type: 100
  06-26 14:31:46.409: E/MediaPlayer(15678): Error (1,-2147483648)
  06-26 14:31:46.419: E/VideoPlayerActivity(15678): MediaPlayer.onError() WHAT: 1EXTRA: -2147483648
  06-26 14:31:46.419: E/VideoPlayerActivity(15678): Media Error, Error Unknown -2147483648

 EDIT: For videos that are not from youtube, it does not crash, with that, error, it just does not show the video.What can be wrong?

这是非youtube视频的logcat:

 06-26 14:50:09.536: D/VideoPlayerActivity(18860): Start
 06-26 14:50:09.536: D/VideoPlayerActivity(18860): set video playback
 06-26 14:50:09.536: D/VideoPlayerActivity(18860): releasemediaplayer
 06-26 14:50:09.536: D/MediaPlayer(18860): release() in
 06-26 14:50:09.536: D/MediaPlayer(18860): release() out
 06-26 14:50:09.536: D/VideoPlayerActivity(18860): playvideo
 06-26 14:50:09.536: D/VideoPlayerActivity(18860): releasemediaplayer
 06-26 14:50:09.536: D/VideoPlayerActivity(18860): set surface holder
 06-26 14:50:09.546: D/VideoPlayerActivity(18860): set displat for surface holder
 06-26 14:50:09.556: D/VideoPlayerActivity(18860): surface changed
 06-26 14:50:09.556: D/VideoPlayerActivity(18860): pathtoplay is http://new.ineed.com/uploads/video/file/11426/Capture_113_1372246928119.mp4
 06-26 14:50:09.556: I/MediaPlayer(18860): This is not a sprint project
 06-26 14:50:09.556: D/MediaPlayer(18860): [DLNA]AP force disable DLNA: FALSE
 06-26 14:50:09.566: D/MediaPlayer(18860): [DLNA]Force disable DLNA: 
 06-26 14:50:09.576: D/VideoPlayerActivity(18860): set holder to mediaplayer
 06-26 14:50:09.576: D/MediaPlayer(18860): [DLNA]setDisplay
 06-26 14:50:09.576: I/MediaPlayer(18860): setLPAflag() in
 06-26 14:50:09.576: I/MediaPlayer(18860): mContext is null, can't getMirrorDisplayStatus!!!
 06-26 14:50:09.576: I/MediaPlayer(18860): setLPAflag() out

1 个答案:

答案 0 :(得分:0)

It Worked,我忘了放一个进度条,所以实际上它开始缓冲,但屏幕是黑色的,看到它不起作用,我没等它缓冲