Implementing Audiotrack Class in Static mode

Asked

Viewed 76 times

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

  • @Wakin I’ve tried to comment on the stop and release, but it didn’t work!

  • logcat indicates that Audiotrack has not been initialized correctly. Check which state of Audiotrack through: audioTrack.getState();. More info on Audiostack

  • 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.

1 answer

1

At the startup of your AudioTrack has a space in an argument. Look closely:

private AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
            AudioFormat. CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, 88200,
            AudioTrack.MODE_STATIC );

The argument AudioFormat. CHANNEL_OUT_MONO is with a space after the point, remove this space. Another thing I realized is that in the class documentation AudioTrack concerning the method play(), says the following:

Starts playing an Audiotrack. If track’s Creation mode is MODE_STATIC, you must have called write() prior.

Translating...

Starts playback of an Audiotrack. If the track creation mode is MODE_STATIC, you should call write() before.

As your way of creating the track there is MODE_STATIC, then reverse the lines:

audioTrack.play();
audioTrack.write(byteData, 0, byteData.length); 

The write method has to be called before the play(). See if this solves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.