我正在尝试制作播放视频的应用,但我遇到了问题。我听到声音但看不到视频。我真诚地试图寻找解决方案,但所有提示似乎都不起作用。这是代码:
#import "VideoTestViewController.h"
@implementation VideoTestViewController
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"spitfiregrill_iPhone.m4v"];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
theMovie.controlStyle = MPMovieControlStyleDefault;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface VideoTestViewController : UIViewController {
}
@end
答案 0 :(得分:2)
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:@"Stock_Footage_Demobroadband"
ofType:@"mp4"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play partial screen---
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
//---play movie---
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
全屏模式的视频 -
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:@"Stock_Footage_Demobroadband"
ofType:@"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
答案 1 :(得分:1)
有点猜测 - 但这就是我要做的事情:
VideoTestViewController.h
#import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface VideoTestViewController : UIViewController { MPMoviePlayerController* m_player; } - (void) play; @end
VideoTestViewController.m
@implementation VideoTestViewController - (void) viewDidAppear:(BOOL)animated { [self play]; } - (void) play { NSURL* url = [[NSBundle mainBundle] URLForResource:@"spitfiregrill_iPhone" withExtension:@"m4v"]; m_player = [[MPMoviePlayerController alloc] initWithContentURL:url]; [m_player.backgroundView setBackgroundColor:[UIColor blackColor]]; [m_player.view setBackgroundColor:[UIColor blackColor]]; [m_player setControlStyle:MPMovieControlStyleNone]; [[m_player view] setFrame:[self.view bounds]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; [m_player play]; [self.view addSubview:[m_player view]]; } - (void) moviePlayBackDidFinish:(NSNotification*)_notification { [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; [m_player.view removeFromSuperview]; [m_player stop]; [m_player release]; m_player = nil; } @end