美文网首页
AVAudioPlayer 播放扬声器没有声音解决方法

AVAudioPlayer 播放扬声器没有声音解决方法

作者: yuebiubiu | 来源:发表于2021-12-28 16:10 被阅读0次

    在开发的过程中发现用以下方法播放,扬声器没有声音:

        NSString * filePath = [[[NSBundle mainBundle]bundlePath] stringByAppendingString:@"audio.mp3"];
        NSURL * url = [NSURL fileURLWithPath:filePath];
        AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [player play];
    

    解决办法 如下:
    1,player 使用全局变量;
    2,在创建AVAudioPlayer前,加入以下代码:

        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        [audioSession setActive:YES error:nil];
    

    3,搞定。

    tips:在实践中发现,只使用最原始的代码,耳机和扬声器都是没有声音的,按照解决办法第一步,使用了全局变量后,用耳机可以听到声音,但扬声器还是没有声音;解决办法步骤一和步骤二都执行后,耳机和扬声器才都有了声音。

    相关文章

      网友评论

          本文标题:AVAudioPlayer 播放扬声器没有声音解决方法

          本文链接:https://www.haomeiwen.com/subject/ulvpqrtx.html