|
1功能:進(jìn)入一個(gè)activity,使用MediaPlayer和SurfaceView類來(lái)播放視頻,啟動(dòng)activity時(shí),直接初始化做MediaPlayer.start(),而不是通過(guò)一個(gè)監(jiān)聽器什么的來(lái)啟動(dòng),就會(huì)出現(xiàn):The surface has been released.
錯(cuò)誤位置:mediaplayer.setDisplay(surfaceView.getHost());
原因:mediaPlayer對(duì)象沒有做好準(zhǔn)備。
為什么?想想,如果你做一個(gè)監(jiān)聽,那么,視頻就不是直接播放了,他有充分時(shí)間做準(zhǔn)備了,就不會(huì)有錯(cuò)誤了。
解決方法:寫一個(gè)通過(guò)一個(gè)ScheduledExecutorService和Handler類來(lái)解決??创a,不多。[mw_shl_code=java,true]import android.view.SurfaceView;
public class PlayActivity extends Activity {
private static String path,endName;
private SurfaceHolder mSurfaceHolder;
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
private ScheduledExecutorService executorService;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
initialize();
}
private void initialize(){
executorService = Executors.newScheduledThreadPool(1);
mSurfaceView = (SurfaceView) findViewById(R.id.VideoView01);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Intent i = getIntent();
path = i.getStringExtra("path");
endName = i.getStringExtra("endName");
Log.i("abc", path+","+endName);
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
initMedia();
return false;
}
});
executorService.schedule(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
}, 10000, TimeUnit.MICROSECONDS);
}
private void initMedia(){
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDisplay(mSurfaceHolder);
try {
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaPlayer.start();
}
[/mw_shl_code]
該貼已經(jīng)同步到 LinPeiChun的微博</font |
上一篇: [底層開發(fā)教程] 從底層硬件到開發(fā)平臺(tái)--一步步深入Android底下一篇: 在線更新——之實(shí)例版!
|