如何在iphone sdk 4.0中播放视频?

时间:2011-02-25 05:43:49

标签: iphone ios4 mpmovieplayercontroller mpmovieplayer

我使用此代码在iphone中播放视频

-(IBAction)btnNew_clicked:(id)sender {  

NSURL *url = [NSURL URLWithString:@"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:mp];}

-(void)videoPlayBackDidFinish:(NSNotification*)notification  {       
[self dismissMoviePlayerViewControllerAnimated];  }

使用此代码电影播放器​​打开1秒钟然后自动关闭,而不播放文件。

我检查了视频文件是否正确,但它没有在iphone sdk 4.0中播放。

先谢谢。

1 个答案:

答案 0 :(得分:2)

请看这个问题 -

Playing a video file from server in an Iphone app

尝试以下代码,不要忘记添加MediaPlayer.framework,并且必须导入< MediaPlayer / MediaPlayer。>


- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4"];
    MPMoviePlayerController *player =[[MPMoviePlayerController alloc] initWithContentURL: url];
    [[player view] setFrame: [self.view bounds]];  // frame must match parent view
    [self.view addSubview: [player view]];
    [player play];

}