Seekbar控制Soundpool,停止工作(Seekbar Controlling Soundpool, stops working)

编程入门 行业动态 更新时间:2024-10-28 05:23:49
Seekbar控制Soundpool,停止工作(Seekbar Controlling Soundpool, stops working)

所以我有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++; } }`

So I have 8 images, each starts a loop of 8 different sounds. I have a seekbar under each image to control the volume for that sound.

The seekbar works fine the first time the sounds starts, but when stopped and started again, the seekbar no longer controls the volume. What am I doing wrong? `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++; } }`

最满意答案

我想到了。 我需要一种方法来了解我是否已经在之前点击过按钮。 我将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++; } }

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

I figured it out. I needed a way of knowing if I had already tapped the button before or not. I set noiseStreaming to 0 initially and then checked it when the button was pressed. If it was 0 then I set the whiteNoiseId into noiseStreaming. Next time it wouldn't be 0 and would skip that bit -

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

The volume can now be controlled through clicks on and off.

更多推荐

本文发布于:2023-08-06 12:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   Soundpool   Seekbar   working   stops

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!