#import <AudioToolbox/AudioToolbox.h> AudioServicesPlaySystemSound(1106);
//Get the filename of the sound file: NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/jad0007a.wav"]; // declare a system sound id SystemSoundID soundID; //Get a URL for the sound file NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; //Use audio sevices to create the sound AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); //Use audio services to play the sound AudioServicesPlaySystemSound(soundID);
AudioServices - iPhone Development Wiki(http://iphonedevwiki.net/index.php/AudioServices)php
#import <AVFoundation/AVFoundation.h> NSURL* url = [[NSBundlemainBundle] URLForResource:@"five"withExtension:@"mp3"]; AVAudioPlayer* audioPlay = [[AVAudioPlayeralloc] initWithContentsOfURL:url error:nil]; [audioPlay play];
AVAudiosession是AVFoundation框架提供的一个单例类,能够用来为咱们的APP设置一个合适的音频环境。经过对他进行配置,咱们能够为本身的音乐播放APP设置合适的特征。ios
Category | 说明 | 场景 | 输入 | 输出 | 混合 | 听从静音 |
---|---|---|---|---|---|---|
AVAudioSessionCategoryAmbient | 能够与其余APP同时播放 | 背景音效 | NO | YES | YES | YES |
AVAudioSessionCategoryPlayback | 不可与其余APP同时播放 | 音乐播放器 | NO | YES | NO | NO |
AVAudioSessionCategoryOptionMixWithOthers | ||||||
AVAudioSessionCategorySoloAmbient | 默认,独占,设置后其余APP就中止播放 | NO | YES | NO | YES | |
AVAudioSessionCategoryRecord | YES | NO | NO | NO | ||
AVAudioSessionCategoryPlayAndRecord | YES | YES | NO | NO | ||
AVAudioSessionCategoryAudioProcessing | 使用硬件解码器处理音频,该音频会话使用期间,不能播放或录音 | |||||
AVAudioSessionCategoryMultiRoute | YES | YES | NO | NO |
[[AVAudioSession sharedInstance] setActive:YES error:nil];
手机上不止咱们一款APP,在听歌的时候,若是有人给咱们打电话;或者以前定的一个闹钟到时间了;或者使用了siri功能。这些会使用手机音频的应用,就会跟咱们的APP音频起冲突。称做音频中断。git
iOS系统的音频服务优先级高于第三方APP。当有电话进入、系统闹钟响起,都会致使第三方APP的audiosession中断。github
有两种方式来处理这种中断session
1.经过注册观察者来获取AVAudioSessionInterruptionNotification
事件的通知来响应中断的开始和结束。框架
2.设置AVAudioSession代理iphone
//设置代理 能够处理电话打进时中断音乐播放 [[AVAudioSession sharedInstance] setDelegate:self];
Media Player framework 加入了MPRemoteCommandCenter这个类。使用block类实现远程控制回调。url
要让APP支持RemoteControl,咱们须要用到MPRemoteCommandCenter单例类。它提供了处理 remote control events所须要的对象。它的属性中包括了众多MPRemoteCommand类对象,表明着iOS所支持的不一样类型的remote control evnt。为MPRemoteCommand对象添加target和action来响应其控制事件。.net
须要在UIApplication中实现remoteControlReceivedWithEvent来处理。代理
为了在锁屏界面和控制中心显示当前歌曲的信息,须要用到Media Player Framework
的MPNowPlayingInfoCenter
类。把须要显示的信息组织成Dictionary并赋值给nowPlayingInfo
属性就完成了。
一些常见的属性值以下:
// MPMediaItemPropertyAlbumTitle 专辑标题 // MPMediaItemPropertyAlbumTrackCount 声道个数 // MPMediaItemPropertyAlbumTrackNumber 左右声道 // MPMediaItemPropertyArtist 艺术家(歌曲做者) // MPMediaItemPropertyArtwork 锁屏界面的封面 // MPMediaItemPropertyComposer // MPMediaItemPropertyDiscCount // MPMediaItemPropertyDiscNumber // MPMediaItemPropertyGenre // MPMediaItemPropertyPersistentID // MPMediaItemPropertyPlaybackDuration 播放时长 // MPMediaItemPropertyTitle
除了上面的Item Property,还有一些播放信息的属性值,Playing Info Property,其中须要特别注意的是MPNowPlayingInfoPropertyPlaybackRate
和MPNowPlayingInfoPropertyElapsedPlaybackTime
。前者表示播放速率,后者表示已播放时间,即上图中进度条左边的时间。当设置了这两个值,而且当前正在播放,就会自动地根据前一个时间和播放速率来计算。
在iOS11以前,当歌曲暂停时,其实该值仍是在增长。为了保证该值的精确,须要在暂停和从新开始播放时,从新设置MPNowPlayingInfoPropertyPlaybackRate的值。
特别在iOS 11时,咱们须要指定playbackState
的值,当不是MPNowPlayingPlaybackStatePlaying
时,并不会在锁屏界面显示当前播放信息。
参考1:http://www.samirchen.com/ios-avaudiosession-3/
参考2:http://msching.github.io/blog/2014/11/06/audio-in-ios-8/