先来介绍一下AVAudioPlayer:
AVAudioPlayer简介
在任何时间播放声音
播放声音文件或者内存的
循环播放声音
在同一时刻,播放多个声音,一个声音对应一个audio player,精确的 同步
控制相关播放的级别,立体声的位置, 你正在播放的每一个声音的速率
追种一个特别的点在一个声音文件中,前进或者后退到那个点上;
获得你可以使用的播放级别的测量数据
——— 注意:用代理去处理中断(像来电话了)和 去更新用户的界面,当音频播放完之后。
——— 注意:支持的音频格式有:AAC,ALAC,HE-AAC,iLBC,IMA4,MP3.
——— 注意 : 大家要特别注意了, AVAudioPlayer是专门播放本地音乐的,网络音乐是不可以播放的
再看一下他官方给的常用的API
主要API:
初始化:
- (instancetype nullable)initWithContentsOfURL:(NSURL * nonnull)url error:(NSError * nullable * nullable)outError
播放:
- (BOOL)play
暂停:
- (void)pause
停止:
- (void)stop
准备播放音频,返回值标志是否解析成功,是否可以播放。
- (BOOL)prepareToPlay
获取是否正在播放
@property(readonly, getter=isPlaying) BOOL playing
1
设置音频音量,取值为0-1之间
@property float volume
1
获取当前音频的总时长
@property(readonly) NSTimeInterval duration
AVAudioPlayerDelegate(简介)
用它来相应音频中断和音频解码错误,来实现音频的从放
完成播放(这个Delegate是最常用的, 一首音乐播放完成调用此方法来播放下一首, 实现连续播放)
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer * nonnull)playersuccessfully:(BOOL)flag
解码错误:
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer * nonnull)playererror:(NSError * nullable)error
处理中断: (一共四个,IOS9全部弃用)
- (void)audioPlayerBeginInterruption:(AVAudioPlayer * nonnull)player
1
下面来看一下代码的实现:
// ViewController.m
// MusicPlayer
//
// Created by dlios on 15/10/6.
// Copyright © 2015年 dlios. All rights reserved.
//
#import "ViewController.h"
//导入系统库
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@property (nonatomic, retain)AVAudioPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view setBackgroundColor:[UIColor whiteColor]];
[self getButtonControl];
[self getPlayer];
}
#pragma mark - 创建按钮和进度滑条方法
- (void)getButtonControl
{
//创建播放按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 40, 20)];
button.center = CGPointMake(375 / 2, 400);
[button setTitle:@"播放" forState:UIControlStateNormal];
[button setTitle:@"暂停" forState:UIControlStateSelected];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
/* 声音slider */
//创建slider
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
slider.center = CGPointMake(375 / 2, 450);
/* 最大值 */
[slider setMaximumValue:100.];
/* 最小值 */
[slider setMinimumValue:0.];
/* 显示颜色 */
[slider setMaximumTrackTintColor:[UIColor purpleColor]];
/* 滑动后颜色 */
[slider setMinimumTrackTintColor:[UIColor cyanColor]];
/* 滑动就调用 */
[slider setContinuous:YES];
/* 滑动事件 */
[slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
/* 进度slider */
UISlider *schSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
schSlider.center = CGPointMake(375 / 2, 500);
[schSlider setMaximumValue:280.];
[schSlider setMinimumValue:0.];
[schSlider setContinuous:YES];
[schSlider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:schSlider];
}
#pragma mark - 播放方法
- (void)getPlayer
{
/* 获取本地文件 */
NSBundle *bundle = [NSBundle mainBundle];
NSString *urlString = [bundle pathForResource:@"李荣浩-模特" ofType:@"mp3"];
/* 初始化url */
NSURL *url = [[NSURL alloc] initFileURLWithPath:urlString];
/* 初始化音频文件 */
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
/* 加载缓冲 */
[self.player prepareToPlay];
}
#pragma mark - button 点击方法
- (void)click:(UIButton *)button
{
if (button.selected == NO) {
[self.player play];
button.selected = YES;
}else{
button.selected = NO;
[self.player pause];
}
}
#pragma mark - 声音滑动事件方法
- (void)slider:(UISlider *)slider
{
self.player.volume = slider.value;
}
#pragma mark - 进度滑动事件
- (void)sliderClick:(UISlider *)slider
{
self.player.currentTime = slider.value;
}
#pragma mark - player Dlegate
//结束时调用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"结束了");
}
//解析错误调用
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end