gpt4 book ai didi

java - Android Mediaplayer 缓冲未完成

转载 作者:行者123 更新时间:2023-11-30 04:39:23 25 4
gpt4 key购买 nike

我已经实现了一个标准的 MediaPlayer,就像 Android API 示例中的那样。出于某种原因,我的视频只缓冲到 47% 左右然后停止。我不认为这是一个互联网连接问题,因为它每次都停在 46% 或 47%。文件缓冲并通过 native 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();
}

}

最佳答案

很可能是缓冲结束了。
你的缓冲可以这样:10% - 23% - 30% - 47% - 100%

关于java - Android Mediaplayer 缓冲未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com