mediaplayer需要在surfaceView创建之后才能创建,不然会导致错误。
1 surfaceholder = msurface.getHolder(); 2 surfaceholder.setKeepScreenOn(true); 3 surfaceholder.addCallback(new SurfaceViewLis());
1 private class SurfaceViewLis implements SurfaceHolder.Callback { 2 3 @Override 4 public void surfaceChanged(SurfaceHolder holder, int format, int width, 5 int height) { 6 7 } 8 9 @Override 10 public void surfaceCreated(SurfaceHolder holder) { 11 mPlayer = MediaPlayer.create(AnimationGuideActivity.this, R.raw.guideanimation); 12 mPlayer.setDisplay(holder); 13 mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 14 15 @Override 16 public void onCompletion(MediaPlayer arg0) { 17 // TODO Auto-generated method stub 18 Intent intent = new Intent(AnimationGuideActivity.this, IrisActivity.class); 19 finish(); 20 startActivity(intent); 21 } 22 }); 23 24 try { 25 mPlayer.setOnPreparedListener(new OnPreparedListener() { 26 27 @Override 28 public void onPrepared(MediaPlayer arg0) { 29 mPlayer.start(); 30 31 } 32 }); 33 mPlayer.prepareAsync(); 34 } catch (IllegalStateException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 39 } 40 41 @Override 42 public void surfaceDestroyed(SurfaceHolder holder) { 43 44 } 45 46 }