IOS 播放視頻(MPMoviePlayerController、MPMoviePlayerViewController)


iOS提供了叫 做MPMoviePlayerControllerMPMoviePlayerViewController的兩個 類,可以用來輕松播放視頻

 ➢ YouTobe就是用MPMoviePlayerController實現的 MPMoviePlayerViewController只能全屏播放視頻

上述兩個類都定義在了MediaPlayer框架中

 

 
 

 

 

MPMoviePlayerController-01-加載資源

 // 加載視頻資源
NSString *urlString = [[NSBundle mainBundle]

pathForResource:@"sample_iTunes" ofType:@"mov"]; 
NSURL *url = [NSURL fileURLWithPath:urlString]; 

// 創建播放器

_player = [[MPMoviePlayerController alloc] initWithContentURL:url];

// 加載視頻資源
NSString *urlString = [[NSBundle mainBundle]
pathForResource:@"sample_iTunes" ofType:@"mov"];
NSURL *url = [NSURL fileURLWithPath:urlString]; 

// 創建播放器

_player = [[MPMoviePlayerController alloc] initWithContentURL:url]; 

 

MPMoviePlayerController-02-顯示

 

// 設置尺寸
_player.view.frame = self.view.bounds;
_player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight; 

// 添加到控制器的view
[self.view addSubview:_player.view]; // 播放
[_player play];

 

 

MPMoviePlayerController-03-添加通知 監聽

 

// 監聽播放狀態的改變

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStateChange)

name:MPMoviePlayerPlaybackStateDidChangeNotification object:_player]; 

// 監聽播放器結束全屏

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitFullscreen) name:MPMoviePlayerDidExitFullscreenNotification object:_player]; 

 

MPMoviePlayerController其他屬性

 

是否要自動播放
@property(nonatomic) BOOL shouldAutoplay;

全屏顯示
@property(nonatomic, getter=isFullscreen) BOOL

fullscreen;
- (void)setFullscreen:(BOOL)fullscreen animated: 

(BOOL)animated;

截取視頻中的圖片

- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option; 

 

 

MPMoviePlayerViewController

MPMoviePlayerViewController繼承自UIViewController,它內部封裝了一 個MPMoviePlayerController

MPMoviePlayerViewController只能全屏播放

簡單使用:

MPMoviePlayerViewController *play = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
其他
音頻處理
依賴的框架:AVFoundationAudioToolbox框架
播放長音樂:AVAudioPlayer
播放短音效:加載音頻文件生成SystemSoundID
錄音:AVAudioRecord
 

 ● 較為底層、高級的音頻\視頻處理

CoreAudioCoreVideo框架

 

 

 

 
        


免責聲明!

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



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