Reputation: 41
I have a simple code using AVAudioPlayer, to play a .caf audio:
AppDelegate.h
AVAudioPlayer *_audioPlayer;
AppDelegate.m
- (void)playAudio{
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:audiosrc];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL
error:nil];
_audioPlayer.volume = 1.0;
[_audioPlayer play];
}
The audio play very well, but he did not run the audio through the speakers of the device , but runs the audio on the speaker where the phone call is made , thus making the audio becomes very low , how to solve and change sound box ?
Upvotes: 0
Views: 179
Reputation: 1108
Try adding the following to the top of your playAudio
method:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
Upvotes: 2