简体   繁体   English

SoundPool 不播放声音

[英]SoundPool doesn't play sounds

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.我已经看到该方法确实被调用了,但是 soundPool 只是不想播放声音。 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) (35.4MB)

Any input is appreciated!任何输入表示赞赏!

StartActivity (relevant bit) StartActivity(相关位)

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 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);
    }
}

is the music file I am trying to play just too large?我要播放的音乐文件是否太大? (35.4MB) (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.我已经看到几个 StackOverflow 答案(包括最近的答案)声称SoundPool的每个声音的未压缩内存限制为 1 MB,尽管我无法在官方文档中进行检查。 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 :如果确实如此,您可能会在日志中看到此错误,如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"如果您的剪辑在 memory 中太大,声音池将静音,您会发现以下错误:“AudioFlinger 无法创建轨道。状态:-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.您应该能够从 Android Studio 内部看到日志,然后您可以过滤错误以更快地搜索上述错误。

If this is indeed the case, MediaPlayer might be a good alternative: https://developer.android.com/guide/topics/media/mediaplayer .如果确实如此, MediaPlayer可能是一个不错的选择: https://developer.android.com/guide/topics/media/mediaplayer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM