Hello, I am Two west, today mainly record AVAudioPlayer local play video, this control can play local audio, if you want to play online audio, please use AVPlayer. Of course, keep tweeting haha ~ long confession y.qq.com/n/yqq/song/…

Initialize the

 NSURL *urlPath = [NSURL fileURLWithPath:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
 AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:nil];
 
Copy the code

Set from the mute control: [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

Pit: AVAudioPlayer needs to be associated with global variables, otherwise it will not play

operation

//-1, loop play 0, 1, 2... _player.numberOfLoops = -1; // volume 0-1 _player.volume = vol; // Set time [_player setCurrentTime:0]; // Whether _player.playing is playing; // pause [_player pause]; // play [_player play]; // End [_player stop];Copy the code

Interrupt notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruptionNotification:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]]; # pragma mark - phone interrupt - (void) interruptionNotification notification: (NSNotification *) {/ * Listening to interrupt event notification, AVAudioSessionInterruptionOptionKey typedef NS_ENUM (NSUInteger, AVAudioSessionInterruptionType) { AVAudioSessionInterruptionTypeBegan = 1, Interrupts start AVAudioSessionInterruptionTypeEnded = 0, end} * / NSDictionary * info = notification in the userInfo. AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; If (type = = AVAudioSessionInterruptionTypeBegan) {/ / break / / pause [_player pause]; }else{dispatch_after(dispatch_time(DISPATCH_TIME_NOW), (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [_player play]; }); }}Copy the code