Search code examples
androidsoundpoologg

Android .ogg file stops playing after a few seconds


The music either doesn't even start playing sometimes, but when it does, it stops after a few seconds. The file is 2 minutes long. I really don't know what's the problem.

@Override
public void onCreate(Bundle savedInstanceState) {
music = soundPoolVar.load(this, R.raw.music11, 1);
}

then

public void soundPlay(int i, MySurface pl, float volume) {
    if (soundOn == true) {
        switch (i) {
case 11:
            soundPoolVar.play(music, 1, 1, 0, 0, 1);
            break;
}}}

Solution

  • SoundPool designed to play short sound effects. To play music (big audio files) you need to use MediaPlayer

    // R.raw.audio_file - res/raw/audio_file.mp3
    
    mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
    mediaPlayer.start();