Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Video in webView and memory consumption

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];

enter image description here

like image 854
chewy Avatar asked Nov 13 '22 11:11

chewy


1 Answers

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];
like image 129
liclac Avatar answered Nov 16 '22 03:11

liclac