zoukankan      html  css  js  c++  java
  • IOS多媒体

    音乐播放器

      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 
      5 @end
      6 
      7 @implementation ViewController
      8 
      9 - (void)viewDidLoad {
     10     [super viewDidLoad];
     11     // Do any additional setup after loading the view, typically from a nib.
     12     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     13     dispatch_async(queue, ^{
     14         // 创建一个定时器
     15         [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
     16         
     17         // 保持线程活跃
     18         [[NSRunLoop currentRunLoop] run];
     19     });
     20 
     21 //    播放
     22     play1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     23     play1.frame = CGRectMake(30, 360, 90, 35);
     24     play1.backgroundColor = [UIColor redColor];
     25     [play1 setTitle:@"播放" forState:UIControlStateNormal];
     26     [play1 addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
     27     
     28     [self.view addSubview:play1];
     29 //    停止播放
     30     stop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     31     stop.frame = CGRectMake(30, 260, 90, 35);
     32     stop.backgroundColor = [UIColor redColor];
     33     [stop setTitle:@"停止" forState:UIControlStateNormal];
     34     [stop addTarget:self action:@selector(stopAction:) forControlEvents:UIControlEventTouchUpInside];
     35     [self.view addSubview:stop];
     36 //    暂停播放
     37     pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     38     pause.frame = CGRectMake(30, 160, 90, 35);
     39     pause.backgroundColor = [UIColor redColor];
     40     [pause setTitle:@"暂停" forState:UIControlStateNormal];
     41     [pause addTarget:self action:@selector(pauseAction:) forControlEvents:UIControlEventTouchUpInside];
     42     [self.view addSubview:pause];
     43 //    声音UILabel
     44     sound = [[UILabel alloc] initWithFrame:CGRectMake(90, 210, 100, 20)];
     45     sound.text = @"声音:";
     46     [self.view addSubview:sound];
     47 //    声音UISlider
     48     audioPlayer = [[UISlider alloc] initWithFrame:CGRectMake(150, 170, 200, 100)];
     49     [audioPlayer addTarget:self action:@selector(audioSilderChanged:) forControlEvents:UIControlEventValueChanged];
     50     
     51     [self.view addSubview:audioPlayer];
     52 //    进度UISlider
     53     _timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(160, 270, 150, 100)];
     54     [_timeSlider addTarget:self action:@selector(timeSliderChanged:) forControlEvents:UIControlEventValueChanged];
     55     [self.view addSubview:_timeSlider];
     56 //    当前进度
     57     _startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 270, 100, 100)];
     58     [self.view addSubview:_startTimeLabel];
     59 //    音乐总长
     60     _endTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(320, 270, 100, 100)];
     61     [self.view addSubview:_endTimeLabel];
     62     _audioPlayer.delegate = self;
     63     
     64   
     65 }
     66 - (void)timerAction:(NSTimer *)timer
     67 {
     68     // 回到主线程刷新UI
     69     dispatch_async(dispatch_get_main_queue(), ^{
     70         _startTimeLabel.text = [NSString stringWithFormat:@"%d",(int)_audioPlayer.currentTime];
     71         _timeSlider.value = _audioPlayer.currentTime;
     72     });
     73 }
     74 
     75 -(void)pauseAction:(UIButton *)pause{
     76     [_audioPlayer pause];
     77 }
     78 -(void)stopAction:(UIButton *)stop{
     79     [_audioPlayer stop];
     80     _audioPlayer = nil;
     81 }
     82 -(void)audioSilderChanged:(UISlider *)audio{
     83     UISlider *slider = (UISlider *)audioPlayer;
     84     // 7.设置音量的大小
     85     _audioPlayer.volume = slider.value;
     86     
     87     
     88 }
     89 -(void)playAction:(UIButton *)pl{
     90     if (_audioPlayer == nil) {
     91         // 1.音乐文件的路径
     92         NSString *urlString = [[NSBundle mainBundle] pathForResource:@"王绎龙-邻家美眉" ofType:@"mp3"];
     93         // 2.转化成本地路径
     94         NSURL *url = [NSURL fileURLWithPath:urlString];
     95         // 3.创建播放对象
     96         _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
     97         _audioPlayer.numberOfLoops = -1;
     98 
     99         // 5.记录音乐的总时长   duration声音的持续时间
    100         _startTimeLabel.text = @"0";
    101         //    音乐总长UILabel
    102         _endTimeLabel.text = [NSString stringWithFormat:@"%d",(int)_audioPlayer.duration];
    103         // 6.设置滑条的参数
    104         _timeSlider.minimumValue = 0;
    105         _timeSlider.maximumValue = _audioPlayer.duration;
    106         // 7.设置音量的大小
    107         _audioPlayer.volume = 0.0;
    108     }
    109     
    110     // 5.播放
    111     [_audioPlayer play];
    112 }
    113 -(void)timeSliderChanged:(UISlider *)time{
    114     // 播放进度的改变
    115     _audioPlayer.currentTime = _timeSlider.value;
    116 }
    117 
    118 @end
  • 相关阅读:
    grunt安装
    RequireJS实例分析
    Linux下解压rar文件
    windows(64位)下使用curl命令
    RequireJS学习资料汇总
    Linux下firefox安装flash player插件
    caj转pdf——包含下载链接
    《社会化营销:人人参与的营销力量》—— 读后总结
    《税的真相》—— 读后总结
    基于代理的数据库分库分表框架 Mycat实践
  • 原文地址:https://www.cnblogs.com/liuguan/p/5052371.html
Copyright © 2011-2022 走看看