gpt4 book ai didi

ios - AudioServicesPlaySystemSound 不播放,除非我跨过

转载 作者:行者123 更新时间:2023-11-29 13:13:05 25 4
gpt4 key购买 nike

我正在编写一个闹钟应用程序,我需要让用户选择唤醒的铃声。我有一个 UITableView,其中包含我的声音列表,当用户点击我想要播放的声音时。

但是,我的声音不会播放,除非我越过对 AudioServicesPlaySystemSound 的调用(或者如果我在对 AudioServicesDisposeSystemSoundID 的调用上设置断点,它会在我听到声音后触发)。

我认为这是主线程问题,所以我使用了 performSelectorOnMainThread:

        [self performSelectorOnMainThread:@selector(playWakeUpSound)
withObject:nil
waitUntilDone:true];

但这并没有帮助。 (waitUntilDone 的值似乎无关紧要。)

This question很有希望,但我检查了一下,我只调用了我的方法一次。

最佳答案

这只是一个猜测,因为我不知道 playWakeUpSound 中有什么。如果您在该调用和 ARC 中使用 AVAudioPlayer,可能是因为它正在发布。将 AVAudioPlayer 的实例存储为类的成员,它应该播放。

@interface MyClass 
{
AVAudioPlayer *player;
}

@end

然后在实现中:

@implementation MyClass

- (void)playWakeUpSound {
// Assuming name and extension is set somewhere.
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:extension]];

NSError* error = nil;

player = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
[player play];
}
}

@end

关于ios - AudioServicesPlaySystemSound 不播放,除非我跨过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697560/

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