2

I've been stuck on this problem for days. I'm at the stage now where I'm just trying to get a working example to build from, but I can't even reach that point.

My question is based on this example. I've included the source (and solution) into my own code, and can see the buffer is being written to, but no sound comes from the device. I'm struggling to identify how to debug this further.

package com.example.audio;
import com.example.R;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

public class Synth {
    private static final String TAG = Synth.class.getSimpleName();
    private AudioTrack audioTrack;
    private Context context;

    public Synth(Context context) {
        this.context = context;
        int minBufferSize = AudioTrack.getMinBufferSize(22050, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM); 
    }
    public void playSound() {
        audioTrack.play();
        int i = 0;
        int bufferSize = 512;
        byte [] buffer = new byte[bufferSize];
        InputStream inputStream = context.getResources().openRawResource(R.raw.mysample);
        try {
            int writes = 0;
            while((i = inputStream.read(buffer)) != -1) {
                writes++; audioTrack.write(buffer, 0, i);
            }
            Log.d(TAG, "Wrote " + writes + " times." );
        } catch (IOException e) {e.printStackTrace();}
        try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
    }
}

(To replicate project: make new project, include this class, add the audio mentioned below, create an instance of the Synth, then fire the playSound public function)

Obviously I can't upload audio, or guarantee it's persistence in a web folder, but this tool will generate a WAV file exactly like I am using (change the sample rate to 22.05kHz and press 'download .wav file')

I realize that the STREAM_MUSIC and MODE_STREAM seem out of place in this example. I am using those flags/mode because I know I will need to use AudioTrack with these modes later in development, and really am just trying to get sound coming out of the device for the moment.

Can anyone suggest debugging steps, or see what the problem with the playback is? Any help appreciated.

Community
  • 1
  • 1
1owk3y
  • 1,115
  • 1
  • 15
  • 30
  • It works for me. The sound is segmented (when I open the `wav` file in `VLC` it's smooth), but it plays *something*. I've also had to remove the `import com.example.R`, but besides that, I've copied-pasted your exect code. Did you make sure that you canplay that file in `VLC` or `Media Player`? Did you check your `audio settings` in your device/computer (I've used an emulator)? – TDG Jul 11 '15 at 07:25
  • Wow, this is awkward. I tried the sample that I suggested in the post and it worked, then I tried my original sample and it worked. I have no idea what happened. This is so frustrating! Any ideas what could have happened? My code sample is completely unedited since I've been stuck! Actually I was using a device. Hm. I'm not sure what to do in this situation, since the issue isn't really an issue.. – 1owk3y Jul 11 '15 at 08:03
  • I've no idea. Sometimes it's pure voodoo.... – TDG Jul 11 '15 at 08:20
  • Thanks for your help anyway! I'd give you the 'correct answer' but I'm concerned that moderators will think it's just point farming or something. I had better self-answer that the code worked as written. – 1owk3y Jul 11 '15 at 08:28
  • 1
    PS - I've tested it now on a 'real' device, and the audio is smooth, unlike the emulator. – TDG Jul 11 '15 at 08:49
  • Thanks for taking the time to test extensively. I would expect the emulator sound performance would differ, but it's good info for anyone else who finds this question – 1owk3y Aug 16 '15 at 00:17

1 Answers1

4

I've determined the problem I had originally when I first raised this question. Hopefully it helps some other people out there...

Android phone volumes

As it turns out, the app wasn't playing audio at the time when I was adjusting the volume, so the volume that was actually maxed was the system/ringtone volumes, and not the 'music, video games, and other media' volume, which was muted.

1owk3y
  • 1,115
  • 1
  • 15
  • 30