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

ぃ、小莉子 提交于 2019-12-02 20:08:23

问题


I want to play a music file through ear speaker,

What I am doing now is:

In viewDidLoad method ::::

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

Initializing music file in viewDidLoad ::::

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

Now Playing file after routing audio in a method :::::

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

It's playing through loud speaker. Please guide me the proper way!


回答1:


You need to change the category of AVAudioSession like this:

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

Find more details in 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).



来源:https://stackoverflow.com/questions/32476523/make-avaudioplayer-play-a-music-file-through-phone-call-ear-speaker

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