// ViewController.m
// 02-音乐播放
//
// Created by apple on 14/11/7.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "HMAudioTool.h"
@interface ViewController ()
// 播放器
@property (nonatomic, strong) AVAudioPlayer *player;
// 播放
- (IBAction)playMusic:(id)sender;
// 暂停
- (IBAction)pauseMusic:(id)sender;
// 停止
- (IBAction)stopMusic:(id)sender;
// 下一首
- (IBAction)nextMusic:(id)sender;
// 保存所有的音乐
@property (nonatomic, strong) NSArray *musics;
// 记录当前播放音乐的索引
@property (nonatomic, assign) int currentIndex;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%s", __func__);
/*
0.加载音乐文件
NSURL *url = [[NSBundle mainBundle] URLForResource:@"简单爱.mp3" withExtension:nil];
1.创建音乐播放器
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.player = player;
2.缓冲音乐文件(准备)
[player prepareToPlay];
3.播放音乐
[player play];
*/
[self.player play];
}
/*
- (AVAudioPlayer *)player
{
if (!_player)
{
0.加载音乐文件
NSURL *url = [[NSBundle mainBundle] URLForResource:@"简单爱.mp3" withExtension:nil];
1.创建音乐播放器
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
2.缓冲音乐文件(准备)
[_player prepareToPlay];
}
return _player;
}
*/
- (IBAction)playMusic:(id)sender {
// 播放器的url时readonly的, 所以不能设置, 也就是说一个播放器对应一首歌
// self.player.url = nil;
// 播放
// [self.player play];
[HMAudioTool playMusicWithFilename:self.musics[self.currentIndex]];
}
- (IBAction)pauseMusic:(id)sender {
// 暂停
// [self.player pause];
[HMAudioTool pauseMusicWithFilename:self.musics[self.currentIndex]];
}
- (IBAction)stopMusic:(id)sender {
// 停止
#warning 注意: 如果播放器销毁, 播放器就不可以用了,因为如果播放器销毁后就不可控制器了. 建议每次调用播放器的销毁方法之后都清空播放器
// [self.player stop];
// self.player = nil;
[HMAudioTool stopMusicWithFilename:self.musics[self.currentIndex]];
}
- (IBAction)nextMusic:(id)sender {
// 下一首
// 1.递增索引
int nextIndex = self.currentIndex + 1;
// 3.判断是否越界
if (nextIndex >= self.musics.count) {
nextIndex = 0;
}
NSLog(@"当前 %d 下一首 %d", self.currentIndex, nextIndex);
// 4.播放
// 停止上一首播放
[self stopMusic: nil];
self.currentIndex = nextIndex;
// 播放下一首
[self playMusic:nil];
}
#pragma mark - 懒加载
- (NSArray *)musics
{
if (!_musics) {
_musics = @[@"最佳损友.mp3", @"心碎了无痕.mp3", @"瓦解.mp3", @"简单爱.mp3"];
}
return _musics;
}
@end
//
// HMAudioTool.m
// 01-音效播放
//
// Created by apple on 14/11/7.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HMAudioTool.h"
#import <AVFoundation/AVFoundation.h>
@implementation HMAudioTool
static NSMutableDictionary *_soundIDs;
static NSMutableDictionary *_players;
+ (NSMutableDictionary *)soundIDs
{
if (!_soundIDs) {
_soundIDs = [NSMutableDictionary dictionary];
}
return _soundIDs;
}
+ (NSMutableDictionary *)players
{
if (!_players) {
_players = [NSMutableDictionary dictionary];
}
return _players;
}
+ (void)playAudioWithFilename:(NSString *)filename
{
// 0.判断文件名是否为nil
if (filename == nil) {
return;
}
// 1.从字典中取出音效ID
SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue];
// 判断音效ID是否为nil
if (!soundID) {
NSLog(@"创建新的soundID");
// 音效ID为nil
// 根据文件名称加载音效URL
NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
// 判断url是否为nil
if (!url) {
return;
}
// 创建音效ID
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
// 将音效ID添加到字典中
[self soundIDs][filename] = @(soundID);
}
// 播放音效
AudioServicesPlaySystemSound(soundID);
}
+ (void)disposeAudioWithFilename:(NSString *)filename
{
// 0.判断文件名是否为nil
if (filename == nil) {
return;
}
// 1.从字典中取出音效ID
SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue];
if (soundID) {
// 2.销毁音效ID
AudioServicesDisposeSystemSoundID(soundID);
// 3.从字典中移除已经销毁的音效ID
[[self soundIDs] removeObjectForKey:filename];
}
}
// 根据音乐文件名称播放音乐
+ (void)playMusicWithFilename:(NSString *)filename
{
// 0.判断文件名是否为nil
if (filename == nil) {
return;
}
// 1.从字典中取出播放器
AVAudioPlayer *player = [self players][filename];
// 2.判断播放器是否为nil
if (!player) {
NSLog(@"创建新的播放器");
// 2.1根据文件名称加载音效URL
NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
// 2.2判断url是否为nil
if (!url) {
return;
}
// 2.3创建播放器
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 2.4准备播放
if(![player prepareToPlay])
{
return;
}
// 允许快进
player.enableRate = YES;
player.rate = 3;
// 2.5将播放器添加到字典中
[self players][filename] = player;
}
// 3.播放音乐
if (!player.playing)
{
[player play];
}
}
// 根据音乐文件名称暂停音乐
+ (void)pauseMusicWithFilename:(NSString *)filename
{
// 0.判断文件名是否为nil
if (filename == nil) {
return;
}
// 1.从字典中取出播放器
AVAudioPlayer *player = [self players][filename];
// 2.判断播放器是否存在
if(player)
{
// 2.1判断是否正在播放
if (player.playing)
{
// 暂停
[player pause];
}
}
}
// 根据音乐文件名称停止音乐
+ (void)stopMusicWithFilename:(NSString *)filename
{
// 0.判断文件名是否为nil
if (filename == nil) {
return;
}
// 1.从字典中取出播放器
AVAudioPlayer *player = [self players][filename];
// 2.判断播放器是否为nil
if (player) {
// 2.1停止播放
[player stop];
// 2.2清空播放器
// player = nil;
// 2.3从字典中移除播放器
[[self players] removeObjectForKey:filename];
}
}
@end