我见过很多人都面临这个问题。尝试过实现他们的一些解决方案(声明moviePlayer
的属性,明确设置_moviePlayer.contentURL
。但它仍然显示黑屏(iOS7)。
以下是代码:
MoviePlayer.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MoviePlayer : UIViewController
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@property (nonatomic,weak) NSURL *vidURL;
@end
MoviePlayer.m
#import "MoviePlayer.h"
@interface MoviePlayer ()
@end
@implementation MoviePlayer
-(void)viewDidLoad {
[super viewDidLoad];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:_vidURL];
NSLog(@"%@", _vidURL);
_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
_moviePlayer.view.transform = landscapeTransform;
_moviePlayer.contentURL = _vidURL;
[_moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:_moviePlayer.view];
}
@end
打印出NSLog :
assets-library://asset/asset.mov?id=07E61ABA-3EEB-461B-B170-000E7AE3F9A8&ext=mov
更新
vidURL从另一个viewControlelr传递:
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
ALAsset *asset = self.assets[indexPath.row];
ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
NSURL *vidURL = [defaultRep url];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
MoviePlayer *moviePController = [storyboard instantiateViewControllerWithIdentifier:@"moviePlayer"];
moviePController.vidURL = vidURL;
[self.navigationController presentViewController:moviePController animated:YES completion:nil];
}
答案 0 :(得分:0)
尝试注册MPMoviePlayerLoadStateDidChangeNotification
和MPMoviePlayerPlaybackDidFinishNotification
并使用
if (self.player.loadState ==MPMoviePlaybackStateInterrupted) {
NSLog(@"MPMoviePlaybackStateInterrupted");
}else if (self.player.loadState ==MPMoviePlaybackStatePaused) {
// [self RemoveVideoCache];
NSLog(@"MPMoviePlaybackStatePaused");
}else if (self.player.loadState ==MPMoviePlaybackStateSeekingBackward) {
// [self RemoveVideoCache];
NSLog(@"MPMoviePlaybackStateSeekingBackward");
}
else if (self.player.loadState ==MPMoviePlaybackStateSeekingForward) {
NSLog(@"MPMoviePlaybackStateSeekingForward");
}else if (self.player.loadState ==MPMoviePlaybackStateStopped) {
NSLog(@"MPMoviePlaybackStateStopped");
}
答案 1 :(得分:0)
原来我只是在将其添加为子视图后才能播放它:
[_moviePlayer play];