AVAudioPlayer的使用

AVAudioPlayer的使用

From: http://hi.baidu.com/loveipon/item/367747a64e1cf639a8cfb7c8

首先引用AVFoundation.framework

.h文件

    AVAudioPlayer *player;

    //用到了拖控件(进度条)
    IBOutlet UIProgressView *_progressView;


.m文件

//此方法连接到一个已经拖在视图上的button控件的触发事件上

-(IBAction)play:(id)sender{

  //找到要播放的音频
    NSString *_path=[[NSBundle mainBundle]pathForResource:@"张玮-high歌 中国好声音"ofType:@"mp3"];
    //文件的URL(如果是文件的URL,使用fileURLWithPath
    NSURL *_url=[NSURL fileURLWithPath:_path];
    NSError *error;
    player=[[AVAudioPlayer alloc]initWithContentsOfURL:_url
                                                            error:&error];
    if (error) {
        NSLog(@"error:%@",[error description]);
        return;
    }

//准备播放
    [player prepareToPlay];

//播放
    [player play];
    _progressView.progress=0.0f;

//用到了时间触发器,刷新进度条
    [NSTimer scheduledTimerWithTimeInterval:0.5
                                     target:self
                                   selector:@selector(refreshUR:)
                                   userInfo:nil
                                    repeats:YES];
    
}

//刷新事件

-(void)refreshUR:(NSTimer *)aTimer{
    float p=player.currentTime/player.duration;//当前时间、总时间
    _progressView.progress=p;
}

//一下三个方法都是连接到已经布置好的BUTTON触发事件上的

//重新播放

-(IBAction)rePlay:(id)sender{
     player.currentTime=0.0f;
    [ player play];

}

//暂停

-(IBAction)pause:(id)sender{
    [ player pause];
}
//停止
-(IBAction)stop:(id)sender{
    if ( player.isPlaying) {
        [ player stop];
         player.currentTime=0.0f;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值