标签:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController (){
AVPlayer *player;
UISlider *slider;
UILabel *label;
UISlider *volumeSlider;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlPath=[[NSBundle mainBundle]pathForResource:@"01-搭建基本界面" ofType:@"mp4"];
NSURL *url=[[NSURL alloc]initFileURLWithPath:urlPath];
// NSString *urlPath=@"http://share.vrs.sohu.com/2516501/v.swf&topBar=1&autoplay=false&plid=6986106&pub_catecode=0&from=page";
// NSURL *url=[[NSURL alloc]initWithString:urlPath];
//AVURLAsset继承自AVASet,媒体的抽象类
AVURLAsset *movieAsset=[[AVURLAsset alloc]initWithURL:url options:nil];
//资源管理对象以及演示设置(资源缓冲状态)
AVPlayerItem *playerItem=[AVPlayerItem playerItemWithAsset:movieAsset];
//播放器
player=[AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer=[AVPlayerLayer playerLayerWithPlayer:player];
layer.frame=self.view.frame;
layer.videoGravity=AVLayerVideoGravityResizeAspect;
[self.view.layer addSublayer:layer];
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//显示黑条
UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(0, (self.view.frame.size.height-30), self.view.frame.size.width,self.view.frame.size.height)];
blackView.backgroundColor=[UIColor grayColor];
//进度条
slider=[[UISlider alloc]initWithFrame:CGRectMake(40,280, 400,50)];
slider.value=0;
slider.minimumValue=0;
slider.maximumValue=1;
[slider setMinimumTrackImage:[UIImage imageNamed:@"播放进度条2"] forState:0];
[slider setMaximumTrackImage:[UIImage imageNamed:@"播放进度条"] forState:0];
[slider setThumbImage:[UIImage imageNamed:@"进度滑块"] forState:UIControlStateNormal];
[slider addTarget:self action:@selector(changeProgress:) forControlEvents:UIControlEventTouchUpInside];
//label
label=[[UILabel alloc]initWithFrame:CGRectMake(440, 285, 100 , 40)];
label.font=[UIFont systemFontOfSize:15];
//label.backgroundColor=[UIColor redColor];
//播放/暂停
UIButton *stopButton=[UIButton buttonWithType:UIButtonTypeCustom];
stopButton.frame=CGRectMake(10, 288, 30, 35);
[stopButton setImage:[UIImage imageNamed:@"播放按钮"] forState:UIControlStateNormal];
[stopButton addTarget:self action:@selector(doMovie:) forControlEvents:UIControlEventTouchUpInside];
//音量
//UISlider *volumeSlider=[[UISlider alloc]initWithFrame:CGRectMake(540, 285, 30, 28)];
UIButton *volumeButton=[UIButton buttonWithType:UIButtonTypeCustom];
[volumeButton setImage:[UIImage imageNamed:@"音量"] forState:UIControlStateNormal];
volumeButton.frame=CGRectMake(530, 290, 35, 35);
[volumeButton addTarget:self action:@selector(doVolume:) forControlEvents:UIControlEventTouchUpInside];
volumeSlider=[[UISlider alloc]initWithFrame:CGRectMake( 500, 240, 100,20)];
//旋转90度
volumeSlider.transform=CGAffineTransformMakeRotation(M_PI*1.5);
volumeSlider.minimumValue=0;
volumeSlider.maximumValue=1;
volumeSlider.value=0.1;
volumeSlider.hidden=YES;
[volumeSlider setThumbImage:[UIImage imageNamed:@"音量滑块"] forState:UIControlStateNormal];
[volumeSlider addTarget:self action:@selector(doChangeVolumeValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:blackView];
[self.view addSubview:slider];
[self.view addSubview:label];
[self.view addSubview:stopButton];
[self.view addSubview:volumeButton];
[self.view addSubview:volumeSlider];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"status"]) {
if (AVPlayerItemStatusReadyToPlay==player.currentItem.status) {
[player play];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(showMovieTime:) userInfo:nil repeats:YES];
}
}
}
-(void)doChangeVolumeValue:(UISlider *)sender{
player.volume=sender.value;
}
-(void)doVolume:(UIButton *)sender{
static BOOL isAppear=YES;
if (isAppear) {
volumeSlider.hidden=NO;
isAppear=!isAppear;
}
else {
volumeSlider.hidden=YES;
isAppear=!isAppear;
}
}
-(void)doMovie:(UIButton *)sender{//暂停播放
static BOOL isOk=YES;
if (isOk) {
[player pause];
[sender setImage:[UIImage imageNamed:@"暂停按钮"] forState:UIControlStateNormal];
isOk=!isOk;
}
else{
[player play];
[sender setImage:[UIImage imageNamed:@"播放按钮"] forState:UIControlStateNormal];
isOk=!isOk;
}
}
-(void)changeProgress:(UISlider *)sender{
float sliderValue=sender.value;
float allTime=[self playerDuration];
CMTime changeTime=CMTimeMake(sliderValue*allTime, 1);
[player.currentItem seekToTime:changeTime];
}
-(NSTimeInterval)playerDuration{
AVPlayerItem *item=player.currentItem;
if (item.status==AVPlayerItemStatusReadyToPlay) {
return CMTimeGetSeconds(item.duration);
}
return 0;
}
-(NSTimeInterval)playerCurrentime{
AVPlayerItem *item=player.currentItem;
if (item.status==AVPlayerItemStatusReadyToPlay) {
return CMTimeGetSeconds(item.currentTime);
}
return 0;
}
-(void)showMovieTime:(NSTimer *)timer{
float allTime=[self playerDuration];
float currentime=[self playerCurrentime];
int allM=(int)allTime/60;
int allS=(int)allTime %60;
int currentS=(int)currentime %60;
int currentM=currentime /60;
NSString *timeString=[NSString stringWithFormat:@"%02d:%02d/%02d:%02d",currentM,currentS,allM,allS];
label.text=timeString;
}
@end
标签:
原文地址:http://www.cnblogs.com/OIMM/p/4779214.html