AVAudioPlayer Play audio on music plays on the sound box of phone calls

一笑奈何 提交于 2019-12-31 07:32:48

问题


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 ?


回答1:


Try adding the following to the top of your playAudio method:

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


来源:https://stackoverflow.com/questions/26829049/avaudioplayer-play-audio-on-music-plays-on-the-sound-box-of-phone-calls

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