关闭

MediaPlayer简单使用

标签: mediaplayer
640人阅读 评论(0) 收藏 举报
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface  MediaPlayerController: UIViewController
{
    AVPlayerLayer *videoPlayerLayer;
	AVPlayer *player;
    NSTimer *timer;
}
@property(nonatomic, retain) AVPlayerLayer *videoPlayerLayer;
@property (strong, nonatomic) IBOutlet UIView  *vidoView;
-(void)onPlay;
@end



#import "MediaPlayerController.h"

@implementation MediaPlayerController
@synthesize videoPlayerLayer;
@synthesize vidoView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"splash-30s" ofType:@"mp4"];
    NSURL *videoUrl = [NSURL fileURLWithPath:videoPath];
    player = [[AVPlayer alloc] initWithURL:videoUrl];
    
    //self.view.frame  = CGRectMake (0, 0, 1024, 748);
    videoPlayerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
    self.view.backgroundColor=[UIColor colorWithWhite:1.0 alpha:1.0];
    videoPlayerLayer.videoGravity = AVLayerVideoGravityResize;
    videoPlayerLayer.frame = CGRectMake (0, 0, 320, 400);
    [vidoView.layer addSublayer:videoPlayerLayer];
    
    timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onPlay) userInfo:nil repeats:NO];
 
}

-(void)onPlay
{
    videoPlayerLayer.hidden = NO;
    [player play];
    
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:108383次
    • 积分:1799
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:48篇
    • 译文:6篇
    • 评论:10条
    最新评论