iPhone/iPad高级应用与手游开发学习笔记:AVAudioPlayer学习(音频播放demo)

AVAudioPlayer是一个属于AVFoundation.framework的类,它的作用类似于一个功能强大的播放器,AVAudioPlayer支持广泛的音频格式。

一,新建一个simple view工程

二,在新建的工程中导入AVFoundation.framework库

三,再storyboard中拖入两个button,一个progress和一个label,如下图所示:


四,ViewController头文件的代码:

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface FKViewController : UIViewController<AVAudioPlayerDelegate>

@property(strong, nonatomic) IBOutlet UIButton *bn1;

@property(strong, nonatomic) IBOutlet UIButton *bn2;

@property(strong, nonatomic) IBOutlet UILabel *show;

@property(strong, nonatomic) IBOutlet UIProgressView *prog;


-(IBAction)play:(id)sender;

-(IBAction)stop:(id)sender;


@end


五,ViewController实现类:

#import "FKViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface FKViewController ()


@end


@implementation FKViewController


AVAudioPlayer *audioPlayer;

UIImage *playImage;

UIImage *pauseImage;

UIImage *stopImage;

CGFloat durationTime;

NSTimer *timer;


- (void)viewDidLoad

{

    [super viewDidLoad];

    playImage = [UIImage imageNamed:@"play.png"];

    pauseImage = [UIImage imageNamed:@"pause.png"];

    stopImage = [UIImage imageNamed:@"stop.png"];

    //为两个按钮设置图片

    [self.bn1 setImage:playImage forState:UIControlStateNormal];

    [self.bn2 setImage:stopImage forState:UIControlStateNormal];

    //获取要播放的音频文件的URL

    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"star" withExtension:@"mp3"];

    //创建AVAudioPlayer对象

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

    NSString *msg = [NSString stringWithFormat:@"音频文件的声道数:%d\n音频文件的持续时间:%g",audioPlayer.numberOfChannels,audioPlayer.duration];

    self.show.text = msg;

    durationTime = audioPlayer.duration;

    //将循环次数设为-1,用于指定该音频文件循环播放

    //audioPlayer.numberOfLoops = -1;

    audioPlayer.delegate = self;

}


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

{

    if(player == audioPlayer && flag) {

        NSLog(@"播放完毕!!");

        [self.bn1 setImage:playImage forState:UIControlStateNormal];

    }

}


-(void) audioPlayerBeginInterruption:(AVAudioPlayer *)player

{

    if(player == audioPlayer) {

        NSLog(@"被中断!");

    }

}


-(IBAction)play:(id)sender

{

    //如果当前正在播放

    if(audioPlayer.playing) {

        //暂停播放

        [audioPlayer pause];

        [sender setImage:playImage forState:UIControlStateNormal];

    } else {

        //播放音频

        [audioPlayer play];

        [sender setImage:pauseImage forState:UIControlStateNormal];

    }

    

    //如果timernil,执行如下方法

    if(timer == nil) {

        //周期性的执行某个方法

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

    }

}


-(IBAction)stop:(id)sender

{

    //定制播放音频

    [audioPlayer stop];

    [timer invalidate];

    timer = nil;

}


-(void) updateProg

{

    self.prog.progress = audioPlayer.currentTime/durationTime;

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


六,最后再把用到的图片加上,音乐文件大家可以自己随便找个mp3都可以:



七,最后效果:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值