Issue with playing music file during a phone calling iOS sdk

大城市里の小女人 提交于 2019-12-13 04:50:37

问题


I have two pieces of code:

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- 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);
}

My issue is:

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.


回答1:


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



来源:https://stackoverflow.com/questions/20119041/issue-with-playing-music-file-during-a-phone-calling-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!