【问题标题】:Simple Video in webView and memory consumptionwebView中的简单视频和内存消耗
【发布时间】:2012-02-08 18:07:08
【问题描述】:

在网络视图中播放视频并查看乐器时 - 我在播放时看到内存使用高峰。 (总共大约 23 MB)

当我离开视图(它在 UINavigation 视图中)时,所有内存都会被清除。 (使用 ARC)

重要提示:我正在从磁盘加载视频,而不是从服务器加载!

问题:播放视频时有没有办法减少内存?

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

【问题讨论】:

  • 视频文件有多大?你用的是什么编解码器?
  • mp4 格式,不同的编解码器(每次加载不同的视频,因此编解码器可能会改变),每个视频文件的大小从 4mb 到 20mb 不等

标签: iphone objective-c ios memory video


【解决方案1】:

从您的代码看来,您正在尝试将 UIWebView 用作视频播放器,而不是同时在其中显示任何其他 HTML 内容。

尽管这很有可能,但正如您所观察到的,它并不是特别有效 - UIWebView 会将其所有内容加载到内存中,因为它是为显示网页而设计的。

更好的解决方案是使用 Apple 的MediaPlayer 框架,即MPMoviePlayerController 和/或MPMoviePlayerViewController

如果你只需要播放全屏视频,你应该使用MPMoviePlayerViewController。使用很简单:

MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:vc];
[vc release];

这将显示一个包含您的剪辑的模态视图控制器。 如果你想自定义它的任何部分,你可以使用moviePlayer 属性。

如果您希望在在另一个视图中显示视频,您应该查看MPMoviePlayerController。 使用此类涉及更多样板,但也为您提供更多控制:

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

【讨论】:

  • 感谢您的回答,不过我指的是使用 MediaPlayer 时或多或少相同的内存使用情况。我都试过了,内存消耗仍然很高(播放时20MB)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多