1
我在這裏和網上查看了十幾個不同的帖子。仍然無法弄清楚。我可以在本地播放支持文件的電影,但當我嘗試播放從相機卷拾取的電影時,我會看到黑屏。所以我相信問題在於從uipicker獲取網址並播放它。試圖從相機膠捲播放電影時出現黑屏
我想我的問題就出在這裏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