【问题标题】:Seekbar Controlling Soundpool, stops workingSeekbar 控制 Soundpool,停止工作
【发布时间】:2017-04-27 14:20:53
【问题描述】:

所以我有 8 张图片,每张图片都会启动 8 种不同声音的循环。我在每个图像下都有一个搜索栏来控制该声音的音量。

搜索栏在第一次声音开始时工作正常,但当停止并再次启动时,搜索栏不再控制音量。我究竟做错了什么? `noiseVolumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            double noiseVol = (double) progress / (double) maxVolume;

            Log.i("Noise Volume", String.valueOf(progress) + " " + String.valueOf((float) noiseVol));
            mySound.setVolume(whiteNoiseId, (float) noiseVol, (float) noiseVol);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

public void whiteNoiseTapped(View view) {

    Log.i("White Noise", "button tapped");

    if (whiteNoisePlaying) {
        Log.i("White Noise", "already playing");
        mySound.stop(noiseStreaming);
        whiteNoisePlaying = false;
        ((ImageView) view).setImageResource(R.drawable.whitenoise);
        isPlaying--;
    } else if (isPlaying < 3) {
        Log.i("White Noise", "start playing");
        noiseStreaming = mySound.play(whiteNoiseId, (float) noiseVol, (float) noiseVol, 1, -1, 1);
        whiteNoisePlaying = true;
        ((ImageView) view).setImageResource(R.drawable.whitenoisepressed);
        isPlaying++;
    }
}`

【问题讨论】:

  • noiseVol 是介于 0.0 和 1.0 之间的值吗?它必须在 API 中描述的范围内..
  • 我猜你必须释放声音池,重新构建它并在停止声音后开始。

标签: android volume soundpool


【解决方案1】:

我想通了。我需要一种知道我之前是否已经点击过按钮的方法。我最初将noiseStreaming 设置为0,然后在按下按钮时检查它。如果它是 0,那么我将 whiteNoiseId 设置为 noiseStreaming。下次它不会是 0 并且会跳过那个位 -

public void whiteNoiseTapped(View view) {

    Log.i("White Noise", "button tapped");

    if (whiteNoisePlaying) {
        Log.i("White Noise", "already playing");
        mySound.pause(noiseStreaming);
        whiteNoisePlaying = false;
        ((ImageView) view).setImageResource(R.drawable.whitenoise);
        isPlaying--;
    } else if (isPlaying < 3) {
        Log.i("White Noise", "start playing");
        if (noiseStreaming == 0) {
            noiseStreaming = mySound.play(whiteNoiseId, (float) noiseVol, (float) noiseVol, 1, -1, 1);
        } else {
            mySound.resume(noiseStreaming);
        }
        whiteNoisePlaying = true;
        ((ImageView) view).setImageResource(R.drawable.whitenoisepressed);
        isPlaying++;
    }
}

现在可以通过点击打开和关闭来控制音量。

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 1970-01-01
    • 2012-05-23
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多