objective-c – 如何停止AudioToolbox声音?
发布时间:2020-12-14 17:31:53 所属栏目:百科 来源:网络整理
导读:我有两个不同的按键声音动作.当我按下一个按钮时它应该播放一个声音,当我按下另一个声音时,它应该停止第一个声音然后播放另一个声音,我该怎么做? 谢谢, -(void) onButtonPressAlbina { [soundID2 stop]; NSString *soundPath = [[NSBundle mainBundle] path
我有两个不同的按键声音动作.当我按下一个按钮时它应该播放一个声音,当我按下另一个声音时,它应该停止第一个声音然后播放另一个声音,我该怎么做?
谢谢, -(void) onButtonPressAlbina { [soundID2 stop]; NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"albina" ofType:@"m4a"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath],&soundID); AudioServicesPlaySystemSound (soundID); } -(void) onButtonPressBalena { NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"balena" ofType:@"m4a"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath],&soundID); AudioServicesPlaySystemSound (soundID); } 解决方法
你无法阻止通过“AudioServicesPlaySystemSound”播放的声音,但你可以做的是使用“AVAudioPlayer”.
在对象中保留对“AVAudioPlayer”的引用,然后在需要暂停时可以在其上调用“ 玩 #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVAudioPlayer.h> AVAudioPlayer *player; // ... NSString *path; NSError *error; path = [[NSBundle mainBundle] pathForResource:@"albina" ofType:@"m4a"]; if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; player.volume = 0.5f; [player prepareToPlay]; [player setNumberOfLoops:0]; [player play]; } 停止 if (player != nil) { if (player.isPlaying == YES) [player stop]; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |