2011-02-25 98 views
0

我在iphone通過使用此代碼的電影播放器​​打開1秒鐘,然後自動關閉,沒有播放文件播放的視頻通過使用此代碼如何在iPhone SDK 4.0中播放視頻?

-(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]; } 

我檢查了視頻文件是正確的,但它不是在iphone SDK 4.0中播放。

在此先感謝。

回答

2

請參閱本question-

Playing a video file from server in an Iphone app

trry了下面的代碼,不要忘記添加MediaPlayer.framework,必須導入<的MediaPlayer/MediaPlayer.h >


- (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]; 

} 
+0

這個職位顯示如何在WebView中播放視頻文件我的問題是改變。請提供足夠的建議。 – Apekshit 2011-02-25 06:06:36

+0

@安德森 - 它在我的測試項目中工作,嘗試更新的答案! – 2011-02-25 06:29:07

+0

您的代碼在我的設備中無法使用。你能不能把你的測試項目以zip格式發給我,所以我可以在我的設備上測試它。目前我正在使用ios 4.0上的iphone 3gs。感謝您提供建議。 – Apekshit 2011-02-25 07:01:35