AVPlayer audio player basic implementation

#import <AVFoundation/AVFoundation.h>

@property (nonatomic, strong) AVPlayer *player;

  • play
- (void)playWithURL:(NSURL *)url { // 1. AVURLAsset asset = [AVURLAsset assetWithURL:url]; / / 2. The resources of the organization AVPlayerItem * item = [AVPlayerItem playerItemWithAsset: asset]; // When the resource organizer tells us that the resource is ready, Let's play / / AVPlayerItemStatus status [item addObserver: self forKeyPath: @ "status" options: NSKeyValueObservingOptionNew context:nil]; / / 3. Resources play self. Player = [AVPlayer playerWithPlayerItem: item]; }Copy the code
  • suspended
- (void)pause {
    [self.player pause];
}
Copy the code
  • Continue to
- (void)resume {
    [self.player play];
}
Copy the code
  • stop
- (void)stop {
    [self.player pause];
    self.player = nil;
}
Copy the code
  • Progress bar pull
- (void)seekWithProgress:(float)progress { if (progress < 0 || progress > 1) { return; } // you can specify the time node to play // time: CMTime: movie time // movie time -> second // second -> movie time // 1. The current audio resources total duration CMTime totalTime = self. Player. CurrentItem. Duration; / / 2. The current audio, has played the length of the / / self. Player. CurrentItem. CurrentTime NSTimeInterval totalSec = CMTimeGetSeconds (totalTime); NSTimeInterval playTimeSec = totalSec * progress; CMTime currentTime = CMTimeMake(playTimeSec, 1); [self.player seekToTime:currentTime completionHandler:^(BOOL finished) {if (finished) {NSLog(@" Make sure to load the audio resource at this time ");}else {NSLog(@" cancel loading of audio resources at this point in time ");}}]; }Copy the code
  • Fast forward a little bit
- (void)seekWithTimeDiffer:(NSTimeInterval)timeDiffer { // 1. The current audio resources total duration CMTime totalTime = self. Player. CurrentItem. Duration; NSTimeInterval totalTimeSec = CMTimeGetSeconds(totalTime); / / 2. The current audio, has played the length of the CMTime playTime = self. Player. CurrentItem. CurrentTime; NSTimeInterval playTimeSec = CMTimeGetSeconds(playTime); playTimeSec += timeDiffer; [self seekWithProgress:playTimeSec / totalTimeSec]; }Copy the code
  • Multiple broadcast
- (void)setRate:(float)rate {
    
    [self.player setRate:rate];
    
}
Copy the code
  • mute
- (void)setMuted:(BOOL)muted {
    self.player.muted = muted;
}
Copy the code
  • Adjust the volume
- (void)setVolume:(float)volume {
    
    if (volume < 0 || volume > 1) {
        return;
    }
    if (volume > 0) {
        [self setMuted:NO];
    }
    
    self.player.volume = volume;
}
Copy the code
#pragma mark - KVO -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { if ([keyPath isEqualToString:@"status"])  { AVPlayerItemStatus status = [change[NSKeyValueChangeNewKey] integerValue]; If (status = = AVPlayerItemStatusReadyToPlay) {NSLog (@ "resources ready, play at this time, no problem"); [self.player play]; }else {NSLog(@" status unknown "); }}}Copy the code