1.ios中系统播放音频的方式:
a AVAudioPlayer 使用简单方便,但只能播放本地音频,不支持流媒体播放;
b.AVPlayer ios4.0以后,可以使用他播放本地音频和流媒体音频,但方法比较少,处理起来不太灵活;
c.播放系统声音 播放一些caf / wav / aiff格式,时间必须小于30秒
d.音频队列
2.使用AVAudioPlayer和AVPlayer需要倒入AVFoundtion类库
例:
NSError *error;
//设置音频会话支持后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.delegate = self;
//准备播放
[audioPlayer prepareToPlay];
// if ([audioPlayer play]) {
// NSLog(@"开始播放音乐了");
// }
float vlume = audioPlayer.volume;//音量
audioPlayer.numberOfLoops = 3;//播放次数,默认播放一次
float duration = audioPlayer.duration;//总时长
audioPlayer.currentTime = 100;//播放的位置
audioPlayer.prepareToPlay;//准备播放,缓冲
audioPlayer.play;//播放
audioPlayer.pause;//暂停
audioPlayer.stop;//停止
AVAudioPlayer的常用代理方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
//播放结束
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
//解码错误
}
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
//处理中断的代码
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
//处理中断结束的代码
}
3.AVPlayer使用代码示例:
//播放远程地址
NSString *mp3Url = @"http://zhangmenshiting.baidu.com/data2/music/32197650/23473715212400128.mp3?xcode=048460eee5f7d0205f13a00e1cf710fe";
NSURL *url = [NSURL URLWithString:mp3Url];
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
[player play];
//播放本地文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
[player play];
4.播放系统声音,需要导人AudioToolbox.framework库
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"44th Street Medium" ofType:@"caf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
// SystemSoundID soundId;
unsigned long soundId;
//为url地址注册系统声音
AudioServicesCreateSystemSoundID((CFURLRef)url, &soundId);
//播放系统声音
AudioServicesPlaySystemSound(soundId);
//播放震动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
#define NeedAudio
#ifdef NeedAudio
// 音效
SystemSoundID _normalId;
#endif
#ifdef NeedAudio
// 加载音频
_normalId = [self loadId:@"normal.wav"];
#endif
#ifdef NeedAudio
#pragma mark 加载音效id
- (SystemSoundID)loadId:(NSString *)filename
{
SystemSoundID ID;
NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [bundle URLForResource:kSrcName(filename) withExtension:nil];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &ID);
return ID;
}
#endif
#ifdef NeedAudio
AudioServicesPlaySystemSound(_normalId);
#endif
5.后台播放任务
当你的应用程序在后台(被挂起)时,ios系统(4.0以后)只允许你做三件事情:播放音频(audio),位置信息(定位),voip(网络电话);
在后台播放音频分两步:a.在plist文件中添加UIBackgroundMode属性,值为audio;b.设置AVAudioSession模式,播放音频时将其属性设置为AVAudioSessionCategoryPlayback(音频播放之前设置);
NSError *error;
//设置音频会话支持后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
自定义后台任务:
自定义后台任务的活跃时间只有600秒;
#pragma mark应用进入后台时执行
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//开启一个后台任务
taskId = [application beginBackgroundTaskWithExpirationHandler:^{
//结束指定的任务
[application endBackgroundTask:taskId];
}];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)timer {
count++;
//当执行500s后停止之前的后台任务,开启新的后台任务,这样可以无限时执行后台任务
if (count % 500 == 0) {
UIApplication *application = [UIApplication sharedApplication];
//结束旧的后台任务
[application endBackgroundTask:taskId];
//开启一个新的后台
taskId = [application beginBackgroundTaskWithExpirationHandler:NULL];
}
NSLog(@"%d",count);
}