1

我在我的应用程序中使用 AVAudioPlayer 加载了基本的哔声。

如果我的手指没有平移我的 MKMapView,它可以很好地播放哔声。

我的哔声设置为每 2 秒播放一次。

当我开始平移地图视图并且手指没有离开屏幕时,哔声停止播放。

我记得 NSUrlConnection 在滚动表格视图时也不会触发,我认为这可能是同一个问题,但我无法弄清楚如何将我的音频播放器添加到正确的运行循环中。

我这样设置我的播放器:

-(void)setupBeep
{
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/beep.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    if(error)
    {
        NSLog(@"Error opening sound file: %@", [error localizedDescription]);
    }
}

我正在播放这样的声音:

// plays a beeping sound
-(void)beep
{
    [audioPlayer play];
}

以前有人遇到过这个问题吗?

4

1 回答 1

1

你是如何安排你的-beep方法被调用的?

我怀疑你NSTimer在默认输入模式下添加了一个主运行循环。您没有听到任何声音的原因是在MKMapView跟踪您的手指时,运行循环不在NSDefaultRunLoopMode- 它在UITrackingRunLoopMode.

尝试在 中进行调度NSRunLoopCommonModes,其中包括默认模式和跟踪模式:

NSTimer *timer = [NSTimer timerWithTimeInterval:interval target:self selector:@selector(beep) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

另外,请注意NSTimer保留其目标。重复计时器会自动重新安排自己的时间,因此您需要调用其-invalidate方法将其从循环中移除并允许释放目标。

编辑:查看 Apple 的线程编程指南:运行循环以获得更详细的解释。

于 2012-11-24T04:00:49.433 回答