MPMoviePlayerController仅在调用两次时播放.仅在iOS 4中出现

Squ*_*tch 1 mpmovieplayercontroller http-live-streaming ios

我有一个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
Run Code Online (Sandbox Code Playgroud)

.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
    }

}
Run Code Online (Sandbox Code Playgroud)

Til*_*ill 5

我可以看到一些可能的问题 - 只需尝试一下,让我们知道是否有任何或所有这些问题.

1. loadstate masking

loadstate不能直接比较,但比较,因为它可能包含多个状态的混合之前所掩盖.

MPMovieLoadState
Run Code Online (Sandbox Code Playgroud)

描述电影播放器​​的网络负载状态的常量.

enum {
   MPMovieLoadStateUnknown        = 0,
   MPMovieLoadStatePlayable       = 1 << 0,
   MPMovieLoadStatePlaythroughOK  = 1 << 1,
   MPMovieLoadStateStalled        = 1 << 2,
};
typedef NSInteger MPMovieLoadState;
Run Code Online (Sandbox Code Playgroud)

因此,不要像你一样直接比较它,而是将它掩盖在安全的一面.

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
    }
}
Run Code Online (Sandbox Code Playgroud)