OnBufferingUpdate不叫的MediaPlayer(OnBufferingUpdate

2019-10-18 08:10发布

我OnBufferingUpdate当我试图用我的MediaPlayer使用它永远不会被调用。 我使用的是保存在我的SD卡作为介质来源的视频。 我认为这是问题的根源,我想知道,如果OnBufferingUpdate仅适用于互联网流。 我使用的代码如下。

 public class FullImageActivity extends Activity implements SurfaceHolder.Callback,       OnPreparedListener, OnInfoListener,                                                                   OnErrorListener, OnBufferingUpdateListener{

private static final String TAG = "MediaPlayer shit";
MediaPlayer player;
SurfaceView surfaceview;
SurfaceHolder surfaceHolder;
int width;
int height;
DisplayImageOptions options;
ViewPager viewpager;
String path;
long id;
String[] projection = { GridviewData.ROWID, GridviewData.BITMAPPATH, GridviewData.VIDEOFILEPATH};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    Intent i = getIntent();

    id = i.getExtras().getLong("id");
    path = i.getExtras().getString("videopath");

    surfaceview = (SurfaceView)findViewById(R.id.surfaceview);
    surfaceHolder = surfaceview.getHolder();
    surfaceHolder.addCallback(this);



}

public void playVideo(){
    player = new MediaPlayer();
    try {
        player.reset();
        player.setOnErrorListener(this);
        File file = new File(path);
        FileInputStream inputStream = new FileInputStream(file);
        player.setDataSource(inputStream.getFD());

        inputStream.close();            
        player.setOnBufferingUpdateListener(this);
        player.setOnInfoListener(this);
        player.setOnPreparedListener(this);
        player.setDisplay(surfaceHolder);
        player.prepareAsync();


    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
@Override
protected void onPause() {
    surfaceview.setVisibility(View.GONE);
    if ( player != null ) 
    {
        if ( player.isPlaying() )
            player.stop();
        player.release();
        player = null;
    }
    super.onPause();
}
@Override
protected void onResume() {
    surfaceview.setVisibility(View.VISIBLE);
    Log.i(TAG, "In onResume");
    super.onResume();
}
@Override
protected void onStop() {
    super.onStop();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {
    if ( player != null ) 
    {
        try {
            Log.i(TAG, "In surface change");
            player.setDisplay(surfaceHolder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "in surface created");
    playVideo();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
        Log.i(TAG, "Surface destroyed");
        if ( player != null ) 
        {
            if ( player.isPlaying() )
                player.stop();
            player.release();
            player = null;
        }   }
@Override
public void onPrepared(MediaPlayer p) {


    player.start();

}

@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
    Log.i(TAG, "THERE WAS AN ERROR");
    return false;
}


@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {

    if (percent == 50){
    Log.i(TAG, "Within bufferingupdate");
    }

}

Answer 1:

你已经回答了你自己的问题! :)

onBufferingUpdate()只要求资源被流在网络上,所以这不会被调用本地资源。

http://developer.android.com/reference/android/media/MediaPlayer.OnBufferingUpdateListener.html

回调的接口定义要调用表示正在直播通过网络媒体资源的缓冲状态。

从源代码 :

称为缓冲通过渐进式HTTP下载接到媒体流来更新状态。 接收到的缓冲百分比表示有多少内容已经被缓冲或播放。 例如,当一半的内容已经播放过的80%的缓冲更新指示播放内容的下一个30%已经被缓冲。



文章来源: OnBufferingUpdate not being called in MediaPlayer