2013-10-20 124 views
3

我想爲iOS 7(AVPlayer)製作一個簡單的Radio播放器,但我不知道如何使用AudioSession API。有一些教程,但那些教程是針對iOS 6或以下。iOS 7背景音頻,AudioSession

有人可以發佈一個片段或鏈接到iOS 7 AV教程嗎?

回答

2

當然。這將設置您的音頻會話進行播放,並啓用與其他音頻的混音,然後激活會話。這是使用新的Objective-C API,而不是您在所有示例中看到的舊的基於C的API。

如果您想通過AirPlay和控制中心接收遠程控制事件和/或顯示專輯/歌曲信息,則無法啓用與其他應用程序的混音選項,因此在您的情況下可能需要省略該選項字典。

NSError *audioError = nil; 
AVAudioSession *session = [AVAudioSession sharedInstance]; 
if(![session setCategory:AVAudioSessionCategoryPlayback 
      withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&audioError]) { 
    NSLog(@"[AppDelegate] Failed to setup audio session: %@", audioError); 
} 
[session setActive:YES error:&audioError]; 

其他一些提示 - 確保您將音頻添加到您的info.plist文件中的UIBackgroundModes鍵以允許背景音頻播放。

如果你想遙控事件(通過控制中心,耳機,藍牙,AirPlay的等),然後調用

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

,並把這個在您的應用程序代理:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event 
{ 
    if(event.type == UIEventTypeRemoteControl) 
    { 
     switch(event.subtype) 
     { 
      case UIEventSubtypeRemoteControlPause: 
      case UIEventSubtypeRemoteControlStop: 
       break; 
      case UIEventSubtypeRemoteControlPlay: 
       break; 
      default: 
       break; 
     } 
    } 
}