问题与手机通话的iOS SDK中播放音乐文件 [英] Issue with playing music file during a phone calling iOS sdk

查看:129
本文介绍了问题与手机通话的iOS SDK中播放音乐文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code两部分组成:

I have two pieces of code:

code 1 - 这是通过在耳扬声器播放音乐文件:

Code 1 - This is playing a music file via the in-ear speaker:

NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"8Ocean.mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:tileDirectory];
NSError *error=nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if(!error)
{
    [self.audioPlayer prepareToPlay];
    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    [self.audioPlayer play];
}
else
{
    NSLog(@"%@",error.localizedDescription);
}

code 2 - 这是一个电话(被路由到耳内或免提电话在通话过程中根据用户的设置)

Code 2- This is playing a music file during a phone call (is routed to in-ear or speaker phone depending on user setting during the phone call)

NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"8Ocean.mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:tileDirectory];
NSError *error=nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if(!error)
{
    [self.audioPlayer prepareToPlay];
    self.audioPlayer.volume=10.0;
    UInt32 sessionCategory = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    [self.audioPlayer play];
}
else
{
    NSLog(@"%@",error.localizedDescription);
}

我的问题是:

code 1和2的工作做好独立。但是,使用code 1,code 2都将无法正常工作(除非应用程序被杀害并重新启动)后

Code 1 and 2 work well independently. However, after using code 1, code 2 will not work at all (unless the app is killed and restarted)

我已经试过所有我能想到的,但不能工作问题出在哪里是。

I have tried everything I can think of but cannot work out where the problem is from.

推荐答案

您的应用程序需要处理来自电话的音频会话中断为了不被杀死你的应用程序的音频处理。

Your app needs to handle audio session interruptions from phone calls in order for audio processing not to be killed for your app.

这篇关于问题与手机通话的iOS SDK中播放音乐文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆