//导入媒体播放器工具
(#import AVFoudation.framework)
{
//声明一个音频播放器
AVAudioPlayer *_player;
NSArray *_nameArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
_nameArray= @[@"最初的梦想",@"时间煮雨",@"可不可以不勇敢"];
//通过文件名和类型获取音频文件路径地址
// Bundle捆,束 。mainBundle系统资源库
NSString *path = [[NSBundle mainBundle] pathForResource:@"时间煮雨" ofType:@"mp3"];
//将字符串路径转化为URL路径
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
//创建播放器
//第一个参数:音频文件地址
//第二个参数:获取错误信息
_player= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//重复次数,-1为单曲循环
_player.numberOfLoops= -1;
//音量0 - 1
_player.volume= 0.5;
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
//播放
[_player play];
}
- (IBAction)next:(id)sender {
static int i = 0;
NSString *name =_nameArray[i];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
_player= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player play];
i ++;
if(i >=_nameArray.count) {
i = 0;
}
}