小编Tim*_* B.的帖子

iOS - 将一个音频流发送到内置扬声器,将另一个音频流发送到蓝牙HFP扬声器

我正在构建一个与物理蓝牙设备配合使用的闹钟应用程序.我需要将一个音频文件(从iTunes资料库中选择的一首歌)发送到内置扬声器,并将一个单独的音频文件同时发送到蓝牙设备(实际上是蓝牙HFP扬声器).

我完成这个的第一个想法是使用AVAudioSession新的AVAudioSessionCategoryMultiRoute,但iOS 7没有检测到我的扬声器作为可能的路线; 它会检测无论是内置扬声器 HFP的扬声器,但是它不会同时检测两种.检测两者需要同时发送两个文件.

我的第二个想法是使用AVAudioSessionCategoryPlayAndRecord(即使应用程序不需要麦克风)并使用overrideOutputAudioPort:AVAudioSessionPortOverrideSpeakeroverrideOutputAudioPort:AVAudioSessionPortOverrideNone喜欢这样:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"alarm"
                                                          ofType:@"m4a"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

self.bluetoothAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
                                                               error:nil];

self.bluetoothAudioPlayer.delegate = self;

[self.bluetoothAudioPlayer play];

NSError *error = nil;

NSURL *selectedSongURL = [self.selectedSong valueForProperty:MPMediaItemPropertyAssetURL];

NSLog(@"selectedSongURL: %@", selectedSongURL);

if (selectedSongURL) {
    if (![[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]) {
        NSLog(@"overrideOutput error: %@", error);
    }
    self.internalAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:selectedSongURL error:nil];
    self.internalAudioPlayer.delegate = self;
    [self.internalAudioPlayer play];
} …
Run Code Online (Sandbox Code Playgroud)

iphone audio bluetooth ios

5
推荐指数
0
解决办法
857
查看次数

标签 统计

audio ×1

bluetooth ×1

ios ×1

iphone ×1