场景:iOS端设备,App页面跳转到H5产品介绍,背景音乐无法播放。(为什么不能自动播放,因该是iPhone人性化设定吧~)
加载H5用UIWebView空间:
代码:
CGRect rect = self.contentView.bounds; normalWeb = [[UIWebView alloc] initWithFrame:rect]; normalWeb.backgroundColor = [UIColor clearColor]; normalWeb.scrollView.bounces = NO; [self.contentView addSubview:normalWeb]; normalWeb.allowsInlineMediaPlayback = YES; //(可选) normalWeb.mediaPlaybackRequiresUserAction = NO; [self loadRequestWebUrl:urlStr];
这样就可以播放音频了。这是在Navtive原生上处理的办法。
//*> H5后台播放音频。当没有前台还没有退出H5界面时候。
@property (nonatomic) BOOL allowsInlineMediaPlayback NS_AVAILABLE_IOS(4_0); // iPhone Safari defaults to NO. iPad Safari defaults to YES
Description: A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller. The default value on iPhone is NO. In order for video to play inline, not only does this property need to be set on the view, but the video element in the HTML document must also include the webkit-playsinline attribute.
//*> H5是否用户控制播放音频设定。
@property (nonatomic) BOOL mediaPlaybackRequiresUserAction NS_AVAILABLE_IOS(4_0); // iPhone and iPad Safari both default to YES
A Boolean value that determines whether HTML5 videos can play automatically or require the user to start playing them.