简体   繁体   中英

AVAudioPlayer how to play fast or slow file

I just need to listen to the file slowly or quickly. Any help will be greatly appreciated

AVAudioplayer can't do that, AVPlayer can.

       // 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]);

                            }

                        });
     }];

thanks but I have solved it as follows:

Install cocos2d http://www.cocos2d-iphone.org/download

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

its simple and works....

in Swift 3.0 it will be like this -

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

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM