AVAudioPlayer本地音乐播放

在音频播放方面,苹果公司推荐使用AVAudioPlayer。但网络上的流媒体音频播放除外。(流媒体播放使用AVPlayer)
AVAudioPlayer具有以下特点:(1)、播放任何时长的音频。(2)、播放来自文件或内存的音频数据。(3)、支持循环播放。(4)、能同时播放多个音频数据(需要实例化多个AVAudioPlayer对象。(5)、可以直接定位到音频播放的任一位置,支持快进和快退。</span>
使用前需要添加AVFoundation框架:
 
 
#import "QYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface QYViewController ()
@property (nonatomic, strong) UIButton *startOrPauseButton;
@property (nonatomic, strong) UIButton *stopButton;

@property (nonatomic, strong) UISlider *sliderVolume;
@property (nonatomic, strong) UISlider *sliderProgress;

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@property (nonatomic, strong) NSTimer *timer;
@end

@implementation QYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
//    从沙盒中读取音乐文件的路径 URLForResource:@"情非得已"是指需要播放的音乐名称 、withExtension:@"mp3"是指文件的音频格式。AVAudioPlayer可以播放任意一种音频格式
    NSURL *musicFileURL = [[NSBundle mainBundle] URLForResource:@"情非得已" withExtension:@"mp3"];
    NSError *error = nil;
//    实例化AVAudioPlayer对象
	_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFileURL error:&error];
    if (nil != error) {
        NSLog(@"%@", error);
        return;
    }
//    对AVAudioPlayer对象进行配置,-1表示无限循环
    self.audioPlayer.numberOfLoops = -1;
//    预播放
    [self.audioPlayer prepareToPlay];

    [self structureView4AudioPlayer];
}

//创建音乐播放的界面元素
- (void)structureView4AudioPlayer
{
    _sliderVolume = [[UISlider alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
    self.sliderVolume.value = 0.5f;
    self.audioPlayer.volume = self.sliderVolume.value;
    [self.sliderVolume addTarget:self action:@selector(onSliderVolumeValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.sliderVolume];
    
    _startOrPauseButton = [[UIButton alloc] initWithFrame:CGRectMake(110, 120, 100, 50)];
    [self.startOrPauseButton setTitle:@"Play" forState:UIControlStateNormal];
    [self.startOrPauseButton addTarget:self action:@selector(playOrPause:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.startOrPauseButton];
    
    _stopButton = [[UIButton alloc] initWithFrame:CGRectMake(110, 220, 100, 50)];
    [self.stopButton setTitle:@"Stop" forState:UIControlStateNormal];
    [self.stopButton addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.stopButton];
    
    _sliderProgress = [[UISlider alloc] initWithFrame:CGRectMake(10, 300, 300, 30)];
    self.sliderProgress.minimumValue = 0.0f;
    self.sliderProgress.maximumValue = self.audioPlayer.duration;
    [self.sliderProgress addTarget:self action:@selector(onSliderProgressValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.sliderProgress];
}

通过按钮的点击,控制音频文件的播放和停止</span>
#pragma mark -
#pragma mark on control button tapped
- (void)playOrPause:(UIButton *)sender
{
    if ([self.audioPlayer isPlaying]) {
//        如果正在播放音乐,那就让它暂停
        [self.audioPlayer pause];
        
        [self.timer setFireDate:[NSDate distantFuture]];
        
        [self.startOrPauseButton setTitle:@"Play" forState:UIControlStateNormal];
    }else {
        if (nil == self.timer) {
            _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
        }
        [self.timer setFireDate:[NSDate date]];
        
        [self.audioPlayer play];
        [self.startOrPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
    }
}

- (void)stop:(UIButton *)sender
{
    if ([self.audioPlayer isPlaying]) {
//        如果正在播放,就让它停止。需要注意的是stop和pause的区别:当要stop时,需要重置播放时长和进行预播放,为下一次播放做准备
        [self.audioPlayer stop];
        self.audioPlayer.currentTime = 0.0f;
        [self.audioPlayer prepareToPlay];
        
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.startOrPauseButton setTitle:@"Play" forState:UIControlStateNormal];
    }
}

调节slider的值的大小,对音频播放的进度和音量进行调节
#pragma mark -
#pragma mark on slider value changed callBack
- (void)onSliderVolumeValueChanged:(UISlider *)sender
{
    self.audioPlayer.volume = sender.value;
}

- (void)onSliderProgressValueChanged:(UISlider *)sender
{
    self.audioPlayer.currentTime = sender.value;
}

在音频播放过程中,需要定时器来实时地跟踪播放进度
#pragma mark -
#pragma mark on timer callBack
- (void)onTimer:(NSTimer *)timer
{
    self.sliderProgress.value = self.audioPlayer.currentTime;
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值