iOS簡單流媒體播放

原文:cocoa自學(xué) - 人人小站?

首先需要引用framework:MediaPlayer.framework

然后在頭文件中

1#import

2@property (nonatomic,retain) MPMoviePlayerController *player;

在M文件中開始實(shí)現(xiàn)方法

1/**

2@method 播放視頻

3*/

4- (void)playMovie:(NSString *)fileURL{

5//本地視頻文件路徑

6//? ? NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];

7//網(wǎng)絡(luò)視頻URL

8NSURL *url = [NSURL URLWithString:fileURL];

9//? ? NSLog(@"%@", url);

10//視頻播放對象

11self.player = [[MPMoviePlayerController alloc] init];

12//設(shè)置為流媒體模式

13self.player.movieSourceType = MPMovieSourceTypeStreaming;

14[self.player setControlStyle:MPMovieControlStyleFullscreen];

15[self.player.view setFrame:self.view.bounds];

16[self.player setContentURL:url];

17self.player.fullscreen = YES;//全屏播放

18self.player.initialPlaybackTime = -1;

19[self.view addSubview:self.player.view];

20// 注冊一個(gè)播放結(jié)束的通知

21[[NSNotificationCenter defaultCenter] addObserver:self

22selector:@selector(myMovieFinishedCallback:)

23name:MPMoviePlayerPlaybackDidFinishNotification

24object:self.player];

25[self.player prepareToPlay];

26//? ? [sharedData stopLoading];

27[self.player play];

28}

29#pragma mark -------------------視頻播放結(jié)束委托--------------------

30/*

31@method 當(dāng)視頻播放完畢釋放對象

32*/

33- (void)myMovieFinishedCallback:(NSNotification*)notify

34{

35//視頻播放對象

36MPMoviePlayerController* theMovie = [notify object];

37//銷毀播放通知

38[[NSNotificationCenter defaultCenter] removeObserver:self

39name:MPMoviePlayerPlaybackDidFinishNotification

40object:theMovie];

41// 釋放視頻對象

42[theMovie.view removeFromSuperview];

43}

通過調(diào)用[self playMovie:videoURL];就可以播放視頻文件了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容