【问题标题】:Android, Soundpool Class - autopause problems while looping soundAndroid,Soundpool 类 - 循环声音时出现自动暂停问题
【发布时间】:2016-04-25 22:52:55
【问题描述】:

我在使用 soundpool 类的程序中遇到问题。我启动了 soundpool builder 并毫无问题地加载了我的声音。我希望能够使用多个切换按钮来打开和关闭特定的声音。我可以打开和关闭切换按钮,但例如,如果我打开两个或更多切换按钮,然后我暂停一个......其余的将停止。我不希望那样,我希望其他声音继续播放。这是我的代码。另外,如果有人可以帮我清理一下,那会很有帮助。提前致谢。

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bT1 = (ToggleButton) findViewById(R.id.firstToggle);
        bT2 = (ToggleButton) findViewById(R.id.secondToggle);
        bT1.setOnClickListener(this);
        bT2.setOnClickListener(this);
        bT3 = (ToggleButton) findViewById(R.id.thirdToggle);
        bT4 = (ToggleButton) findViewById(R.id.fourthToggle);
        bT3.setOnClickListener(this);
        bT4.setOnClickListener(this);
        bT5 = (ToggleButton) findViewById(R.id.fifthToggle);
        bT6 = (ToggleButton) findViewById(R.id.sixToggle);
        bT5.setOnClickListener(this);
        bT6.setOnClickListener(this);
        bT7 = (ToggleButton) findViewById(R.id.seventhToggle);
        bT8 = (ToggleButton) findViewById(R.id.eighthToggle);
        bT7.setOnClickListener(this);
        bT8.setOnClickListener(this);

        initionalizeSoundpool();
    }

    private void initionalizeSoundpool() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .build();

            ourSounds = new SoundPool.Builder()
                    .setMaxStreams(8)
                    .setAudioAttributes(audioAttributes)
                    .build();
            soundFx = ourSounds.load(this, R.raw.highhatandrims, 1);
            beatOne = ourSounds.load(this, R.raw.technodrums, 1);
            soundFx2 = ourSounds.load(this, R.raw.soothing, 1);
            beatTwo = ourSounds.load(this, R.raw.thumpohyeahbeat, 1);
            soundFx3 = ourSounds.load(this, R.raw.dreamyone, 1);
            beatThree = ourSounds.load(this, R.raw.atredundantsynthesis, 1);
            soundFx4 = ourSounds.load(this, R.raw.tawaka, 1);
            clap = ourSounds.load(this, R.raw.cabessa, 1);
        } else {
            ourSounds = new SoundPool(4, AudioManager.STREAM_MUSIC,1);
            soundFx = ourSounds.load(this, R.raw.highhatandrims, 1);
            beatOne = ourSounds.load(this, R.raw.technodrums, 1);
            soundFx2 = ourSounds.load(this, R.raw.soothing, 1);
            beatTwo = ourSounds.load(this, R.raw.thumpohyeahbeat, 1);
            soundFx3 = ourSounds.load(this, R.raw.dreamyone, 1);
            beatThree = ourSounds.load(this, R.raw.atredundantsynthesis, 1);
            soundFx4 = ourSounds.load(this, R.raw.tawaka, 1);
            clap = ourSounds.load(this, R.raw.cabessa, 1);
        }

    }




public void onClick(View view) {
        switch (view.getId()) {
            case R.id.firstToggle:
                if(bT1.isChecked()) {
                    ourSounds.play(soundFx, 1, 1, 1, -1, 1);
                }
                else {
                    ourSounds.autoPause();
                }
                break;
            case R.id.secondToggle:
                if(bT2.isChecked()) {
                    ourSounds.play(beatOne, 1, 1, 1, -1, 1);
                }
                else {
                    ourSounds.autoPause();
                }
                break;
            case R.id.thirdToggle:
                if(bT3.isChecked()) {
                    ourSounds.play(soundFx2, 1, 1, 1, -1, 1);
                }
                else {
                    ourSounds.autoPause();
                }
                break;
            case R.id.fourthToggle:
                if(bT4.isChecked()) {
                    ourSounds.play(beatTwo, 1, 1, 1, -1, 1);
                }
                else {
                    ourSounds.autoPause();
                }
                break;

【问题讨论】:

  • 声音文件的长度是多少?
  • 它们是短样本,可能 5-10 秒长。

标签: java android android-studio audio soundpool


【解决方案1】:

将此添加到您的 OnPause 方法中:

@Override
public void onPause() {
   super.onPause();   

   ourSounds.release();
   ourSounds = null;
}

更新:

例如,请尝试使用ourSounds.pause(soundID);,而不是ourSounds.autoPause();

case R.id.firstToggle:
            if(bT1.isChecked()) {
                ourSounds.play(soundFx, 1, 1, 1, -1, 1);
            }
            else {
                ourSounds.pause(soundFx);
            }
            break;
 ...

【讨论】:

  • 我尝试将这两行代码放在自动暂停下方的 else 语句中,但没有成功。我会按两个或三个按钮,所有按钮都会播放,但是当我暂停一个按钮时,其他按钮会停止,它会将我踢出程序。不过感谢您的建议。
  • 不,我的意思是在OnPause 方法中,请查看我更新的答案
  • 我还是个初学者,所以我很难理解。我没有 onPause 方法或 onResume。如果我创建 onPause 是否需要 onResume?在我的 else 语句中,我在哪里调用 onPause?我需要将 int 传递给 onPause 吗?再次感谢您的帮助。
  • 只需将我的答案复制到您的activity 中即可。在您的onClick onCreate 之外...
  • 我粘贴在我的 onCreate 上方的活动中,在那里我设置了我的 setOnClickListeners,而不是我的 initionalize soundpool() 和我的 onclick(view),这就是我的整个 java 程序。程序运行,我可以打开/关闭每个按钮,音乐将毫无问题地开始和停止。但是当我打开两个或多个切换按钮并关闭一个时,它们都会像以前一样关闭。希望这是有道理的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多