Oc MPMoviePlayerController(視頻播放器)~dome

1.導入系統框架 UIKit.framework
CoreGraphics.framework
Foundation.framework
MediaPlayer.framework

導入 MP4 的文件用於播放視頻用的


#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>//視頻播放器

@interface ViewController ()
//播放視圖
@property (strong,nonatomic) IBOutletUIView *movieView;
//播放按鈕
- (IBAction)play:(id)sender;
//播放電影控制器
@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //1. 創建本地URL(也可創建基於網絡的URL)
    NSURL* movieUrl = [[NSBundle mainBundle] URLForResource:@"aaa"withExtension:@"mp4"];

    // 使用指定URL創建MPMoviePlayerController
    //2. MPMoviePlayerController將會播放該URL對應的視頻
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:movieUrl];

    //3. 設置該播放器的控制條風格。
    _moviePlayer.controlStyle =MPMovieControlStyleEmbedded;

    //4. 設置該播放器的縮放模式
    _moviePlayer.scalingMode =MPMovieScalingModeAspectFit;
    [_moviePlayer.view setFrame: CGRectMake(0 ,0 , 380 ,320)];
}

//點擊播放按鈕
- (IBAction)play:(id)sender
{
    //添加視頻顯示內容
    [self.movieView addSubview:_moviePlayer.view];

    //添加視頻聲音內容
    [_moviePlayer prepareToPlay];

}

/**重寫該方法,控制該視圖控制器只支持橫屏顯示
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
 */
@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章