MPMoviePlayerController仅在被调用两次时播放.仅在iOS 4中发生 [英] MPMoviePlayerController plays only when called twice. Only occurs in iOS 4

查看:76
本文介绍了MPMoviePlayerController仅在被调用两次时播放.仅在iOS 4中发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于iOS 4.0-5.1的应用程序,该应用程序使用HTTP Live Streaming播放视频.我有一个简单的设置,在视图中有一个按钮,当点击该流时便开始播放该流.在iOS 5中可以正常工作,但是在iOS 4中开始播放流之前,需要先轻按两次按钮.有人知道为什么会发生这种情况以及如何在第一次点击按钮时播放视频吗?

I have an app for iOS 4.0-5.1 that uses HTTP Live Streaming to play videos. I have a simple setup with a button in a view that starts playing the stream when it is tapped. This works fine in iOS 5 but the button needs to be tapped twice before the stream begins playing in iOS 4. Does anybody know why this is happening and how to make the video play on the first tap of the button?

这是我在做什么:

.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


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

}

推荐答案

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

I can see some possible issues - just try it out and let us know if any or all of this did the trick.

1.负载状态屏蔽

loadstate不应直接进行比较,而应在进行比较之前进行屏蔽,因为它可能包含多种状态的组合.

The loadstate should not be directly compared but masked before comparing as it might contain a mixture of multiple states.

MPMovieLoadState

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

Constants describing the network load state of the movie player.

enum {
   MPMovieLoadStateUnknown        = 0,
   MPMovieLoadStatePlayable       = 1 << 0,
   MPMovieLoadStatePlaythroughOK  = 1 << 1,
   MPMovieLoadStateStalled        = 1 << 2,
};
typedef NSInteger MPMovieLoadState;

因此,与其直接进行比较,不如将其遮盖起来以确保安全.

So instead of directly comparing it, as you are, mask it to be on the safe side.

2.查看设置

为什么不在开始播放之前立即设置播放器视图.我从来没有像您那样看过它(不是我肯定这是问题所在-仍然对我来说很奇怪).此外,即使播放器实际上不会使用您分配的视图(全屏模式也会有所不同),您可能仍想通过分配适当的帧来增强视图设置.

Why not setting up the player view right before starting the playback. I have never seen it the way you do it (not that I was positively sure that this is the problem - still, seems odd to me). Additionally, even though the player will not actually use the view you are assigning (fullscreen mode does it a bit differently), you may want to enhance the view-setup with assigning a proper frame.

以上所有内容都集成到了此新版本的代码中……

All of the above rolled into this new version of your code...

-(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中发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆