在视图中添加视频播放器 和 音频播放器

视频播放器 MPMoviePlayerController


#import "RootViewController.h"

#import <MediaPlayer/MediaPlayer.h>


@interface RootViewController ()


@property(nonatomic, retain) MPMoviePlayerController *moviePlayer;


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    

    NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"abc" ofType:@"mp4"];

    

    NSURL *url = [NSURL fileURLWithPath:urlStr];

    

    self.moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];

    

    self.moviePlayer.view.frame = self.view.frame;

    

    

    self.moviePlayer.repeatMode = YES; //循环播放

    

    

    //横竖屏自适应高度和宽度 

    self.moviewPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    

    [self.view addSubview:self.moviewPlayer.view];

    

    [_moviewPlayer release];

    

    [self.moviewPlayer play];

    

    

}


-------------------------------------------------------------------------------------------

音频播放器 AVAudioPlayer




#import "RootViewController.h"


//引入框架

#import <AVFoundation/AVFoundation.h>


//签协议

@interface RootViewController ()<AVAudioPlayerDelegate>



//

@property(nonatomic ,retain )AVAudioPlayer * avAudioPlayer;



//跐溜滑

@property(nonatomic ,retain) UISlider * slider ;




//另一个跐溜滑

@property(nonatomic ,retain ) UISlider * slider2;



//定时器


@property(nonatomic ,retain )NSTimer *timer ;




@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.navigationController.navigationBar.translucent = NO;


    self.view.backgroundColor = [ UIColor cyanColor ] ;

    

    

    

    

    

    

    

    

    NSString * string = [[NSBundle mainBundle ] pathForResource:@"走在冷风中" ofType:@"mp3"];

    

    NSURL * url = [NSURL fileURLWithPath:string];

    

    

    self.avAudioPlayer = [[AVAudioPlayer alloc ]initWithContentsOfURL:url error:nil];

    

    self.avAudioPlayer.delegate =self;

    

    

    

  

    

//三个button

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setFrame:CGRectMake(150, 150, 60, 40)];

    [button setTitle:@"Play" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button1 setFrame:CGRectMake(150, 200, 60, 40)];

    [button1 setTitle:@"pause" forState:UIControlStateNormal];

    [button1 addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button1];

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button2 setFrame:CGRectMake(150, 250, 60, 40)];

    [button2 setTitle:@"stop" forState:UIControlStateNormal];

    [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];


    

    

    

//音量大小

    self.avAudioPlayer.volume = 5;


    

    

    //设置音乐播放次数  -1为一直循环

    self.avAudioPlayer.numberOfLoops = -1;

    

    


//跐溜滑

    //声音

    self.slider = [[UISlider alloc ] initWithFrame:CGRectMake(100, 100, 200, 30)];

    

    

    self.slider.minimumValue = 0;

    

    self.slider.maximumValue = 20;

    

    self.slider.value =self.avAudioPlayer.volume ;

    

    [self.slider addTarget:self action:@selector(changeVoid:) forControlEvents:UIControlEventValueChanged];

    

    

    

    [self.view addSubview:self.slider];

    

    [_slider release];


    

    

    

    ///进度条

    

    self.slider2 = [[UISlider alloc ] initWithFrame:CGRectMake(100, 50, 200, 30)];

    

    self.slider2.minimumValue = 0 ;

    

    self.slider2.maximumValue = self.avAudioPlayer.duration ;//总时长

    

    self.slider2.value = 0 ;

    

    [self.slider2 addTarget:self action:@selector(changeTime:) forControlEvents:UIControlEventValueChanged];

    

    

    

    [self.view addSubview:self.slider2];

    

    [_slider2 release];


    

    

//开关 静音

    

    UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(150, 20, 60, 40)];

    [swith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];

    

    

    

    //默认状态为打开

    swith.on = YES;

    [self.view addSubview:swith];

    [swith  release];

    


    


    

    

}




//播放

- (void)play

{

    [self.avAudioPlayer play];

    

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeSlider2) userInfo:nil repeats:YES];

    

}



//暂停

- (void)pause

{

    [self.avAudioPlayer pause];

}



//停止

- (void)stop

{

    self.avAudioPlayer.currentTime = 0 //当前播放时间设置为0

    [self.avAudioPlayer stop];

}




-(void)changeVoid:(UISlider *)slider

{

    self.avAudioPlayer.volume = slider.value ;

    

}




-(void)changeTime:(id  ) slider

{

        self.avAudioPlayer.currentTime = self.slider2.value   ;

}



-(void)changeSlider2

{

    self.slider2.value = self.avAudioPlayer.currentTime  ;

    

    static int  i = 0 ;

    

    NSLog(@"%d",i++);


}




//播放完成时调用的方法  (代理里的方法),需要设置代理才可以调用

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    [self.timer invalidate]; //NSTimer暂停   invalidate  使...无效;

    


    

}



//声音开关(是否静音)

- (void)onOrOff:(UISwitch *)sender

{

    self.avAudioPlayer.volume = sender.on;

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值