AVAudioPlayer rate

后端 未结 4 2084
孤独总比滥情好
孤独总比滥情好 2021-01-05 17:41

So I\'m trying to play a sound file at a different rate in iOS 5.1.1, and am having absolutely no luck. So far I have tried setting the rate of the AVAudioPlayer:

         


        
相关标签:
4条回答
  • 2021-01-05 17:53

    This is how you do it.

    rate value is between 0.1f - 2.0f

    player = [[AVAudioPlayer alloc]
              initWithContentsOfURL:[NSURL fileURLWithPath:path]
              error:&err];
    player.volume = 0.4f;
    player.enableRate=YES;
    [player prepareToPlay];
    [player setNumberOfLoops:0];
    player.rate=2.0f;
    [player play];
    
    0 讨论(0)
  • 2021-01-05 18:01

    here is some code that i know works, just re-tested in an app i've been working on. as you mention, using setEnableRate: and setRate: will only work with iOS 5.0 and above. so i use respondsToSelector: to test on the device whether or not the device will accept the request:

    _noticeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Rooster-mono" ofType:@"wav"]]
                                                          error:nil];;
    if ([_noticeAudio respondsToSelector:@selector(setEnableRate:)])
        _noticeAudio.enableRate = YES;
    if ([_noticeAudio respondsToSelector:@selector(setRate:)])
        _noticeAudio.rate = 2.0;
    

    running on an iOS 5 device, it performs the double-rate successfully. running on iOS 4.3, it plays it at normal speed.

    so, the only way you'll get the proper rate is if your device has iOS 5 on it.

    0 讨论(0)
  • 2021-01-05 18:05

    Swift 2.0

    let player = AVPlayer(URL: url)
    player.play()
    player.rate = 0.9
    
    0 讨论(0)
  • 2021-01-05 18:08

    Modify the code to:

    avPlayer = [[AVPlayer alloc] initWithURL:referenceURL];
    
    [avPlayer play]; //call play first
    
    avPlayer.rate = 0.5; //then set rate
    
    0 讨论(0)
提交回复
热议问题