2012-01-05 171 views
0

我正在使用下面的代碼播放音頻文件。AudioPlay給出錯誤

但它給了我錯誤。

01-05 11:35:05.793: W/System.err(1739): java.lang.IllegalStateException 
01-05 11:35:05.793: W/System.err(1739):  at android.media.MediaPlayer.prepare(Native Method) 
01-05 11:35:05.793: W/System.err(1739):  at com.android.halfivrs.MyAudioControl.startAudio(MyAudioControl.java:22) 
01-05 11:35:05.793: W/System.err(1739):  at com.android.halfivrs.HalfIVRSActivity.onCreate(HalfIVRSActivity.java:13) 
01-05 11:35:05.793: W/System.err(1739):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-05 11:35:05.793: W/System.err(1739):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
01-05 11:35:05.793: W/System.err(1739):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
01-05 11:35:05.793: W/System.err(1739):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
01-05 11:35:05.793: W/System.err(1739):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
01-05 11:35:05.793: W/System.err(1739):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-05 11:35:05.793: W/System.err(1739):  at android.os.Looper.loop(Looper.java:123) 
01-05 11:35:05.793: W/System.err(1739):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
01-05 11:35:05.793: W/System.err(1739):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-05 11:35:05.793: W/System.err(1739):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-05 11:35:05.793: W/System.err(1739):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-05 11:35:05.793: W/System.err(1739):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-05 11:35:05.793: W/System.err(1739):  at dalvik.system.NativeStart.main(Native Method) 

這是我的代碼。

public class MyAudioControl { 

    private Context mContext; 

    public MyAudioControl(Context con) { 
     this.mContext = con; 
    } 

    private MediaPlayer player; 

    public void startAudio() { 
     try { 
      player = MediaPlayer.create(mContext, R.raw.tabla); 
      player.setLooping(false); // Set looping 
      player.prepare(); 
      player.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void stopAudio() { 
     try { 
      player.stop(); 
      player.reset(); 
      player.release(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

有沒有人知道上面的代碼有什麼問題?

謝謝。

回答

0

試試這個代碼:

public void startAudio() { 
     player = MediaPlayer.create(ScaleTestActivity.this, R.raw.test30fps); 
     player.setLooping(false); // Set looping 
     player.setOnPreparedListener(new OnPreparedListener() { 

      @Override 
      public void onPrepared(MediaPlayer mp) { 
       // TODO Auto-generated method stub 
       player.start(); 
      } 
     });   
} 

希望對你有用...

+0

它不給我的錯誤,而且不打文件。 – Nirav 2012-01-05 06:33:32

+0

檢查你放在原始文件夾上的文件是否在其他播放器上播放,因爲我可以像這樣播放.mp3文件。 – 2012-01-05 06:45:19

+0

@Nirav,我認爲如果你不能播放這個文件,那麼你的日誌裏一定有錯誤。 – artsylar 2012-01-06 05:18:11

0
  player = MediaPlayer.create(this, R.raw.robotrock); 
      player.setLooping(false); // Set looping 


      playerButton = (Button) this.findViewById(R.id.play); 

      playerButton.setOnClickListener(new OnClickListener() { 

        public void onClick(View v) { 
         player.start(); 

        } 
       });