//導(dǎo)入媒體播放器工具
(#import AVFoudation.framework)
{
//聲明一個音頻播放器
AVAudioPlayer *_player;
NSArray *_nameArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
_nameArray= @[@"最初的夢想",@"時間煮雨",@"可不可以不勇敢"];
//通過文件名和類型獲取音頻文件路徑地址
// ???Bundle捆,束 学密。mainBundle系統(tǒng)資源庫
NSString *path = [[NSBundle mainBundle] pathForResource:@"時間煮雨" ofType:@"mp3"];
//將字符串路徑轉(zhuǎn)化為URL路徑
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
//創(chuàng)建播放器
//第一個參數(shù):音頻文件地址
//第二個參數(shù):獲取錯誤信息
_player= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//重復(fù)次數(shù),-1為單曲循環(huán)
_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;
}
}