私は、こことウェブでさまざまな投稿を見てきました。まだわかりません。サポート ファイルに添付したムービーをローカルで再生できますが、カメラ ロールから選択したムービーを再生しようとすると、黒い画面が表示されます。したがって、問題は uipicker から URL を取得して再生することにあると思います。
私の問題は、playMovie- の最初の 2 行にあると思います。
NSURL *videoURL = [NSURL URLWithString:@"public.movie"]; MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];
これは、ピッカーから映画を再生するための適切な URL を教えてくれません。@"public.movie" の代わりに 'url' を入れてみました
#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