简体   繁体   中英

MPViewController showing black screen

I'd seen many people facing this issue. Have tried implementing some of their solutions (declaring a property for moviePlayer , explicitly set _moviePlayer.contentURL . But its still showing a black screen for me (iOS7).

Here's the code:

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

Print out of the NSLog :

assets-library://asset/asset.mov?id=07E61ABA-3EEB-461B-B170-000E7AE3F9A8&ext=mov

Update:

vidURL passed from another 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];
}

Try registering for MPMoviePlayerLoadStateDidChangeNotification and MPMoviePlayerPlaybackDidFinishNotification and use

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");
}

原来,我只需要在将它添加为子视图之后就可以播放它:

[_moviePlayer play];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM