iOS 音频开发AVAudioPlayer的使用,音效播放AudioServicesPlaySystemSound使用

音频开发AVAudioPlayer的使用
AVAudioPlayer是iOS系统自带的可以播放本地mp3文件的一个类,(注意:只能播放本地)
参考:http://www.jianshu.com/p/589999e53461
1,先导入库AVFoundation/AVFoundation.h 并进行代理AVAudioPlayerDelegate
2,把将要播放的mp3文件导入工程
下面贴代码:
定义需要用到的全局变量
@property (nonatomic , strong) AVAudioPlayer *player;
@property (weak, nonatomic) IBOutlet UIProgressView *Progress;
@property (weak, nonatomic) IBOutlet UILabel *CurrentTime;
@property (weak, nonatomic) IBOutlet UILabel *countTime;
@property (nonatomic,strong)NSArray *sourceArray;//歌曲数组
@property (nonatomic,copy)NSString *currentsong;
@property (nonatomic,strong)NSTimer *timer;//定时器

-(void)viewDidLoad {
[super viewDidLoad];

_sourceArray = @[@"muise",@"music2"];

NSUserDefaults *usedefault = [NSUserDefaults standardUserDefaults];
NSString *songStr = [usedefault objectForKey:@"current_song"];
if (songStr.length == 0) {

    songStr = _sourceArray[0];

    [usedefault setObject:@"0" forKey:@"current_song"];
    [usedefault synchronize];
}
NSString *str = _sourceArray[[songStr intValue]];

[self PlayMusicWith:str];

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[_timer setFireDate:[NSDate distantFuture]];


// Do any additional setup after loading the view, typically from a nib.

}

//暂停按钮
-(IBAction)Stop:(id)sender {

[self.player stop];
[_timer setFireDate:[NSDate distantFuture]];

}

//下一曲按钮
-(IBAction)Next:(id)sender {

 [self NextSong];

}

//上一曲按钮
-(IBAction)Previous:(id)sender {

NSUserDefaults *usedefault = [NSUserDefaults standardUserDefaults];
NSString *songStr = [usedefault objectForKey:@"current_song"];

int a = [songStr intValue]-1;

if (a <0) {

    a = (int)_sourceArray.count -1;
    [usedefault setObject:[NSString stringWithFormat:@"%d",a] forKey:@"current_song"];
    [usedefault synchronize];
}else{

    [usedefault setObject:[NSString stringWithFormat:@"%d",a]forKey:@"current_song"];
    [usedefault synchronize];
}


NSLog(@"上一曲a==== %d",a);

 [self PlayMusicWith:_sourceArray[a]];

}

//初始化播放器并播放
-(void)PlayMusicWith:(NSString *)songName{

NSURL *url = [[NSBundle mainBundle] URLForResource:songName withExtension:@"mp3"];
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
self.player.delegate = self;
 NSInteger countTime = self.player.duration;
_countTime.text = [NSString stringWithFormat:@"%d:%d",(int)countTime/60,(int)countTime%60];//歌曲总长度
[self.player play];
//设置锁屏仍能继续播放
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

}

//代理方法

//播放结束自动切换为下一首
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(@”播放结束”);
// 切换下一首

[self NextSong];

}

//中断
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
NSLog(@”一般这个方法中会暂停音频”);
// [player stop];
}

// 中断结束的时候会调用这个方法
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags {
// 一般在这个方法中继续播放音频
[player play];
NSLog(@”继续播放”);
}

// 解码错误的时候会调用这个方法
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer )player error:(NSError )error {
NSLog(@”文件出错”);
}

//下一首
-(void)NextSong{

NSUserDefaults *usedefault = [NSUserDefaults standardUserDefaults];
NSString *songStr = [usedefault objectForKey:@"current_song"];

int a = [songStr intValue]+1;

if (a >=_sourceArray.count) {

    a = 0;
    [usedefault setObject:@"0" forKey:@"current_song"];
    [usedefault synchronize];
}else{

    [usedefault setObject:[NSString stringWithFormat:@"%d",a]forKey:@"current_song"];
    [usedefault synchronize];
}

NSLog(@"下一曲a==== %d",a);

[self PlayMusicWith:_sourceArray[a]];

}

-(void)timerAction{

NSUInteger a = self.player.currentTime;

// _CurrentTime.text = [[NSString stringWithFormat:@”%.2f”,a/60.00,self.player.currentTime] stringByReplacingOccurrencesOfString:@”.” withString:@”:”];

NSLog(@"kkkk %ld  %f",a,self.player.currentTime);


int b = a%60;

if (b<10) {


}

int c = (int)a/60;

_CurrentTime.text =[NSString stringWithFormat:@"%d:%d",c,b];// [[NSString stringWithFormat:@"%",a/60.00] stringByReplacingOccurrencesOfString:@"." withString:@":"];
_Progress.progress = _player.currentTime/_player.duration;

}

音效播放AudioServicesPlaySystemSound使用
- (IBAction)Playmusic:(id)sender {

NSLog(@"音乐播放");

[self.player play];

[_timer setFireDate:[NSDate distantPast]];
 NSLog(@"hhhh %f",self.player.currentTime);

}

-(void)playSoundEffect:(NSString*)name {

//获取文件的路径
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:name ofType:nil];

NSURL *fileUrl = [NSURL fileURLWithPath:audioFilePath];

//1.获取系统声音ID
SystemSoundID soundID = 0;

AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);

//2.如何播放完之后需要执行某些操作,可以调用下面方法
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL,soundCompleteCallBack, NULL);

//3.播放音频
AudioServicesPlaySystemSound(soundID);

// 这个方法直接监听播放完成回调,方便了很多
AudioServicesPlaySystemSoundWithCompletion(soundID, ^{


    NSLog(@"播放完成回调");
});


AudioServicesPlayAlertSound(soundID); // 播放并且震动

}

void soundCompleteCallBack(SystemSoundID soundID,void* clientDate) {

NSLog(@"播放完成");

}
iOS 音频开发AVAudioPlayer的使用,音效播放AudioServicesPlaySystemSound使用_第1张图片

你可能感兴趣的:(音频播放器)