1

我有一个适用于 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
    }

}
4

1 回答 1

5

我可以看到一些可能的问题 - 试一试,让我们知道是否有任何或所有这些都成功了。

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
    }
}
于 2012-08-20T18:05:10.437 回答