你可以在下面看到我的代码。但首先,
你应该选择这个位目标 | 背景模式 | 来自 Capabilities 的音频、Airplay 和画中画。
// ViewController.m
// AVAudioPlayer
#import "ViewController.h"
@import AVFoundation;
@import MediaPlayer;
@interface ViewController ()
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) IBOutlet UISlider *volumeSlider;
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupAudio];
}
- (void) setupAudio {
NSError *error;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error != nil) {
NSAssert(error == nil, @"");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
if (error != nil) {
NSAssert(error == nil, @"");
}
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"SoManyTimes" withExtension:@"mp3"];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
if (error != nil) {
NSAssert(error == nil, @"");
}
//[self.audioPlayer setVolume:0.8];
self.audioPlayer.enableRate = YES;
[self.audioPlayer prepareToPlay];
[self.audioPlayer setVolume:self.volumeSlider.value];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterrupt:) name:AVAudioSessionInterruptionNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil];
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTarget:self action:@selector(playButtonPressed:)];
[commandCenter.stopCommand addTarget:self action:@selector(stopButtonPressed:)];
[commandCenter.pauseCommand addTarget:self action:@selector(stopButtonPressed:)];
}
- (void) audioRouteChanged:(NSNotification*)notification {
NSNumber *reason = (NSNumber*)[notification.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey];
switch ([reason integerValue]) {
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
[self stopButtonPressed:nil];
break;
default:
break;
}
}
- (void) audioInterrupt:(NSNotification*)notification {
NSNumber *interruptionType = (NSNumber*)[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey];
switch ([interruptionType integerValue]) {
case AVAudioSessionInterruptionTypeBegan:
[self stopButtonPressed:nil];
break;
case AVAudioSessionInterruptionTypeEnded:
{
if ([(NSNumber*)[notification.userInfo valueForKey:AVAudioSessionInterruptionOptionKey] intValue] == AVAudioSessionInterruptionOptionShouldResume) {
[self playButtonPressed:nil];
}
break;
}
default:
break;
}
}
- (IBAction)playButtonPressed:(id)sender {
BOOL played = [self.audioPlayer play];
if (!played) {
NSLog(@"Error");
}
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"CoverArt"]];
NSDictionary *songInfo = @{
MPMediaItemPropertyTitle:@"So Many Times",
MPMediaItemPropertyArtist:@"The Jellybricks",
MPMediaItemPropertyAlbumTitle:@"Soap Opera",
MPMediaItemPropertyArtwork:albumArt,
MPMediaItemPropertyPlaybackDuration:@(self.audioPlayer.duration),
MPNowPlayingInfoPropertyElapsedPlaybackTime:@(self.audioPlayer.currentTime),
};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
- (IBAction)stopButtonPressed:(id)sender {
[self.audioPlayer stop];
}
- (IBAction)volumeSliderChanged:(UISlider*)sender {
[self.audioPlayer setVolume:sender.value];
}
- (IBAction)rateSliderChanged:(UISlider*)sender {
[self.audioPlayer setRate:sender.value];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end