Android Mediaplayer buffering does not complete
I have implemented a standard MediaPlayer like the one in the Android API sample. For some reason my video only buffers to something like 47% and then stops. I don't think it's an internet connection issue since it stops at 46% or 47% every single time. The file buffers and plays fine through the native android video player. Its a progressive download mp4.
Here is the code for it:
package com.exact.test;
import android.app.A开发者_C百科ctivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoPlayer extends RelativeLayout implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
private VideoPlayer me = null;
//Components that make up the view
private VideoView videoView = null;
private ImageSwitcher imageSwitcher = null;
private ImageButton btnBack = null;
private ImageButton btnPlay = null;
private ImageButton btnStop = null;
private ImageButton btnForward = null;
private TextView txtBufferStatus = null;
//Video playback variables
private static final String TAG = "VideoPlayer";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
public String path = "http://mydomain.com/android/test8.mp4";
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;
public VideoPlayer(Context context) {
this(context, null);
}
public VideoPlayer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VideoPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
((Activity)getContext())
.getLayoutInflater()
.inflate(R.layout.videoplayer, this, true);
me = this;
//Get all the components in the layout
//Layout videoLayout = VideoPlayer.
videoView = (VideoView)findViewById(R.id.videoView);
imageSwitcher = (ImageSwitcher)findViewById(R.id.ImageSwitcher);
btnBack = (ImageButton)findViewById(R.id.btnBack);
btnPlay = (ImageButton)findViewById(R.id.btnPlay);
btnStop = (ImageButton)findViewById(R.id.btnStop);
btnForward = (ImageButton)findViewById(R.id.btnForward);
txtBufferStatus = (TextView)findViewById(R.id.txtBufferStatus);
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
txtBufferStatus.setText("Initilaising...");
imageSwitcher.setVisibility(INVISIBLE);
videoView.setVisibility(VISIBLE);
mPreview = (SurfaceView) videoView;
holder = mPreview.getHolder();
holder.addCallback(me);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
txtBufferStatus.setText("Initilaised");
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnForward.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
public void playVideo() {
doCleanUp();
Runnable r = new Runnable() {
@Override
public void run() {
try {
// Create a new media player and set the listeners
txtBufferStatus.setText("Setting up Media Player...");
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.setDisplay(holder);
txtBufferStatus.setText("Preparing...");
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnBufferingUpdateListener(me);
mMediaPlayer.setOnCompletionListener(me);
mMediaPlayer.setOnPreparedListener(me);
mMediaPlayer.setOnVideoSizeChangedListener(me);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
}
}
};
Thread t = new Thread(r);
t.start();
}
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate percent:" + percent);
txtBufferStatus.setText(Integer.toString(percent));
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
}
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.v(TAG, "onVideoSizeChanged called");
if (width == 0 || height == 0) {
Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
return;
}
mIsVideoSizeKnown = true;
mVideoWidth = width;
mVideoHeight = height;
if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
startVideoPlayback();
}
}
public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
txtBufferStatus.setText("Prepared");
mIsVideoReadyToBePlayed = true;
if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
startVideoPlayback();
}
}
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
Log.d(TAG, "surfaceChanged called");
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceDestroyed called");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "surfaceCreated called");
playVideo();
}
public void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public void doCleanUp() {
mVideoWidth = 0;
mVideoHeight = 0;
mIsVideoReadyToBePlayed = false;
mIsVideoSizeKnown = false;
}
private void startVideoPlayback() {
Log.v(TAG, "startVideoPlayback");
holder.setFixedSize(mVideoWidth, mVideoHeight);
mMediaPlayer.start();
}
}
It is likely that the buffering is finished.
Your buffering could go this way: 10% - 23% - 30% - 47% - 100%
精彩评论