iOS:如何在我的音乐应用中播放蓝牙耳机中的缓冲音频?

时间:2015-04-20 06:23:14

标签: ios xcode audio bluetooth airplay

我目前正在做一个音乐应用。因为我需要玩缓冲  蓝牙耳机的音频歌曲。我搜索了过去1天的代码。但  我不能。请给出解决方案,如何在我的音乐应用程序中添加它。

当iphone处于背景模式和屏幕锁定模式时,还需要在蓝牙耳机中播放音频。

   AVAudioSession* audioSession = [AVAudioSession sharedInstance];
//[audioSession setDelegate:self];
NSError *error;
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];
[audioSession setActive: YES error: nil];


// check the audio route
UInt32 size = sizeof(CFStringRef);
CFStringRef route;

OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
// if bluetooth headset connected, should be "HeadsetBT"
// if not connected, will be "ReceiverAndMicrophone"

// now, play a quick sound we put in the bundle (bomb.wav)
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;
soundFileURLRef  = CFBundleCopyResourceURL (mainBundle,CFSTR ("sample"),CFSTR ("m4a"),NULL);
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlaySystemSound (soundFileObject);     // should play into headset

我在我的音乐应用中添加了此代码。但此代码适用于Bundle音频文件。但我需要缓冲音频。如果不可能请告诉我的代码是否正确通过蓝牙耳机播放本地音频文件。?

1 个答案:

答案 0 :(得分:0)

根据我在文档中阅读的内容,设置音频会话的类别应为

  

AVAudioSessionCategoryPlayAndRecord

  

AVAudioSessionCategoryRecord

如果将选项设置为

 AVAudioSessionCategoryOptionAllowBluetooth

所以你应该改变这一点,当我看到枚举时另一件事:AVAudioSessionCategoryOptions蓝牙的选项是__TVOS_PROHIBITED,但我不知道替代方案......