AudioTrack静态模式实现

时间:2014-06-05 14:36:38

标签: android

我正在努力学习如何以最简单的方式实现audiotrack课程。我研究了很多主题,但我仍然不了解录音带。 我创建了一个简单的应用程序,它只包含一个按钮,按下该按钮时会执行1秒*.wav个文件。 但当我按下按钮时,应用程序崩溃了。 有人能帮助我吗?

在我的java代码下面,和LogCat:

MainActivity.java:

  public class MainActivity extends Activity{

    private DrumKit dk = new DrumKit();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        }  

    public void onClick(View v) { 
        dk.playAudio();
    } 
   }

运行音频的类:

     public class DrumKit {

     byte[] byteData = null;
     private File file = null; 
     private AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
            AudioFormat. CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, 88200,
            AudioTrack.MODE_STATIC );

     public void playAudio(){

         file = new File(Environment.getExternalStorageDirectory(), "snare.wav");
         byteData = new byte[(int) file.length()];

         audioTrack.play();
         audioTrack.write(byteData, 0, byteData.length); 
         audioTrack.stop();
         audioTrack.release();
     }
  }

logcat的:

06-05 10:23:01.131: D/gralloc_goldfish(1381): Emulator without GPU emulation detected.
06-05 10:23:14.371: D/AndroidRuntime(1381): Shutting down VM
06-05 10:23:14.371: W/dalvikvm(1381): threadid=1: thread exiting with uncaught exception (group=0xb1a1eba8)
06-05 10:23:14.451: E/AndroidRuntime(1381): FATAL EXCEPTION: main
06-05 10:23:14.451: E/AndroidRuntime(1381): Process: com.example.airdrums, PID: 1381
06-05 10:23:14.451: E/AndroidRuntime(1381): java.lang.IllegalStateException: Could not execute method of the activity
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.view.View$1.onClick(View.java:3823)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.view.View.performClick(View.java:4438)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.view.View$PerformClick.run(View.java:18422)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at     android.os.Handler.handleCallback(Handler.java:733)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.os.Looper.loop(Looper.java:136)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at java.lang.reflect.Method.invoke(Method.java:515)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at dalvik.system.NativeStart.main(Native Method)
06-05 10:23:14.451: E/AndroidRuntime(1381): Caused by: java.lang.reflect.InvocationTargetException
06-05 10:23:14.451: E/AndroidRuntime(1381):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at java.lang.reflect.Method.invoke(Method.java:515)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.view.View$1.onClick(View.java:3818)
06-05 10:23:14.451: E/AndroidRuntime(1381):     ... 11 more
06-05 10:23:14.451: E/AndroidRuntime(1381): Caused by: java.lang.IllegalStateException: play() called on uninitialized AudioTrack.
06-05 10:23:14.451: E/AndroidRuntime(1381):     at android.media.AudioTrack.play(AudioTrack.java:984)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at com.example.airdrums.DrumKit.playAudio(DrumKit.java:25)
06-05 10:23:14.451: E/AndroidRuntime(1381):     at com.example.airdrums.MainActivity.onClick(MainActivity.java:51)
06-05 10:23:14.451: E/AndroidRuntime(1381):     ... 14 more

1 个答案:

答案 0 :(得分:0)

您需要在播放之前编写音轨。否则就没有数据可以播放。

此外,您的代码实际上并未读取文件,因此您尝试播放随机字节。