我正在开发Android媒体播放器,它可以播放视频和显示内容。该应用程序能够缓冲,但视频没有显示在表面视图上,虽然我能够听到声音。 以下是我的代码:
public class VideoPlayerActivity extends Activity implements OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener, OnSeekCompleteListener, OnErrorListener, SurfaceHolder.Callback {
private VideoView videoView;
private MediaPlayer mp;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
// surfaceHolder.setFixedSize(176, 144);
// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
try {
String path = "http://xxxxxxxxxxx/4_xxx.mp4";
mp = new MediaPlayer();
mp.setDataSource(path);
mp.setDisplay(surfaceHolder);
mp.prepare();
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnSeekCompleteListener(this);
mp.setOnErrorListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
// Uri uri = Uri.parse(path);
//
// videoView = (VideoView) findViewById(R.id.videoView1);
//
// videoView.setVideoURI(uri);
//
// videoView.setMediaController(new MediaController(this));
//
//
//
// videoView.setOnPreparedListener(this);
//
//
//
// videoView.requestFocus();
//
// videoView.start();
}
@Override
public void onPrepared(MediaPlayer mp) {
System.out.println("=======In on prepared======Media Player=====" + mp);
}
@Override
public void onCompletion(MediaPlayer mp) {
System.out.println("=======In on completion====== Media player====" + mp);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
System.out.println("=======In on Buffering update======Media Player====" + mp + "====percent====" + percent);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
System.out.println("=======In on Error======Media Player====" + mp + "=====what====" + what + "====extra====" + extra);
return false;
}
@Override
public void onSeekComplete(MediaPlayer mp) {
System.out.println("=======In on seek complete======Media Player====" + mp);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mp.setDisplay(holder);
play();
}