Question

I'm using UIWebView to play an .mp3 file. Using Safari because it gives me audio controls with no additional code and I couldn't see how to do that with AVPlayer. It's simple and works well enough with one exception. The .mp3 is played based on regions. 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. Second region fires audio_region_2 and then audio_region_1. Nth region fires audio_region_n and then audio_region_1 again. 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. 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?
  2. log shows my NSLog message only for the current region, the expected value of 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.

Was it helpful?

Solution 2

The essential problem was that UIWebView was caching a previous .mp3 sent to it, and playing it after the current load request completed. Interestingly it was caching the first .mp3 sent not the last as you might expect. Anyway, I evaluated two suggestions for generally "cleaning contents" of UIWebView found on another question.

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?

OTHER TIPS

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top