本範例為點擊按鈕播放影片,步驟如下

1. 建立專案後加入MediaPlayer.framework

2. storyboard加入一顆按鈕來觸發影片播放

3. 將要播放的影片網址複製

4. 建立影片與程式的關聯性, 再建立一個player來播放影片, 接著代入播放語法即完成

程式碼如下:

ViewController.m 

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()

- (IBAction)play:(id)sender;

@end

@implementation ViewController

- (IBAction)play:(id)sender {
    
    
    /*建立程式與影片的關聯性,並放入影片網址*/
    NSURL *videoURL =[NSURL URLWithString:@"https://dl.dropboxusercontent.com/u/11373114/movie/big_buck_bunny.mp4"];
    /*建立一個player來播放影片*/
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:videoURL];
    /*播放影片*/
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
@end

 

arrow
arrow
    文章標籤
    iOS 播放網路上的影片
    全站熱搜

    Will 發表在 痞客邦 留言(0) 人氣()