Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmur/bca0d281a6c68b5f3b30 to your computer and use it in GitHub Desktop.
Save dmur/bca0d281a6c68b5f3b30 to your computer and use it in GitHub Desktop.
- (AVAudioPlayer *)playWaveformNamed:(NSString *)fileName
withCompletionHandler:(void (^)(void))completionHandler
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil];
audioPlayer.delegate = self;
[audioPlayer play];
AVURLAsset *audioAsset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
CMTime audioDuration = audioAsset.duration;
float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(audioDurationSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (completionHandler) completionHandler();
});
return audioPlayer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment