问题
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