AvaudioPlayer

 

//

//  ViewController.m

//  播放

//

//  Created by 草帽~小子 on 2017/6/30.

//  Copyright © 2017年 HLJ. All rights reserved.

//

 

 

#import "ViewController.h"

 

#import <AVFoundation/AVFoundation.h>

 

@interface ViewController ()<AVAudioPlayerDelegate>

 

@property (nonatomic, strong) AVAudioPlayer *avPlayer;

@property (nonatomic, strong) NSTimer *timer;

 

@property (nonatomic, strong) UIProgressView *progress;

 

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    

    UIButton *play = [UIButton buttonWithType:UIButtonTypeSystem];

    play.frame = CGRectMake(75, 200, 100, 50);

    play.backgroundColor = [UIColor orangeColor];

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

    [self.view addSubview:play];

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

    

    

    UIButton *pause = [UIButton buttonWithType:UIButtonTypeSystem];

    pause.frame = CGRectMake(200, 200, 100, 50);

    pause.backgroundColor = [UIColor orangeColor];

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

    [self.view addSubview:pause];

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

    

    self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width - 40, 200)];

    self.progress.backgroundColor = [UIColor cyanColor];

    self.progress.tintColor = [UIColor purpleColor];

    self.progress.progressViewStyle = UIProgressViewStyleDefault;

    [self.view addSubview:self.progress];

    

    //更改进度条高度

    //self.progress.transform = CGAffineTransformMakeScale(1.0f,20.0f);

    

    

    //UIImage* imgTemp = [UIImage imageNamed:@"btn_sound_h"];

    //CGFloat width = imgTemp.size.width/2.0;

    //CGFloat height = imgTemp.size.height/2.0;

    

    //UIImage *img = [[UIImage imageNamed:@"btn_manual_n"] resizableImageWithCapInsets:UIEdgeInsetsMake(height, width, height, width)];

    

    //UIImage *imgProgress = [[UIImage imageNamed:@"progress_on.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(height, width, height, width)];

    //[self.progress setProgressImage:imgProgress];

    //[self.progress setTrackImage:img];

    ///self.progress.progressImage = img;

    //背景图片

    //self.progress.trackImage = [UIImage imageNamed:@"btn_sound_h"];

    

    NSLog(@"sdfasdfasfs%@", self.progress.observedProgress);

    

    // Do any additional setup after loading the view, typically from a nib.

}

 

 

 

/**

 *定时器

 */

 

- (NSTimer *)timer {

    if (!_timer) {

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

    }

    return _timer;

}

 

- (void)timer:(NSTimer *)timer {

    float progress = self.avPlayer.currentTime / self.avPlayer.duration;

    [self.progress setProgress:progress animated:YES];

}

 

/*

 *创建播放器

 */

- (AVAudioPlayer *)avPlayer {

    if(!_avPlayer) {

        //NSURL *url = [[NSBundle mainBundle] URLForResource:@"AVAudioPlayer" withExtension:@"mp3"];

        //NSLog(@"%@", url);

        

        

        //关闭手机左侧静音按钮,会静音,用的手机铃声播放器

        NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"AVAudioPlayer.mp3" ofType:nil];

        NSURL *url=[NSURL fileURLWithPath:urlStr];

        

        NSError *error = nil;

        //初始化播放器,文件路径只支持url, 不支持http url

        _avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

        //播放器属性 -1 循环 

        _avPlayer.numberOfLoops = 0;

        _avPlayer.delegate = self;

        //加载文件到缓存区

        [_avPlayer prepareToPlay];

        if (error) {

            return nil;

        }

        

    }

    return  _avPlayer;

}

 

/**

 *播放音频

 */

 

- (void)play:(UIButton *)button {

    if (![_avPlayer isPlaying]) {

        [self.avPlayer play];

        self.timer.fireDate = [NSDate distantPast];

        NSLog(@"sfsfs");

    }

}

 

/**

 *暂停播放

 */

- (void)pause:(UIButton *)button {

    

    if ([_avPlayer isPlaying]) {

        [_avPlayer pause];

    

        self.timer.fireDate = [NSDate distantFuture];暂停定时器,注意不能调用invalidate方法,此方法会取消,之后无法恢复

    }

    

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值