MPMoviePlayerController 仅在调用两次时播放。仅在 iOS 4 中出现
Posted
技术标签:
【中文标题】MPMoviePlayerController 仅在调用两次时播放。仅在 iOS 4 中出现【英文标题】:MPMoviePlayerController plays only when called twice. Only occurs in iOS 4 【发布时间】:2012-08-20 15:53:43 【问题描述】:我有一个适用于 ios 4.0-5.1 的应用程序,它使用 HTTP Live Streaming 来播放视频。我有一个简单的设置,视图中有一个按钮,当它被点击时开始播放流。这在 iOS 5 中运行良好,但在 iOS 4 中开始播放流之前需要点击按钮两次。有人知道为什么会发生这种情况以及如何在第一次点击按钮时播放视频吗?
这是我正在做的事情:
.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController
MPMoviePlayerController *moviePlayer;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
-(IBAction)playApple:(id)sender;
@end
.m
-(IBAction)playApple:(id)sender
if(self.moviePlayer)
self.moviePlayer = nil;
//Use Apple's sample stream
NSURL *mediaURL = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
//Begin observing the moviePlayer's load state.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self.moviePlayer];
[self.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming];
[self.moviePlayer setShouldAutoplay:NO];//Stop it from autoplaying
[self.moviePlayer prepareToPlay];//Start preparing the video
#pragma mark Notification Center
- (void)moviePlayerLoadStateChanged:(NSNotification *)notification
NSLog(@"State changed to: %d\n", moviePlayer.loadState);
if(self.moviePlayer.loadState == MPMovieLoadStatePlayable)
//if load state is ready to play
[self.view addSubview:[self.moviePlayer view]];
[self.moviePlayer setFullscreen:YES];
[self.moviePlayer play];//play the video
【问题讨论】:
【参考方案1】:我可以看到一些可能的问题 - 只需尝试一下,让我们知道是否有任何或所有这些都可以解决问题。
1.负载状态屏蔽
loadstate
不应该直接比较,而是在比较之前屏蔽,因为它可能包含多种状态的混合。
MPMovieLoadState
描述电影播放器网络负载状态的常量。
enum
MPMovieLoadStateUnknown = 0,
MPMovieLoadStatePlayable = 1 << 0,
MPMovieLoadStatePlaythroughOK = 1 << 1,
MPMovieLoadStateStalled = 1 << 2,
;
typedef NSInteger MPMovieLoadState;
因此,不要像您一样直接比较它,而是将其掩盖以确保安全。
2。查看设置
为什么不在开始播放之前设置播放器视图。我从来没有见过你这样做的方式(不是我肯定这是问题所在 - 对我来说仍然很奇怪)。此外,即使播放器实际上不会使用您分配的视图(全屏模式会有所不同),您可能希望通过分配适当的帧来增强视图设置。
以上所有内容都融入了您的新版本代码...
-(IBAction)playApple:(id)sender
if(self.moviePlayer)
self.moviePlayer = nil;
//Use Apple's sample stream
NSURL *mediaURL = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
//Begin observing the moviePlayer's load state.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self.moviePlayer];
self.moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:[self.moviePlayer view]];
[self.moviePlayer setFullscreen:YES];
[self.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming];
[self.moviePlayer setShouldAutoplay:NO]; //Stop it from autoplaying
[self.moviePlayer prepareToPlay]; //Start preparing the video
- (void)moviePlayerLoadStateChanged:(NSNotification *)notification
NSLog(@"State changed to: %d\n", moviePlayer.loadState);
if((self.moviePlayer.loadState & MPMovieLoadStatePlayable) == MPMovieLoadStatePlayable)
//if load state is ready to play
[self.moviePlayer play];//play the video
【讨论】:
以上是关于MPMoviePlayerController 仅在调用两次时播放。仅在 iOS 4 中出现的主要内容,如果未能解决你的问题,请参考以下文章
MPMoviePlayerController 仅显示黑屏 - Swift
如何在隐藏控件时仅捕获 MPMoviePlayerController 视图上的单击/点击?
设置 MPMoviePlayerController 的方向
MPMoviePlayerController 全屏方向问题