0

我遇到的问题是,当我尝试使用 MediaPlayer 播放视频时,我的 OnInfoListener 永远不会被调用。我做了研究,似乎找不到答案。我想出的唯一可能性是我正在使用 mp4 视频文件,可能 OnInfoListner 无法识别该类型的文件。我的代码如下。

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

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.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 what, int arg2) {
    Log.i(TAG, "THERE WAS AN ERROR of " );
    return true;
}





@Override
public boolean onInfo(MediaPlayer p, int what, int extra) {
    Log.i(TAG, "Within onInfo and of " + what + "and the extra of " + extra);

    return true;
}

谢谢。

4

1 回答 1

2

MEDIA_INFO_VIDEO_RENDERING_START在 API 级别 17 中添加。

确保您在此级别上构建和测试(Android 4.2 JELLY_BEAN_MR1)。如果不是,您将不会看到这些消息。

http://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_INFO_VIDEO_RENDERING_START

于 2013-07-09T07:15:10.203 回答