iPhone上的Safari和Chrome都可以在访问此网页时播放后端声音。但是,当使用UIWebView访问它时,没有后端声音。必须有一些特殊的设置才能使它发挥作用。
尝试了许多在线解决方案,例如,但仍然没有工作。请帮帮我。
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
_webView.allowsInlineMediaPlayback = YES;
_webView.mediaPlaybackRequiresUserAction = NO;
答案 0 :(得分:0)
我试过这段代码,它对我有用 希望所以它也适用于你
-(void)loadWebView:(UIWebView *)webView withUrl:(NSString *)urlString
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
error:&setCategoryError];
if (!ok) {
NSLog(@"setCategoryError=%@", setCategoryError);
}
webView.allowsInlineMediaPlayback = YES;
webView.mediaPlaybackRequiresUserAction = NO;
NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *encodedString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:encodedString]]];
}