gpt4 book ai didi

objective-c - 如何停止 AudioToolbox 的声音?

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:07 24 4
gpt4 key购买 nike

我有两种不同的按钮按下声音 Action 。当我按下一个按钮时,它应该播放一种声音,当我按下另一个按钮时,它应该停止第一种声音并播放另一种声音,我该怎么做?

谢谢,

-(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”的引用,然后您可以调用 "stop"当你需要停止它的时候。

播放

#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];
}

关于objective-c - 如何停止 AudioToolbox 的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15370584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com