(ios實戰)MediaPlayer播放視頻


1MediaPlayer內嵌的方式播放視頻 

1.1 導入頭文件 

#import "MediaPlayer/MediaPlayer.h"

 1.2 定義MoviePlayerController類

創建MoviePlayerController類

player = [[MPMoviePlayerControlleralloc] init];

設置視頻路徑

NSString *path = [[NSBundlemainBundle] pathForResource:self.questiondata.titleimgofType:@"mp4"inDirectory:nil];

                NSURL *movieURL = [NSURL fileURLWithPath:path];

player.contentURL = movieURL

配置 控件樣式

player.controlStyle = MPMovieControlStyleEmbedded;//內嵌的方式

player.controlStyle =MPMovieControlStyleFullscreen; //全屏的方式

 

1.3 將視頻控件添加到指定的地方

[player.viewsetFrame:CGRectMake(30,_viewHeight+10,270,97)];

 [selfaddSubview:player.view];

 [player play];

 

1.4 釋放控件

 if (player!=nil) {

        [ player stop ];

        [player.viewremoveFromSuperview];

        [playerrelease];

    }

 

2 MediaPlayer播放過程中的事件捕獲和處理

本次舉例偵聽

 // 注冊一個播放結束的通知  

 [[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(myMovieFinishedCallback:)    name:MPMoviePlayerPlaybackDidFinishNotification   object:movie]; 

//處理事件

 

 	/* 
 	 @method 當視頻播放完畢釋放對象  
	*/  
 -(void)myMovieFinishedCallback:(NSNotification*)notify  
 {  
    //視頻播放對象  
    MPMoviePlayerController* theMovie = [notify object];  
	    //銷毀播放通知  
    [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                    name:MPMoviePlayerPlaybackDidFinishNotification  
		                                                  object:theMovie];  
	    [theMovie.view removeFromSuperview];  
	   // 釋放視頻對象  
    [theMovie release];  
	}  

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM