OC-AVAudioPlayer的使用小记

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //设置音乐的后台播放,注意background mode中需要勾选上

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayback error:nil];

}

 
//开始播放
- (IBAction)player:(id)sender {

    [self.player play];

}

 
//暂停播放
- (IBAction)pause:(id)sender {

    [self.player pause];

}

 
//停止播放
- (IBAction)stop:(id)sender {

    [self.player stop];

}

 
//前进5s
- (IBAction)qianJin5s:(id)sender {

    self.player.currentTime += 5;

}

 
//后退5s
- (IBAction)houTui5s:(id)sender {

    self.player.currentTime -= 5;

}

 
//快速播放
- (IBAction)faster:(id)sender {

    self.player.rate = 2;

}

 
//播放一次
- (IBAction)playOnce:(id)sender {

    self.player.numberOfLoops = 0;

}

 
//播放3次
- (IBAction)playThirst:(id)sender {

    self.player.numberOfLoops = 2;

}

 
//循环播放
- (IBAction)playAllTheTime:(id)sender {

    self.player.numberOfLoops = -1;

}

 
//获取一些基础信息
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"deviceCurrentTime=%f",self.player.deviceCurrentTime);

    NSLog(@"currentTime=%f",self.player.currentTime);

    NSLog(@"duration=%f", self.player.duration);

    NSLog(@"settings=%@", self.player.settings);

    

}


//听筒播放

 

- (IBAction)tingTongPlay:(id)sender {

 
 
 

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:nil];

 
 
 

}

 
 
 

 

 
 
 

//扬声器播放

 
 
 

- (IBAction)outSpeakerPlayer:(id)sender {

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
 
 
 

}

//懒加载 

- (AVAudioPlayer *)player {

 

    if (!_player) {

 

        //获取播放的路径 paomo.mp3   2018-11-27 10_36_51 1.wav

 

        NSURL *path = [[NSBundle mainBundle] URLForResource:@"paomo.mp3" withExtension:nil];

 

        //根据路径创建播放对象

 

        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];

 

 

 

        //如果这个属性不设置的话,那么不能设置倍速播放的功能

 

        player.enableRate = YES;

 

        

 

        //准备播放

 

        [player prepareToPlay];

 

        _player = player;

 

    }

 

    return _player;

 

}

 

 

 

 

AVAudioPlayer 这个框架,去看看苹果开发文档,还比较容易理解,此处只是贴上代码作为一个记录而已。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值