gpt4 book ai didi

java - SoundPool 不播放声音

转载 作者:行者123 更新时间:2023-12-01 16:25:01 26 4
gpt4 key购买 nike

当我尝试在应用程序中启动声音时,它无法播放。我已经看到该方法确实被调用,但是 soundPool 只是不想播放声音。该应用程序按预期启动,但是,它只是不想播放排队的声音。难道是我做错了什么?我尝试播放的音乐文件太大了吗? (35.4MB)

任何意见都值得赞赏!

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

申请


public class Application {
public static SoundManager soundManager;

public static SoundManager getSoundManager() {
return soundManager;
}

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

SoundManager 类


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)

情况可能是这样。我见过几个 StackOverflow 答案(包括最近的答案),声称 SoundPool 每个声音的未压缩内存限制为 1 MB,尽管我无法在官方文档中检查这一点。如果确实如此,您可能会在日志中看到此错误,如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"

您应该能够从 Android Studio 内部看到日志,然后您可以过滤错误以更快地搜索上述错误。

如果确实如此,MediaPlayer 可能是一个不错的选择:https://developer.android.com/guide/topics/media/mediaplayer .

关于java - SoundPool 不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com