在重复循环中使用SoundPool,例如CountDownTimer

时间:2019-06-10 17:39:14

标签: android repeat countdowntimer soundpool

我想在CountDownTimer之类的重复循环中使用SoundPool每秒生成一次哔声,但是无论我做什么,哔声都会在14次哔声后停止。 CountDownTimer循环甚至可以运行30个或更多,并执行其他代码。我是Android的新手。谁能帮忙解决这个问题?

我希望至少运行15分钟,最好关闭应用程序。我尝试使用其他声音(在我的实际代码中)并将问题缩小到这个范围。

public class MainActivity extends AppCompatActivity {
    TextView cd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cd = findViewById(R.id.cd);

        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                cd.setText("seconds remaining: " + millisUntilFinished / 1000);
                playSoundPool(fourhundred);
            }

            public void onFinish() {
                cd.setText("done!");
            }
        }.start();
    }


    private void playSoundPool(int soundID) {
        int MAX_STREAMS = 20;
        int REPEAT = 0;
        SoundPool soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, REPEAT);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
                int priority = 0;
                int repeat = 0;
                float rate = 1.f; 
                AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                float streamVolumeCurrent =
                        mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
                float streamVolumeMax =
                        mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                float volume = streamVolumeCurrent / streamVolumeMax;
                // Play it
                soundPool.play(soundId, volume, volume, priority, repeat, rate);
            }
        });
        soundPool.load(this, soundID, 1);
    }
}
```````````

I expect the beep to repeat 30 times and stop, but it stops after 14 beeps; at the same time text is updated 30 times and stops.

~~~~~~~~~~~
I could capture the RUN window messages during this behavior. Please see below, the messages for 13th and 14th beep[here the "numero" is just a dummy variable that starts from 72 and goes up]. I believe this would provide some more insight:
~~~~~~~~~~~
W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 44100 Hz, output 48000 Hz
I/System.out: Numero: 84
I/OMXClient: MuxOMX ctor
D/AudioManager: getStreamVolume isRestricted mode = 0
W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 44100 Hz, output 48000 Hz
I/System.out: Numero: 85
I/OMXClient: MuxOMX ctor
D/AudioManager: getStreamVolume isRestricted mode = 0
W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 44100 Hz, output 48000 Hz
E/AudioTrack: AudioFlinger could not create track, status: -12
E/SoundPool: Error creating AudioTrack
I/System.out: Numero: 86

~~~~~~~~~
The same message is repeated here onwards.

0 个答案:

没有答案