iOS5:MPMoviePlayerController 仅视频,无音频
这不是没有视频,只有音频的问题。恰恰相反。 使用 iOS 5.0 时会出现此问题。运行 4.3 或更低版本的 iPad 可以完美地播放相同的视频文件。
由于 iOS 5 改变了 MPMoviePlayerControllers 的初始化方式,我必须进行一些基于 SDK 的编程才能显示视频。在实现我接下来展示的代码片段之前,视频及其控件甚至不会显示在屏幕上。控制器只会显示一个黑色方块,其大小和原点为给定的 CGRect 框架。
我的处理方式如下:
视频文件位于文档文件夹中。因此 NSURL 必须初始化为 fileURLWithPath。完成后,我继续使用给定的帧初始化控制器。因为否则它将无法工作,因此视图只会在更改其 loadState 后添加播放器。这是通过订阅通知来实现的。订阅者选择器在主线程上将控制器视图添加到父视图,因为通知可以从其他线程处理。
初始化并将视频添加到视图:
-(void)addVideo:(NSString*) videoName onRect:(CGRect)rect {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
iPadMagazineAppDelegate *appDelegate = GET_APP_DELEGATE;
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirName = [dirArray objectAtIndex:0];
// get directory name for this issue
NSURL *baseURL;
/*
BUGFIX: Video does not work on iOS 5.0
*/
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){
baseURL = [[NSURL fileURLWithPath:dirName]URLByAppendingPathComponent:[appDelegate.currentIssue getIssueDirectoryName ]];
}else {
baseURL = [[NSURL URLWithString:dirName] URLByAppendingPathComponent:[appDelegate.currentIssue getIssueDirectoryName]];
}
/* end BUGFIX: Video does not work on iOS 5.0 */
NSURL *videoURL = [baseURL URLByAppendingPathComponent:videoName];
MPMoviePlayerController * movieController= [[MPMoviePlayerController alloc]initWithContentURL:videoURL];
// set frame for player
movieController.view.frame = rect;
// set auto resizing masks
[movieController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
// don't auto play.
[movieController setShouldAutoplay:NO];
[movieController setUseApplicationAudioSession:YES];
/*
BUGFIX: Video does not work on iOS 5.0
*/
if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"5.0")) {
[movieController prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadVideo:) name:MPMoviePlayerLoadStateDidChangeNotification object:movieController];
}else {
[pdfView addSubview:movieController.view];
[pdfView bringSubviewToFront: movieController.view];
}
/* end BUGFIX: Video does not work on iOS 5.0 */
[_moviePlayerViewControllerArray addObject:movieController];
[movieController release];
[pool release];
}
通知处理程序:
-(void)loadVideo:(NSNotification*)notification {
for (MPMoviePlayerController *movieController in _moviePlayerViewControllerArray) {
if (movieController.loadState != MPMovieLoadStateUnknown) {
[pdfView performSelectorOnMainThread:@selector(addSubview:) withObject:movieController.view waitUntilDone:YES];
[pdfView performSelectorOnMainThread:@selector(bringSubviewToFront:) withObject:movieController.view waitUntilDone:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:movieController];
}
}
}
感谢您阅读这个大问题。我很欣赏你的回答。
干杯。
This is not the No video, audio only problem. It's just the opposite.
The problem arises when using iOS 5.0. iPads running 4.3 or lower play the same video files flawlessly.
since iOS 5 changed the way stuff is initialized for MPMoviePlayerControllers, I had to do some SDK based programming in order to get the video to be displayed. Before implementing the snippet I'm showing next, the video and it's controls won't even show up on the screen. The controller would only show a black square with the size and origin of given CGRect frame.
The way I handle it is the following:
The video files are located on the documents folder. So the NSURL has to be initialized as fileURLWithPath. Once that's done, I proceed to initialized the controller with a given frame. Since it wouldn't work otherwise, the view will only add the player once it has changed its loadState. That's achieve by subscribing to a notification. the subscriber selector performs the addition of the controller's view to the parent view on the main thread since the notification could be handled from other threads.
Initializing and adding video to the view:
-(void)addVideo:(NSString*) videoName onRect:(CGRect)rect {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
iPadMagazineAppDelegate *appDelegate = GET_APP_DELEGATE;
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirName = [dirArray objectAtIndex:0];
// get directory name for this issue
NSURL *baseURL;
/*
BUGFIX: Video does not work on iOS 5.0
*/
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){
baseURL = [[NSURL fileURLWithPath:dirName]URLByAppendingPathComponent:[appDelegate.currentIssue getIssueDirectoryName ]];
}else {
baseURL = [[NSURL URLWithString:dirName] URLByAppendingPathComponent:[appDelegate.currentIssue getIssueDirectoryName]];
}
/* end BUGFIX: Video does not work on iOS 5.0 */
NSURL *videoURL = [baseURL URLByAppendingPathComponent:videoName];
MPMoviePlayerController * movieController= [[MPMoviePlayerController alloc]initWithContentURL:videoURL];
// set frame for player
movieController.view.frame = rect;
// set auto resizing masks
[movieController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
// don't auto play.
[movieController setShouldAutoplay:NO];
[movieController setUseApplicationAudioSession:YES];
/*
BUGFIX: Video does not work on iOS 5.0
*/
if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"5.0")) {
[movieController prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadVideo:) name:MPMoviePlayerLoadStateDidChangeNotification object:movieController];
}else {
[pdfView addSubview:movieController.view];
[pdfView bringSubviewToFront: movieController.view];
}
/* end BUGFIX: Video does not work on iOS 5.0 */
[_moviePlayerViewControllerArray addObject:movieController];
[movieController release];
[pool release];
}
notification handler:
-(void)loadVideo:(NSNotification*)notification {
for (MPMoviePlayerController *movieController in _moviePlayerViewControllerArray) {
if (movieController.loadState != MPMovieLoadStateUnknown) {
[pdfView performSelectorOnMainThread:@selector(addSubview:) withObject:movieController.view waitUntilDone:YES];
[pdfView performSelectorOnMainThread:@selector(bringSubviewToFront:) withObject:movieController.view waitUntilDone:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:movieController];
}
}
}
Thank you for reading this huge question. I appreciate your answers.
cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
将 MPMoviePlayerController 的属性“useApplicationAudioSession”设置为“NO”。
Try this:
Set MPMoviePlayerController's property "useApplicationAudioSession" to "NO".
显然存在一个错误,但它与 MPMoviePlayerController 无关,而是与 iOS 5 本身有关。
我的 iPad 通过开关静音,但仍然从 iPod 应用程序播放音频,所以我没有意识到是这样,所以 MPMoviePlayerController 没问题,但部分操作系统没有注意到 iPad 被静音。
我已在 Apple 的错误跟踪器上提交了相应的错误。 Bug ID# 10368531。
如果浪费了您的时间,我深表歉意。
更新:已收到苹果针对该错误的反馈。这是预期的行为。 :\
Apparently there's a bug, but it's not related to the MPMoviePlayerController, but to iOS 5 itself.
My iPad was muted from the switch but still played audio from iPod app anyway so I didn't realized that it was that way, so MPMoviePlayerController was fine, but part of the OS did not notice that the iPad was muted.
I've filed the corresponding bug on Apple's bug tracker. Bug ID# 10368531.
I Apologize if I've wasted your time.
UPDATE: Got feedback from apple for the bug. It's expected behavior. :\