美文网首页WKweb使用,FMDB讲解和封装
iOS WKWebView播放音视频没有声音

iOS WKWebView播放音视频没有声音

作者: 路边的风景呢 | 来源:发表于2022-07-18 11:26 被阅读0次

在初始化 WKWebViewConfiguration 的时候 添加下面的代码! 我这边是解决了!

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];

    if(@available(iOS10.0, *)) {

        config.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;

    }else{

        // Fallback on earlier versions

    }

    config.allowsInlineMediaPlayback = YES;

    // 自动播放, 不需要用户采取任何手势开启播放

    // WKAudiovisualMediaTypeNone 音视频的播放不需要用户手势触发, 即为自动播放

    config.allowsAirPlayForMediaPlayback = YES;

    config.allowsPictureInPictureMediaPlayback = YES;

    //        自定义的WKScriptMessageHandler 是为了解决内存不释放的问题

    MWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[MWebViewScriptMessageDelegate alloc] initWithDelegate:self];

    //这个类主要用来做native与JavaScript的交互管理

    WKUserContentController * wkUController = [[WKUserContentController alloc] init];

    //        注册一个name为jsToOcNoPrams的js方法 设置处理接收JS方法的对象

    NSError*error;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];

    if(error) {

        NSLog(@"Error in setting category = %@", error);

        //handle error here.

    }

    [[AVAudioSession sharedInstance] setActive:YES error:&error];

    if(error) {

        NSLog(@"Error in activating session = %@", error);

        //handle error here.

    }

相关文章

网友评论

    本文标题:iOS WKWebView播放音视频没有声音

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