2013-10-20 130 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; 
     } 
    } 
}