让AVAudioPlayer通过phone呼叫(耳)喇叭播放音乐文件

Make AVAudioPlayer play a music file through phone call (ear) speaker

我想通过耳机播放音乐文件,

我现在做的是:

viewDidLoad方法中::::

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

正在初始化 viewDidLoad 中的音乐文件 ::::

musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"1985_Ring_" ofType:@"wav"]];                                                                        
click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile  error:nil];

现在在方法中路由音频后播放文件 :::::

[[AVAudioSession sharedInstance] overrideOutputAudioPort:kAudioSessionOverrideAudioRoute_None error:nil];
[click2 play];

正在通过扬声器播放。请指导我正确的方法!

您需要像这样更改 AVAudioSession 的类别:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

Audio Session Programming Guide 中查找更多详细信息:

You can programmatically influence the audio output route. When using the AVAudioSessionCategoryPlayAndRecord category, audio normally goes to the receiver (the small speaker you hold to your ear when on a phone call).