when playing a video inside a webview and looking at instruments - I see a high peak of memory usage when playing. (Around 23 MB in total)
As I leave the view (it is in a UINavigation view) all memory get cleared as it should. (using ARC)
IMPORTANT: I am loading the video from DISK and not loading it from server!
Question: Is there a way to reduce the memory when playing the video?
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: videoURL cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 10.0];
[webView loadRequest: request];
[webView setOpaque:NO];
From your code it seems to me that you are trying to use an UIWebView as a video player, without displaying any other HTML content in it at the same time.
Even though this is quite possible, it is, as you have observed, not particularly efficient - a UIWebView will load all of it's content into memory since it was made for displaying web pages.
A better solution would be to use Apple's MediaPlayer
framework, namely MPMoviePlayerController
and/or MPMoviePlayerViewController
.
If you only need to playback fullscreen video, you should use MPMoviePlayerViewController
. Using it is simple:
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:vc];
[vc release];
This will present a modal view controller containing your clip.
If you want to customize any part of it, you can use the moviePlayer
property.
If you would rather display video inside another view, you should look into MPMoviePlayerController
.
Using this class involves more boilerplate, but also gives you more control:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player prepareToPlay];
player.view.frame = contentView.bounds; //The Player View's Frame must match the Parent View's
// ...
[player play];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With