【问题标题】:How Can I repeat a System Sound如何重复系统声音
【发布时间】:2012-09-27 08:14:36
【问题描述】:

我想重复发送到此方法的声音?

- (void) playSound:(CFURLRef)soundFileURLRef {
    SystemSoundID soundID;
    OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    if (errorCode != 0) {

    }else{
        AudioServicesPlaySystemSound(soundID);
    }
}

我已经看到了有关如何使用 numberOfRepeats 进行此操作的答案。但我无法将其放入我的代码中。

【问题讨论】:

    标签: ios audio system-sounds


    【解决方案1】:

    尝试使用AVAudioPlayer

    - (void) playSound:(CFURLRef)soundFileURLRef {
        NSError* sndErr;        
        AVAudioPlayer *player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:(NSURL *)soundFileURLRef error:(&sndErr) ];
    
        // at this point player has a retain count of 1. you should save it 
        // somewhere like a class member variable so you can stop the playing
        // and release the object later. Under ARC, you must have a strong reference
        // to the object so that it doesn't get released when this function goes out
        // of scope.
    
        if (sndErr != nil) {
            player.numberOfLoops = -1; // infinite number of loops. call stop when you want to stop.
            [player play];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-12
      • 2011-09-11
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多