私はBluetoothセンサーから常に整数データを受信するアプリケーションを持っています。整数が50未満の場合、MP3を再生するようにしました。一度に1つのサウンドインスタンスを再生する
問題は、センサが非常に迅速に確認し、整数を送信しているため、オーディオインスタンスが多すぎるため、基本的にmp3ファイルが同時に多くの回数再生されているということです。もう一度やり直す前にオーディオを終了するにはどうすればいいですか?
これがメインのコードです:
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
// ----------------------------------------------
// set your custom boolean flag 'isPlayingAudio'
// to false so you can play another audio again
// ----------------------------------------------
}
...
-(void)monitorBluetoothNumber
{
if(bluetoothNumber < 50 && !self.isPlayingAudio)
{
[self playMusic];
self.isPlayingAudio = YES;
}
}
:私はAVAudioPlayerは、オーディオの再生が完了したかどうかを確認するためのデリゲートメソッドを持っていると信じて
func playSound() {
guard let url = Bundle.main.url(forResource: "beep1", withExtension: "mp3") else {
print("url not found")
return
}
do {
/// this codes for making this app ready to takeover the device audio
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
/// change fileTypeHint according to the type of your audio file (you can omit this)
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3)
// no need for prepareToPlay because prepareToPlay is happen automatically when calling play()
player!.play()
} catch let error as NSError {
print("error: \(error.localizedDescription)")
}
}