gpt4 book ai didi

ios - MPMoviePlayerController 仅在调用两次时播放。仅在 iOS 4 中出现

转载 作者:行者123 更新时间:2023-12-01 17:59:43 25 4
gpt4 key购买 nike

我有一个适用于 iOS 4.0-5.1 的应用程序,它使用 HTTP Live Streaming 来播放视频。我有一个简单的设置, View 中有一个按钮,当它被点击时开始播放流。这在 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.负载状态掩蔽
loadstate不应该直接比较,而是在比较之前屏蔽,因为它可能包含多种状态的混合。

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;

因此,不要像您一样直接比较它,而是将其掩盖以确保安全。

2.查看设置

为什么不在开始播放之前设置播放器 View 。我从未见过你这样做的方式(并不是说我肯定这是问题所在 - 对我来说仍然很奇怪)。此外,即使播放器实际上不会使用您分配的 View (全屏模式会有所不同),您可能希望通过分配适当的帧来增强 View 设置。

以上所有内容都融入了您的代码的这个新版本......
-(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
}
}

关于ios - MPMoviePlayerController 仅在调用两次时播放。仅在 iOS 4 中出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com