AVAudioPlayer在电话的音箱上播放音乐时播放音频 [英] AVAudioPlayer Play audio on music plays on the sound box of phone calls

查看:95
本文介绍了AVAudioPlayer在电话的音箱上播放音乐时播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用AVAudioPlayer播放.caf音频的简单代码:

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 ?

推荐答案

尝试将以下内容添加到playAudio方法的顶部:

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

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

这篇关于AVAudioPlayer在电话的音箱上播放音乐时播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆