OnBufferingUpdate not being called in MediaPlayer

北战南征 提交于 2019-12-13 02:32:53

问题


My OnBufferingUpdate never gets called when I try to use it with my MediaPlayer. I'm using videos saved on my SDcard as the media source. I think this is the source of the problem and I'm wondering if OnBufferingUpdate only works for internet streams. The code I am using is below.

 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");
    }

}

回答1:


You've answered your own question! :)

onBufferingUpdate() is only called for resources being streamed over a network, so this will not be called for local resources.

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

Interface definition of a callback to be invoked indicating buffering status of a media resource being streamed over the network.

From the source code:

Called to update status in buffering a media stream received through progressive HTTP download. The received buffering percentage indicates how much of the content has been buffered or played. For example a buffering update of 80 percent when half the content has already been played indicates that the next 30 percent of the content to play has been buffered.



来源:https://stackoverflow.com/questions/17526601/onbufferingupdate-not-being-called-in-mediaplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!