【问题标题】:iPhone / iOS : AVAudioPlayer PlayAtTime: not working in iOS 6iPhone / iOS:AVAudioPlayer PlayAtTime:不适用于 iOS 6
【发布时间】:2012-10-15 18:34:41
【问题描述】:

您好,我有许多 AVAudioPlayers,我使用 PlayAtTime 函数同步它们开始播放。自 4 以来,我的代码在每个 iOS 上都可以正常工作,只是现在它在 iOS 6 中无法正常工作。还有其他人遇到过这个问题吗?

代码:

//Mixers are AVAUdioPlayer instances loaded from before and are fine
NSTimeInterval shortStartDelay = 0.00;            // seconds
NSTimeInterval now = player.deviceCurrentTime

[miXer.music playAtTime:now + shortStartDelay];
[miXer.music2 playAtTime:now + shortStartDelay];
[miXer.music3 playAtTime:now + shortStartDelay];
[miXer.music4 playAtTime:now + shortStartDelay];
[miXer.music5 playAtTime:now + shortStartDelay];

注意:如果我使用播放功能,一切正常(当然混音器不同步除外),但 playAtTime 无法播放文件。

【问题讨论】:

    标签: iphone ios6 avaudioplayer


    【解决方案1】:

    您未来的时间必须大于deviceCurrentTime-playAtTime: 才能工作。文档如是说:https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

    您需要将shortStartDelay 设置为大于 0 的值。

    编辑:

    或者,您可能需要先致电prepareToPlay

    【讨论】:

    • 没有。引用内容为“您提供给时间参数的值必须大于或等于音频播放器的 deviceCurrentTime 属性的值”。我尝试直接输入大于 deviceCurrentTime 的时间,无论如何第一次运行时为 0。
    • 确实如此,但在 Apple 提供的示例中,他们使用了非零值 0.01。 Didja 为您的 shortStartDelay 尝试过这个?
    • @Zigglzworth:我认为您需要发布更多代码,以便我们了解您是如何设置AVAudioPlayer 等的。
    【解决方案2】:

    不知道你是否已经解决了这个问题。

    我遇到了同样的问题,但更简单,因为我只有一个 AVAudioPlayer 实例。当我的应用程序进入后台时,它会暂停播放声音,并且在激活时它应该从头开始播放声音。

    我所做的是设置 currentTime 属性并调用 play

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification object:nil queue:nil usingBlock:^(NSNotification *arg1) {
            [self pauseNarration];
        }];
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *arg1) {
            [self playNarration];
        }];
    }
    
    - (void)playNarration {
        if ([self isAudioPlayerSet]) {
            [_audioPlayer setCurrentTime:0.0];
            [_audioPlayer play];
        }
    }
    
    - (void)pauseNarration {
        if ([self isAudioPlayerSet]) {
            [_audioPlayer pause];
        }
    }
    

    【讨论】:

      【解决方案3】:

      我正在使用这个,因为原始 AVAudioPlayer 的 playAtTime 不能仅使用您想要开始播放的值偏移量。

      //自定义函数

      func playAtTime(time: NSTimeInterval) {
          mediaPlayer.pause()
          mediaPlayer.currentTime = time
          mediaPlayer.play()
      }
      

      这个流程对我来说效果很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多