需要在 App 內播放一段網路影片時,不必急著找第三方庫,MediaPlayer 框架就能勝任。本文整理一個可直接運行的範例,包含首幀縮圖抓取、播放/暫停切換與播放結束後的清理。
先在專案中匯入 MediaPlayer.framework,然後引入標頭:
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
{
MPMoviePlayerViewController *playerViewController;
MPMoviePlayerController *player;
UIButton *_playBtn; // 播放按鈕(透明覆蓋層)
UIImageView *_image; // 播放圖示
UIImageView *_thumbImgView; // 影片首幀截圖
}
@end
這裡有個容易忽略的細節:必須先建立一個暫時的 MPMoviePlayerController 來取縮圖。若直接把要播放的實例拿去取首幀,會導致後續無法播放。
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger VideoWidth = 320;
NSInteger VideoHeight = 200;
NSInteger VideoY = 44;
NSString *urlStr = @"https://sm.domob.cn/ugc/151397.mp4";
// 重新初始化一個 player,否則取首幀會造成無法播放的 bug
MPMoviePlayerController *pc =
[[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
UIImage *videoThumbImg = [pc thumbnailImageAtTime:0
timeOption:MPMovieTimeOptionNearestKeyFrame];
_thumbImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, VideoY, VideoWidth, VideoHeight)];
[_thumbImgView setImage:videoThumbImg];
[self.view addSubview:_thumbImgView];
// 透明播放按鈕鋪在上方接收點擊
_playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_playBtn.backgroundColor = [UIColor clearColor];
[_playBtn setFrame:CGRectMake(0.0f, VideoY, VideoWidth, VideoHeight)];
[_playBtn addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playBtn];
// 中央的播放圖示
_image = [[UIImageView alloc] init];
_image.frame = CGRectMake(140, 120, 50, 50);
[_image setImage:[UIImage imageNamed:@"yss_ios_hy_huodong_touxiang.png"]];
[self.view addSubview:_image];
}
點擊時依當前 playbackState 決定暫停、續播還是首次播放。這種寫法讓同一顆按鈕同時肩負「播放」與「暫停」兩個角色。
- (void)playVideo {
// 正在播放 → 暫停並顯示播放圖示
if (player && player.playbackState == MPMoviePlaybackStatePlaying) {
[player pause];
_image.hidden = NO;
return;
}
// 已暫停 → 續播並隱藏圖示
if (player && player.playbackState == MPMoviePlaybackStatePaused) {
_image.hidden = YES;
[player play];
return;
}
_image.hidden = YES;
NSInteger VideoWidth = 320;
NSInteger VideoHeight = 200;
NSInteger VideoY = 44;
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL URLWithString:@"https://sm.domob.cn/ugc/151397.mp4"]];
player.view.frame = CGRectMake(0, VideoY, VideoWidth, VideoHeight);
player.controlStyle = MPMovieControlStyleNone;
player.repeatMode = MPMovieRepeatModeNone;
player.scalingMode = MPMovieScalingModeAspectFit;
[player setFullscreen:YES animated:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[self.view insertSubview:player.view belowSubview:_playBtn];
[player play];
}
通知回呼裡一定要做三件事:移除 observer、停止播放、把播放器視圖從階層中移除。少了任何一步,再次播放時都可能出現畫面堆疊或記憶體洩漏。
- (void)myMovieFinishedCallback:(NSNotification *)notify {
MPMoviePlayerController *theMovie = [notify object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie stop];
[theMovie.view removeFromSuperview];
if (player && player.playbackState == MPMoviePlaybackStateStopped) {
_image.hidden = NO;
[player stop];
}
}
- (BOOL)shouldAutorotate { return YES; }
補充一點:MPMoviePlayerController 家族在 iOS 9 之後已被標記為 deprecated,新專案建議改用 AVPlayer 搭配 AVPlayerViewController,但上面這套「取首幀當封面 + 自製播放覆蓋層」的互動思路,換到 AVPlayer 一樣適用。