Objective C iPhone 使用 MediaPlayer 播放视频不起作用
Objective C iPhone Playing Video with MediaPlayer dont work
在 iPhone 的应用中,
我正在尝试使用 MediaPlayer 播放视频,但无法播放。
我使用 main Bundle 来存储视频。
我没有错误,但是当我点击启动视频时,我有一个空白屏幕。
我的路径似乎是正确的。我不明白为什么它不起作用。谢谢你的帮助。
这是我的代码:
#import "VideoViewController.h"
@interface VideoViewController ()
@end
@implementation VideoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"myvideo" ofType:@"mp4"];
NSURL *url = [NSURL URLWithString: moviePath];
NSLog(@"%@", url);
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
self.moviePlayer.shouldAutoplay = YES;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
@end
好的,我找到了解决办法!希望这对其他人也有帮助 ;)
#import "VideoViewController.h"
@interface VideoViewController ()
@end
@implementation VideoViewController
{
BOOL didPlay;
}
- (void)playMovie
{
didPlay = YES;
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"myvideo" ofType:@"mp4"];
NSURL *mediaURL = [NSURL fileURLWithPath:moviePath];
// play
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaURL];
player.moviePlayer.allowsAirPlay = YES;
player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:player];
id observer1;
id observer2;
observer1 = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player.moviePlayer queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification)
{
[[NSNotificationCenter defaultCenter] removeObserver:observer1];
[[NSNotificationCenter defaultCenter] removeObserver:observer2];
}];
observer2 = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerLoadStateDidChangeNotification object:player.moviePlayer queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification)
{
if ((player.moviePlayer.loadState & MPMovieLoadStatePlayable) != 0)
[player.moviePlayer performSelector:@selector(play) withObject:nil afterDelay:1.0f];
}];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!didPlay) [self playMovie];
}
- (void)viewDidLoad {
[super viewDidLoad];
didPlay = NO;
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.moviePlayer.view];
}
- (IBAction)goBack:(id)sender {
[self performSegueWithIdentifier: @"back" sender: self];
NSLog(@"ok");
}
@end
在 iPhone 的应用中, 我正在尝试使用 MediaPlayer 播放视频,但无法播放。 我使用 main Bundle 来存储视频。 我没有错误,但是当我点击启动视频时,我有一个空白屏幕。 我的路径似乎是正确的。我不明白为什么它不起作用。谢谢你的帮助。 这是我的代码:
#import "VideoViewController.h"
@interface VideoViewController ()
@end
@implementation VideoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"myvideo" ofType:@"mp4"];
NSURL *url = [NSURL URLWithString: moviePath];
NSLog(@"%@", url);
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
self.moviePlayer.shouldAutoplay = YES;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
@end
好的,我找到了解决办法!希望这对其他人也有帮助 ;)
#import "VideoViewController.h"
@interface VideoViewController ()
@end
@implementation VideoViewController
{
BOOL didPlay;
}
- (void)playMovie
{
didPlay = YES;
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"myvideo" ofType:@"mp4"];
NSURL *mediaURL = [NSURL fileURLWithPath:moviePath];
// play
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaURL];
player.moviePlayer.allowsAirPlay = YES;
player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:player];
id observer1;
id observer2;
observer1 = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player.moviePlayer queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification)
{
[[NSNotificationCenter defaultCenter] removeObserver:observer1];
[[NSNotificationCenter defaultCenter] removeObserver:observer2];
}];
observer2 = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerLoadStateDidChangeNotification object:player.moviePlayer queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification)
{
if ((player.moviePlayer.loadState & MPMovieLoadStatePlayable) != 0)
[player.moviePlayer performSelector:@selector(play) withObject:nil afterDelay:1.0f];
}];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!didPlay) [self playMovie];
}
- (void)viewDidLoad {
[super viewDidLoad];
didPlay = NO;
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.moviePlayer.view];
}
- (IBAction)goBack:(id)sender {
[self performSegueWithIdentifier: @"back" sender: self];
NSLog(@"ok");
}
@end