AVAudioPlayer how to play fast or slow file
I just need to listen to the file slowly or quickly. Any help will be greatly appreciate开发者_开发知识库d
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 :)
精彩评论