【问题标题】:AudioServicesPlaySystemSound not playing except when I step overAudioServicesPlaySystemSound 不播放,除非我跨过
【发布时间】:2013-05-22 16:58:57
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    这只是一个猜测,因为我不知道 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
    

    【讨论】:

    • 就是这样。我被ARC'd。有趣的是,我正在使用 Apple SoundEffect.m 示例代码,并将 NSLog 放入 play 和 dealloc 方法中,并且它们被以正确的顺序调用!感谢您的快速回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    相关资源
    最近更新 更多