#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/