2014-02-24 61 views
0

在這裏,我想使用MediaPlayer框架從服務器播放視頻。 1)我正在添加mediaplayer框架。 2)我正在導入#import頭文件3)我通過使用谷歌實現了代碼。從服務器播放視頻在MediaPlayer框架中不起作用

我的代碼是:

- (void)act_GoToVideoPlayer:(UIButton*)sender 
{ 
    NSString* resourcePath = [NSString stringWithFormat:@"http://%@",[postMediaFileArr objectAtIndex:sender.tag]]; //Video file URL is getting from server and stored it in a MutableArray 
    NSURL *url = [NSURL URLWithString: resourcePath]; 
    mediaplayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:str]]; 
    [mediaplayer.view setFrame:CGRectMake(20, 100, 250, 150)]; 
    [self.view addSubview:mediaplayer.view]; 

    mediaplayer.fullscreen = YES; 
    mediaplayer.allowsAirPlay = YES; 
    mediaplayer.shouldAutoplay = YES; 
    mediaplayer.controlStyle = MPMovieControlStyleEmbedded; 
} 

的觀點去黑屏和無限加載時調用此函數。我嘗試過許多其他版本的這種實現,結果都是失敗的。在特定情況下登錄是:

_itemFailedToPlayToEnd: { 
    kind = 1; 
    new = 2; 
    old = 0; 
} 

如果我從網站傳遞任何其他視頻。它將播放音頻而不是視頻。 瞭解原因?這是我第一次嘗試播放視頻,現在它已成爲一場噩夢。

+0

在你的代碼中加入這一行mpvc.movi​​ePlayer.movi​​eSourceType = MPMovieSourceTypeStreaming; 。 – Rushabh

+0

嗨@Rushabh - 感謝您的即時回覆。我也加了這條線。但同樣的警告即將到來,音頻和視頻都不起作用。 – Sabs

+0

你能告訴我這段視頻的完整性嗎? – Rushabh

回答

2
-(void)notifyCompletelyVisible 
{ 

     NSURL *aUrl = [NSURL URLWithString:@"*YOUR URL*"]; 
     _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl]; 
     _moviePlayer.controlStyle = MPMovieControlStyleNone; 
     _moviePlayer.shouldAutoplay = YES; 
     _moviePlayer.view.frame = asyvideoImage.frame; 
     _moviePlayer.scalingMode=MPMovieScalingModeFill; 
     [self.view addsubview _moviePlayer.view]; 
     [_moviePlayer prepareToPlay]; 
     [_moviePlayer setFullscreen:NO animated:NO]; 

} 

這是工作剛剛導入

進口 「的MediaPlayer/MediaPlayer.h」

+0

如何設置contentView。 – Sabs

+0

對不起,你只需要添加_movieplayer到你的視圖 –

+0

我添加這樣[self.view addSubview:mediaplayer.view]; – Sabs

0

你的.h嘗試:

#import <MediaPlayer/MediaPlayer.h> 

,並添加委託

interface YourViewcontroler : UIViewController <MPMediaPlayback> 
{ 
MPMoviePlayerController *player; 
} 

和您的.m文件中:

-(void) launchVideo{ 
    YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSString* myURL [email protected]"http://yourUrl/file.mp4"; 
    NSURL *urlString = [NSURL URLWithString:myURL]; 
    player = [[MPMoviePlayerController alloc] initWithContentURL:urlString]; 
    [player view].alpha=1; 
    [appDelegate.window.rootViewController.view addSubview:[player view]]; 
    [player play]; 
} 

和整理委託梅索德:

-(void)videoPlayBackDidFinish:(NSNotification*)notification { 

    [player.view removeFromSuperview]; 
    [player stop]; 
    player=nil; 
    [player release]; 
} 

我希望這會幫助你

相關問題