0

When I try to start a sound in my application it doesn't play. I have seen that the method does get called however soundPool just doesn't want to play the sound. The app starts as intended, however, it just doesn't want to play the queued sound. Is it something I am doing wrong? is the music file I am trying to play just too large? (35.4MB)

Any input is appreciated!

StartActivity (relevant bit)

public class StartActivity extends AppCompatActivity {
    private Button startButton;

    //Sounds
    private SoundManager soundManager;
    private int ambient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_activity);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        startButton = (Button) findViewById(R.id.startButton);

        //Setting Up sounds
        soundManager = new SoundManager(this);
        ambient = soundManager.getSoundID(R.raw.music_ambient);

        soundManager.playLoop(ambient);
        Application.setSoundManager(soundManager);

Application


public class Application {
    public static SoundManager soundManager;

    public static SoundManager getSoundManager() {
        return soundManager;
    }

    public static void setSoundManager(SoundManager soundManagerIns) {
        soundManager = soundManagerIns;
    }
}

SoundManager class


import android.content.Context;
import android.media.SoundPool;
import android.util.Log;

public class SoundManager {

    private Context context;
    private SoundPool soundPool;
    private boolean isPlaying = false;

    SoundManager(Context context) {
        this.context = context;

        SoundPool.Builder builder = new SoundPool.Builder();
        builder.setMaxStreams(10);
        soundPool = builder.build();
    }

    int getSoundID(int resourcesID) {
        return (soundPool.load(context, resourcesID, 1));
    }

    void play(int soundId) {
        soundPool.play(soundId, 1, 1, 1, 0, 1);
    }

    void playLoop(int soundId){
        if(!isPlaying) {
            Log.d("LEADER", "called this");
            soundPool.play(soundId, 1, 1, 1, -1, 1);
            isPlaying = true;
        }
    }

    void stop(int soundId){
        isPlaying = false;
        soundPool.stop(soundId);
    }
}

  • Why do you have `soundManager` in the application? It is basically a global mutable variable, which may not be a good idea for several reasons, such as thread-safety if you are not careful, as well as reasoning about what can be modified where and when. I would consider refactoring it away, though I might be wrong about that. – MelvinWM Jun 03 '20 at 06:49
  • Have you taken an existing Android example application that has sound and tried to get that successfully running? That could help cut down on the possible causes and help you find the bug. – MelvinWM Jun 03 '20 at 06:50
  • Are there any part of your program that "owns" the resource `SoundManager`? Thinking in terms of "ownership" can be very useful for managing resources, especially since it gives guidance to when the given resource should be released. For instance, if you have a resource that is owned by a given activity, then it likely makes sense to initialize and release that resource as part of the specific activity's life-cycle. In Android, `Context` is often in practice related to ownership. – MelvinWM Jun 03 '20 at 06:53

1 Answers1

0

is the music file I am trying to play just too large? (35.4MB)

That might be the case. I have seen several StackOverflow answers (including recent ones) that claim that the uncompressed in-memory limit per sound is 1 MB for SoundPool, though I haven't been able to check that in the official documentation. If it is indeed the case, you might be able to see this error in your log as described by https://stackoverflow.com/a/18548242/9543944 :

If your clip is too big in memory, sound pool will fall silent, and you'll find following error: "AudioFlinger could not create track. status: -12"

You should be able to see the log from inside Android Studio, and you can then filter for errors to more quickly search for the above error.

If this is indeed the case, MediaPlayer might be a good alternative: https://developer.android.com/guide/topics/media/mediaplayer .

MelvinWM
  • 749
  • 4
  • 13