简体   繁体   English

iOS UIWebView loadRequest

[英]iOS UIWebView loadRequest

I'm using UIWebView to play an .mp3 file. 我正在使用UIWebView播放.mp3文件。 Using Safari because it gives me audio controls with no additional code and I couldn't see how to do that with AVPlayer. 使用Safari是因为它为我提供了没有其他代码的音频控件,而且我看不到如何使用AVPlayer进行操作。 It's simple and works well enough with one exception. 它很简单,除了一个例外,效果很好。 The .mp3 is played based on regions. .mp3基于区域播放。 It will play the audio file correctly for the first region. 它将正确播放第一个区域的音频文件。 But each subsequent region first plays audio for that region and then immediately whatever was the first audio played. 但是,每个随后的区域首先播放该区域的音频,然后立即播放最初播放的音频。 So, first region plays audio_region_1. 因此,第一个区域播放audio_region_1。 Second region fires audio_region_2 and then audio_region_1. 第二个区域触发audio_region_2,然后触发audio_region_1。 Nth region fires audio_region_n and then audio_region_1 again. 第N个区域触发audio_region_n,然后再次触发audio_region_1。 It's always playing two files, with the second file played always the first in app. 它总是播放两个文件,第二个文件始终在应用程序中播放第一个文件。 A new Safari View is started, plays, closes, then second Safari view opens, plays and closes. 启动,播放,关闭新的Safari视图,然后打开,播放和关闭第二个Safari视图。 Any ideas on either why or how to troubleshoot? 关于为什么或如何进行故障排除的任何想法?

NSURL *currentRegion = [[NSBundle mainBundle]URLForResource: regionIdentifier withExtension:@"mp3"];
NSURLRequest *currentRegionForSafari = [NSURLRequest requestWithURL: currentRegion];
[_viewWeb loadRequest: currentRegionForSafari];
NSLog(@"        ** ** ** Narration shud have finished:%@.mp3", regionIdentifier);
  1. should I be using something like webViewDidFinishLoad to then explicitly call stopLoading or something like that? 我应该使用webViewDidFinishLoad之类的东西来显式调用stopLoading之类的吗?
  2. log shows my NSLog message only for the current region, the expected value of regionIdentifier. 日志仅显示当前区域的我的NSLog消息,即regionIdentifier的期望值。
  3. log shows two instances of "setting movie path: " matching the behavior described above. 日志显示了两个与上述行为匹配的“设置电影路径:”实例。

Thanks in advance and excuse the simple nature of the question. 在此先感谢并原谅问题的简单性。 This is my first IOS app. 这是我的第一个IOS应用。

Please make sure that you have different region each time that you need play another sound. 每次需要播放其他声音时,请确保您具有不同的区域。

Also I am not sure if it will help you but just try this code: 另外,我不确定它是否对您有帮助,请尝试以下代码:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURL *currentRegion = [[NSBundle mainBundle]URLForResource: regionIdentifier withExtension:@"mp3"];
NSURLRequest *currentRegionForSafari = [NSURLRequest requestWithURL: currentRegion];
[currentRegionForSafari setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[_viewWeb loadRequest: currentRegionForSafari];

The essential problem was that UIWebView was caching a previous .mp3 sent to it, and playing it after the current load request completed. 根本问题是UIWebView会缓存发送给它的先前的.mp3,并在当前加载请求完成后对其进行播放。 Interestingly it was caching the first .mp3 sent not the last as you might expect. 有趣的是,它缓存的是第一个.mp3,而不是您所期望的最后一个。 Anyway, I evaluated two suggestions for generally "cleaning contents" of UIWebView found on another question. 无论如何,我评估了关于在另一个问题上发现的UIWebView的“清理内容”的两个建议。

This worked for me, added on entry to my load request method. 这对我有用,添加到我的负载请求方法中。

 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

Here's the URL for the other question, should it help someone else: 这是其他问题的网址,它是否可以帮助其他人:

How to clean the content of UIWebView without loading an empty page? 如何在不加载空白页面的情况下清除UIWebView的内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM