代码改变世界

音乐播放器 AVAudioPlayer、定时器、UISlider

2015-11-10 16:13  xiangjune  阅读(367)  评论(0编辑  收藏  举报

 

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) AVAudioPlayer *mediaPlayer;

@property (strong, nonatomic) UIButton *playBtn;

@property (strong, nonatomic) UIButton *stopBtn;

@property (strong, nonatomic) UIButton *pauseBtn;

@property (strong, nonatomic) UILabel *showLabel;

@property (strong, nonatomic) UISlider *processView;

@end

 

@implementation ViewController

 

NSTimer *timer;

NSString *totalTimeShowText;

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 播放器初始化 - 文件路径

    NSURL *playFileUrl = [[NSBundle mainBundle]URLForResource:@"test" withExtension:@"mp3"];

    _mediaPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:playFileUrl error:nil];

    // 播放按钮

    _playBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    _playBtn.backgroundColor = [UIColor grayColor];

    [_playBtn setTitle:@"play" forState:UIControlStateNormal];

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

    [self.view addSubview:_playBtn];

    //暂停按钮

    _stopBtn = [[UIButton alloc]initWithFrame:CGRectMake(300, 100, 100, 50)];

    _stopBtn.backgroundColor = [UIColor grayColor];

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

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

    [self.view addSubview:_stopBtn];

    //停止按钮

    _pauseBtn = [[UIButton alloc]initWithFrame:CGRectMake(500, 100, 100, 50)];

    _pauseBtn.backgroundColor = [UIColor grayColor];

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

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

    [self.view addSubview:_pauseBtn];

    // 显示文字

    _showLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 800, 50)];

    _showLabel.textColor = [UIColor redColor];

    int totalTime = _mediaPlayer.duration;

    int minuts = totalTime/60;

    int seconds = totalTime%60;

    NSString *minusShow = minuts < 10 ? [NSString stringWithFormat:@"0%i", minuts] : [NSString stringWithFormat:@"%i", minuts];

    NSString *secondsShow = seconds < 10 ? [NSString stringWithFormat:@"0%i", seconds] : [NSString stringWithFormat:@"%i", seconds];

    totalTimeShowText = [NSString stringWithFormat:@"%@:%@", minusShow, secondsShow];

    _showLabel.text = [NSString stringWithFormat:@"00:00 %@", totalTimeShowText];

    [self.view addSubview:_showLabel];

    

    // 进度

    _processView = [[UISlider alloc]initWithFrame:CGRectMake(100, 260, 500, 15)];

    [_processView addTarget:self action:@selector(slideProcess) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_processView];

    

}

 

// 播放

-(void)play

{

    NSLog(@"play...");

    if(!_mediaPlayer.isPlaying)

    {

        [_mediaPlayer play];

    }

    

    if(!timer)

    {

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

    }

}

//停止

-(void)stop

{

    if(_mediaPlayer.isPlaying)

    {

        [_mediaPlayer stop];

    }

}

//停止

-(void)pause

{

    [_mediaPlayer pause];

}

// 更新进度

-(void)upgrocess

{

    _processView.value = _mediaPlayer.currentTime/_mediaPlayer.duration;

    

    int minus = _mediaPlayer.currentTime/60;

    int seconds = (int)_mediaPlayer.currentTime%60;

    NSString *minusShow = minus < 10 ? [NSString stringWithFormat:@"0%i", minus] : [NSString stringWithFormat:@"%i", minus];

    NSString *secondsShow = seconds < 10 ? [NSString stringWithFormat:@"0%i", seconds] : [NSString stringWithFormat:@"%i", seconds];

    

    NSString *currentTimeText = [NSString stringWithFormat:@"%@:%@", minusShow, secondsShow];

    _showLabel.text = [NSString stringWithFormat:@"%@ / %@", currentTimeText, totalTimeShowText];

}

// 拖动

-(void)slideProcess

{

    _mediaPlayer.currentTime = _mediaPlayer.duration * _processView.value;

    [self upgrocess];

}