我在这里和网络上查看了十几个不同的帖子。还是想不通。我可以在本地播放已附加到支持文件的电影,但是当我尝试播放从相机胶卷中挑选的电影时,出现黑屏。所以我认为问题在于从 uipicker 获取 url 并播放它。
我认为我的问题出在 playMovie 的前两行 -
NSURL *videoURL = [NSURL URLWithString:@"public.movie"]; MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];
这没有给我正确的网址来从选择器播放电影。我也试过用'url'代替@"public.movie"
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize player;
-(IBAction) selectMovie
{
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentModalViewController:picker animated:YES];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
//[mediaType isEqualToString:@"public.movie"];
//{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];//used to be*videoURL
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[picker dismissModalViewControllerAnimated:YES];
//[picker release];
}
-(IBAction)playMovie
{
NSURL *videoURL = [NSURL URLWithString:@"public.movie"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];
[moviePlayer prepareToPlay];
moviePlayer.view.frame = CGRectMake(100, 100, 200, 200);
[self.view addSubview:moviePlayer.view];
moviePlayer.shouldAutoplay = NO;
moviePlayer.view.backgroundColor = [UIColor grayColor];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
//[player release];
}
-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[self dismissModalViewControllerAnimated:YES];
//[player autorelease];
//[player release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
[moviePlayer release];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end