我已经实现了类似Android API示例中的标准MediaPlayer。出于某种原因,我的视频只缓冲到47%,然后停止。我不认为这是一个互联网连接问题,因为它每次都停止在46%或47%。该文件缓存并通过本机Android视频播放器播放。它是一个渐进式下载mp4。
以下是代码:
package com.exact.test;
import android.app.Activity;
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();
}
}
答案 0 :(得分:1)
缓冲可能已经完成 你的缓冲可以这样:10% - 23% - 30% - 47% - 100%