I'm trying to make in app pretty simple for me to learn how to implement the AudioTrack class (% with%).
The app is just a screen with a button, which when pressed must play a .sav file of 1sec. The app installs smoothly in the emulator, but when I click the button to play the sound, it gives error.
Is my implementation of android.media.AudioTrack
correct?
//MainActivity
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();
}
}
//Classe para executar o audio:
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