0
I’m trying to do in app very simple so I learn to implement the Audiotrack class (android.media.AudioTrack
).
The app is just a screen with a button, which when pressed should play a file . wav of 1seg. The app installs smoothly in the emulator, but when I click the button to play the sound, it gives error.
My implementation of audioTrack
is 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
@Andrecsimoes, is the fact that giving stop/release soon after giving play does not cause problems (even if it is 1 second)? It is not worth testing
– Wakim
@Wakin I’ve tried to comment on the stop and release, but it didn’t work!
– AndreCSimoes
logcat indicates that Audiotrack has not been initialized correctly. Check which state of Audiotrack through:
audioTrack.getState();
. More info on Audiostack– ramaral
Just one question... Did you happen to declare the two public classes in the same file? Or did you just put them together here in your question? A source file cannot have two public classes.
– Lucas Santos