在ios6中播放视频

时间:2012-11-14 23:31:31

标签: iphone ios xcode ipad mpmovieplayercontroller

当应用程序启动时,视频将自动运行

但是按下按钮并再次打开应用冻结并且不知道为什么

我能做什么?

- (void)viewDidLoad{

    m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
            [m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
            [m_player.view setBackgroundColor:[UIColor blackColor]];
            [m_player setControlStyle:MPMovieControlStyleNone];
            [[m_player view] setFrame:[self.view bounds]];

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

            [m_player play];
            [self.view addSubview:[m_player view]];
    }


- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{

        [[NSNotificationCenter defaultCenter] removeObserver:self
                                              name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:nil];

        [m_player.view removeFromSuperview];
        [m_player stop];
        m_player = nil;

    }

}

1 个答案:

答案 0 :(得分:2)

这段代码可以帮助我避免冻结应用程序按下按钮回家,视频播放和工作!!!

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:)
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];




-(void)handleEnteredBackground:(NSNotification*)_notification{

   [m_player play];

}