【发布时间】:2015-04-20 06:23:14
【问题描述】:
我目前正在开发一款音乐应用。因为我需要玩缓冲 蓝牙耳机中的音频歌曲。我过去 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
我已在我的音乐应用中添加了此代码。但此代码用于捆绑音频文件。但我需要缓冲音频。如果不可能,请告诉我的代码是否正确,可以通过蓝牙耳机播放本地音频文件。?
【问题讨论】:
-
请发布您尝试过的任何内容,即使它是对蓝牙播放的非缓冲内容,或者是对蓝牙不播放的缓冲内容。发布你所拥有的一切。
-
@GHC 我编辑了我的帖子。请检查并更新我。谢谢。
-
顺便说一句,您可以在techslides.com/sample-files-for-development找到示例文件
-
@raja 你是否在 info.plist 中添加了关于播放音频背景模式的权限?
标签: ios xcode audio bluetooth airplay