AVAudioPlayer如何播放快速或慢速文件

时间:2011-09-19 18:27:00

标签: objective-c iphone avaudioplayer

我只需要慢慢地或快速地收听文件。任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

AVAudioplayer无法做到这一点,AVPlayer可以。

       // play audio file as NSURL *url;
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                                        forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:options];
    NSString *tracksKey = @"tracks";
    [urlAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
     ^{

         // Completion handler block.
         dispatch_async(dispatch_get_main_queue(),
                        ^{
                            NSError *error = nil;
                            AVKeyValueStatus status = [urlAsset statusOfValueForKey:tracksKey error:&error];

                            if (status == AVKeyValueStatusLoaded) {
                                self.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
                                [avPlayer play];

                                avPlayer.rate = 0.5;
                            }
                            else {
                                // You should deal with the error appropriately.
                                NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);

                            }

                        });
     }];

答案 1 :(得分:1)

谢谢,但我已经解决了以下问题:

安装cocos2d http://www.cocos2d-iphone.org/download

[[SimpleAudioEngine sharedEngine] playEffect:(NSString *)pitch(float32)pan(float32)gain(float32)]

简单而有效......

答案 2 :(得分:0)

在Swift 3.0中,它会像这样 -

    audioP = try! AVAudioPlayer(contentsOf: URL(fileURLWithPath: selectedPath), fileTypeHint: "caf")
    audioP.enableRate = true
    audioP.prepareToPlay()
    audioP.rate = 1.5
    audioP.play()

希望这有帮助:)