@kezhen
2015-12-15T00:09:27.000000Z
字数 4630
阅读 8530
SystemSoundServices
后台音乐播放
打开后台模式的音乐播放,或者在info.plist文件中添加Required Background Modes
键,其值是App plays audio or streams audio/video using AirPlay
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
// 1.获取要播放音频文件的URL
NSURL *fileURL = [[NSBundle mainBundle]URLForResource:@"王力宏-流泪手心" withExtension:@".mp3"];
// 2.创建 AVAudioPlayer 对象
self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
// 3.打印歌曲信息
NSString *msg = [NSString stringWithFormat:@"音频文件声道数:%ld\n 音频文件持续时间:%g",self.audioPlayer.numberOfChannels,self.audioPlayer.duration];
NSLog(@"%@",msg);
// 4.设置循环播放
self.audioPlayer.numberOfLoops = -1;
self.audioPlayer.delegate = self;
// 5.开始播放
[self.audioplayer play];
利用
System Sound Services
只能播放一些很小的提示或警告音频,它存在诸多限制,例如:声音长度不能超过30秒,不能控制进度,格式支持少等。
调用 AudioServicesCreateSystemSoundID(CFURLRef inFileURL,SystemSoundID *outSystemSoundID)
该函数的第一个参数代表音频文件的URL(可通过NSURL转换成CFURLRef),第二个参数代表注册音频文件的SystemSoundID。
调用AudioServicesAddSystemSoundCompletion()
函数为制定SystemSoundID注册Callback函数。
调用AudioServicePlaySystemSound
函数或者AudioServicePlayAlertSound
(调用系统振动功能)。
#import "FKViewController.h"
static void completionCallback(SystemSoundID mySSID)
{
// Play again after sound play completion
AudioServicesPlaySystemSound(mySSID);
}
@implementation ViewController
// 音频文件的ID
SystemSoundID ditaVoice;
- (void)viewDidLoad
{
[super viewDidLoad];
// 1. 定义要播放的音频文件的URL
NSURL *voiceURL = [[NSBundle mainBundle]URLForResource:@"CleanDidFinish" withExtension:@"aiff"];
// 2. 注册音频文件(第一个参数是音频文件的URL 第二个参数是音频文件的SystemSoundID)
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(voiceURL),&ditaVoice);
// 3. 为crash播放完成绑定回调函数
AudioServicesAddSystemSoundCompletion(ditaVoice,NULL,NULL,(void*)completionCallback,NULL);
// 4. 播放 ditaVoice 注册的音频 并控制手机震动
AudioServicesPlayAlertSound(ditaVoice);
// AudioServicesPlaySystemSound(ditaVoice);
// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); // 控制手机振动
}
使用
System Sound Services
需要AudioToolbox
框架的支持,需要导入其主头文件:#import<AudioToolbox/AudioToolbox.h>
MPMusicPlayerController
控制本机音乐播放对控制内置音乐库播放状态以及媒体信息的获取进行了封装,主要用于公司手环端蓝牙发送指令控制手机音乐播放以及在手环荧幕上显示歌曲名称。
下面介绍一下用法:
1. 包含我封装的头文件#import "BackgroundMusic.h"
2. 遵守协议:achieveMusicInfoDelegate
3. 初始化:
@property (nonatomic,strong) BackgroundMusic *musicController;
self.musicController = [[BackgroundMusic alloc]init];
self.musicController.delegate = self; // 设置当前控制器为代理
4. 监听媒体曲目的改变:
// 在 - (void)viewDidLoad 方法中调用以下方面
[self.musicController monitorMediaItem];
// 实现代理方法
- (void)achieveachieveMusicInfo:(NSString *)songName andPlaybackStatus:(int)playStatus
{
if (1 == playStatus) {
NSLog(@"正在播放音乐中...");
}else if(0 == playStatus){
NSLog(@"音乐暂停中...");
}else{
NSLog(@"播放状态未知...");
}
NSLog(@"歌曲信息:%@",songName);
}
5. 播放控制
// 控制音乐播放、暂停
[self playOrPause];
// 下一曲
[self next];
MPMusicPlayerController
实例化对象来播放内置音乐库的媒体文件,有以下两种类方法来实例化对象:MPMusicPlayerController *playController = [MPMusicPlayerController systemMusicPlayer];
MPMusicPlayerController *playController = [MPMusicPlayerController applicationMusicPlayer];
NSNotFound
表示empty queue or an infinite playlist
if ([self.playController indexOfNowPlayingItem] == NSNotFound) {
return YES;
}else{
return NO;
}
query
创建媒体队列,创建成功后可以条用play
来播放(默认播放第 index = 0 首曲目)
/**
* 设置媒体播放队列
*/
- (void)createMediaQuery
{
// Creates a media query that matches music items and that groups and sorts collections by song name.
self.query = [MPMediaQuery songsQuery];
// Sets a music player’s playback queue based on a media query.
[self.playController setQueueWithQuery:self.query];
}
nowPlayingItem
取得一系列正在播放的曲目的详细信息:
@property (nonatomic, readonly) MPMediaEntityPersistentID
@property (nonatomic, readonly) MPMediaType mediaType
@property (nonatomic, readonly) NSString *title
@property (nonatomic, readonly) NSString *albumTitle
@property (nonatomic, readonly) MPMediaEntityPersistentID
@property (nonatomic, readonly) NSString *artist
@property (nonatomic, readonly) MPMediaEntityPersistentID
@property (nonatomic, readonly) NSString *albumArtist
...
/**
* 获取媒体信息
*/
- (void)obtainMusicInfo
{
MPMediaItem *currentItem = [self.playController nowPlayingItem];
NSString *artist = [currentItem valueForProperty:MPMediaItemPropertyArtist];
NSString *songName = [currentItem valueForProperty:MPMediaItemPropertyTitle];
self.musicInfo = [NSString stringWithFormat:@"%@-%@",artist,songName];
}
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(updateMusicInfo)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:nil];
[self.playController beginGeneratingPlaybackNotifications];