MPMoviePlayerController 在 iOS 7 中显示带声音的黑屏,在 iOS 6 中运行良好

Posted

技术标签:

【中文标题】MPMoviePlayerController 在 iOS 7 中显示带声音的黑屏,在 iOS 6 中运行良好【英文标题】:MPMoviePlayerController showing black screen with sound in iOS 7, works fine in iOS 6 【发布时间】:2013-09-24 02:19:32 【问题描述】:

//introVideoViewController

#import <AVFoundation/AVAudiosession.h>
#import <AudioToolbox/AudioServices.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <MediaPlayer/MediaPlayer.h>
@property(nonatomic,strong) MPMoviePlayerController *playercontroller;

在 iOS 7 中我能听到视频声音但听不到视频。它在 iOS 6 中运行良好。

//introVideoViewController

//prepare & init video
-(void)prepareIntroVideo

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"intro_movie" ofType:@"mp4"]];
    //NSURL *url = [NSURL URLWithString:@"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"];
    self.playercontroller = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.playercontroller.view setFrame:CGRectMake(0, 0, 320,480)];
    self.playercontroller.movieSourceType = MPMovieSourceTypeFile;
    self.playercontroller.scalingMode = MPMovieScalingModeAspectFill;
    self.playercontroller.fullscreen = YES;
    self.playercontroller.controlStyle = MPMovieControlStyleNone;
    //playercontroller.controlStyle = MPMovieControlStyleFullscreen;
    self.playercontroller.view.userInteractionEnabled =NO;
    self.playercontroller.view.backgroundColor = [UIColor blackColor];
    self.playercontroller.shouldAutoplay = NO;
    
    //playercontroller.repeatMode = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.playercontroller];
    [self.playercontroller prepareToPlay];
    [self.view addSubview:self.playercontroller.view];
    
    
    // get the handle

    
    // Get the result
    self.firstlaunchVideoCheck = [self.defaults stringForKey:@"firstlaunchVideo"];
    
    //NSLog(@"first launch null or 1 = %@",firstlaunchVideoCheck);
    
    
    if ([self.firstlaunchVideoCheck intValue])
    
        //skip view overlay
        UIView *skipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        UITapGestureRecognizer *singleTapping =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showSkipButton)];
        singleTapping.delegate =self;
        singleTapping.numberOfTapsRequired = 1;
        [skipView addGestureRecognizer:singleTapping];
        
        singleTapping.cancelsTouchesInView = NO;
        
        //skipbutton
        self.skipMovieButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //0, 276, 480, 54)
        
        if (isPhone568)
        
         [self.skipMovieButton setFrame:CGRectMake(227, 504, 92, 44)];
        
        else
        
            [self.skipMovieButton setFrame:CGRectMake(227, 436, 92, 44)];
        
        
        [self.skipMovieButton setBackgroundColor:[UIColor clearColor]];
        
        UIImage *skipMovieButtonNormal = [UIImage imageNamed:@"button_skip.png"];
        //UIImage *skipMovieButtonPress = [UIImage imageNamed:@"skip-(active).png"];
        
        [self.skipMovieButton setBackgroundImage:skipMovieButtonNormal forState:UIControlStateNormal];
        //[self.skipMovieButton setBackgroundImage:skipMovieButtonPress forState:UIControlStateHighlighted];
        [self.skipMovieButton addTarget:self action:@selector(skipMovie) forControlEvents:UIControlEventTouchUpInside];
        [skipView addSubview:self.skipMovieButton];
        [self.view addSubview:skipView];
    
    [self.playercontroller stop];
    
    [self.view bringSubviewToFront:self.titleImgView];
    [self.view bringSubviewToFront:self.startMovieButton];
    [self.view sendSubviewToBack:self.playercontroller.view];
    [self.view sendSubviewToBack:self.skipMovieButton];


//play video
-(void)playIntroVideo

    NSLog(@"self.playercontroller.playbackState %i",self.playercontroller.playbackState);
    if (self.playercontroller.playbackState == MPMoviePlaybackStatePlaying)
    
        NSLog(@"playerconroller playing, do nothing");
    
    else if (self.playercontroller.playbackState == MPMoviePlaybackStateStopped)
    
        NSLog(@"playerconroller playing, do nothing11111111111");
        [self.playercontroller play];
    



//called when video played completed
- (void)moviePlaybackComplete:(NSNotification *)notification

    ////NSLog(@"Here Got Called");
    
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];
    //[playercontroller.view removeFromSuperview];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationDidEnterBackgroundNotification
                                                  object:[UIApplication sharedApplication]];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIApplicationWillEnterForegroundNotification
                                                  object:[UIApplication sharedApplication]];
    
    
    

    
   if (![self.firstlaunchVideoCheck intValue])
   
       self.firstlaunchVideoCheck = @"1";
        // get the handle
       [self.defaults setObject:self.firstlaunchVideoCheck forKey:@"firstlaunchVideo"];
       
        // save it
       [self.defaults synchronize];
    
       
    [self skipMovie];

【问题讨论】:

您是否偶然使用了 iPhone 5S?我在 5S 上遇到了同样的问题,但在其他设备上运行良好(甚至是运行 iOS7 的 iPhone 5)。它也适用于 iOS 6。 @wermy 是的,我正在使用 5s。我通过添加一行来修复它 self.playercontroller.contentURL = url; 2013 年 12 月 .. 提示 - 关于文件扩展名(文件来自网络)非常不稳定。在我的应用程序中,.MOV 有效,.mov 使 iPhone 严重崩溃。奇怪。模拟器也根本不起作用,它完全没用,只会崩溃。最后在 MPMoviePlayerViewController 方法有效时,MPMoviePlayerController 无用且损坏。 @Joe Blow,我使用了 MPMoviePlayerViewController 并展示了它,但在视频加载时我仍然出现黑屏 【参考方案1】:

再添加一行后一切正常。

self.playercontroller.contentURL = url;

【讨论】:

为什么它适用于上述设置? (iOS 6 和/或 iPhone 5)

以上是关于MPMoviePlayerController 在 iOS 7 中显示带声音的黑屏,在 iOS 6 中运行良好的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MPMoviePlayerController 上处理多个 SRT 文件

在 MPMoviePlayerController 上执行捏合手势时屏幕黑屏

在 UITableViewCell 中嵌入 MPMoviePlayerController

MPMoviePlayerController 在影片结尾更改控件样式

后台的 MPMoviePlayerController / AVAudioSession 在来电后不会重新开始播放

MPMoviePlayerController 在启动前显示空白屏幕?