iOS開發之 AVAudioPlayer音訊播放
阿新 • • 發佈:2019-02-08
// // ViewController.m #import "ViewController.h" #import "AudioTool.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () /** 播放器 */ @property (nonatomic, strong) AVAudioPlayer *player; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)otherMusic { [AudioTool playMusicWithMusicName:@"120125029.mp3"]; /* NSURL *url = [[NSBundle mainBundle] URLForResource:@"120125029.mp3" withExtension:nil]; [self.player setValue:url forKeyPath:@"url"]; [self.player play]; */ } - (IBAction)play { /* // 取出資源的URL NSURL *url = [[NSBundle mainBundle] URLForResource:@"1201111234.mp3" withExtension:nil]; // 建立播放器 NSError *error = nil; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; // 準備播放 [player prepareToPlay]; */ // 播放歌曲 // [self.player play]; [AudioTool playMusicWithMusicName:@"1201111234.mp3"]; } - (IBAction)pause { // [self.player pause]; [AudioTool pauseMusicWithMusicName:@"1201111234.mp3"]; } - (IBAction)stop { // [self.player stop]; // self.player = nil; [AudioTool stopMusicWithMusicName:@"1201111234.mp3"]; } //#pragma mark - 懶載入 //- (AVAudioPlayer *)player //{ // if (_player == nil) { // // 取出資源的URL // NSURL *url = [[NSBundle mainBundle] URLForResource:@"1201111234.mp3" withExtension:nil]; // // // 建立播放器 // NSError *error = nil; // self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; // // // 準備播放 // [self.player prepareToPlay]; // } // // return _player; //} @end
// // XMGAudioTool.m // #import "AudioTool.h" #import <AVFoundation/AVFoundation.h> @implementation AudioTool static NSMutableDictionary *_soundIDs; static NSMutableDictionary *_players; + (void)initialize { _soundIDs = [NSMutableDictionary dictionary]; _players = [NSMutableDictionary dictionary]; } + (void)playMusicWithMusicName:(NSString *)musicName { assert(musicName); // 1.定義播放器 AVAudioPlayer *player = nil; // 2.從字典中取player,如果取出出來是空,則對應建立對應的播放器 player = _players[musicName]; if (player == nil) { // 2.1.獲取對應音樂資源 NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil]; if (fileUrl == nil) return; // 2.2.建立對應的播放器 player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil]; // 2.3.將player存入字典中 [_players setObject:player forKey:musicName]; // 2.4.準備播放 [player prepareToPlay]; } // 3.播放音樂 [player play]; } + (void)pauseMusicWithMusicName:(NSString *)musicName { assert(musicName); // 1.取出對應的播放 AVAudioPlayer *player = _players[musicName]; // 2.判斷player是否nil if (player) { [player pause]; } } + (void)stopMusicWithMusicName:(NSString *)musicName { assert(musicName); // 1.取出對應的播放 AVAudioPlayer *player = _players[musicName]; // 2.判斷player是否nil if (player) { [player stop]; [_players removeObjectForKey:musicName]; player = nil; } } #pragma mark - 音效的播放 + (void)playSoundWithSoundname:(NSString *)soundname { // 1.定義SystemSoundID SystemSoundID soundID = 0; // 2.從字典中取出對應soundID,如果取出是nil,表示之前沒有存放在字典 soundID = [_soundIDs[soundname] unsignedIntValue]; if (soundID == 0) { CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundname withExtension:nil]; if (url == NULL) return; AudioServicesCreateSystemSoundID(url, &soundID); // 將soundID存入字典 [_soundIDs setObject:@(soundID) forKey:soundname]; } // 3.播放音效 AudioServicesPlaySystemSound(soundID); } @end