IOS开发中如何调用音频播放组件
1、与音频相关的头文件等都在AVFoundation.h中,所以第一步是添加音频库文件:
#import <AVFoundation/AVFoundation.h>
2、先找一段要播放的音频文件,我这里找到是一个mp3文件,然后添加到项目中:
3、使用NSBundle获取这个文件并转化成NSData类型:
NSString *filepath = [[NSBundle mainBundle]pathForResource:@"bbb" ofType:@"mp3"];
NSData *data = [[NSData data]initWithContentsOfFile:filepath];
4、声明一个全局变量的AVAudioPlayer对象player:
@interface ViewController (){
AVAudioPlayer *player;
} @end
5、初始化全局变量player:
player = [[AVAudioPlayer alloc]initWithData:data error:nil];
6、添加一个开始、暂停播放的按钮:
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
myBtn.backgroundColor = [UIColor orangeColor];
myBtn.layer.cornerRadius = ;
myBtn.layer.masksToBounds = YES;
myBtn.enabled = YES;
[myBtn setTitle:@"开始播放" forState:UIControlStateNormal];
[myBtn addTarget:self action:@selector(myBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myBtn];
7、编写按钮的方法,点击按钮之后检测player是否在播放,如果在播放,就停止播放,如果未播放,就开始播放,其中停止播放和开始播放调用的是player对象的play方法和pause方法:
-(void)myBtnAction :(UIButton*)sender{ if (player.playing) {
[player pause];
[sender setTitle:@"开始按钮" forState:UIControlStateNormal];
}else{
[player play];
[sender setTitle:@"暂停按钮" forState:UIControlStateNormal];
}
}
8、添加一个音量滑块,音量滑块添加一个方法volumeslidervaluechange:,当值改变时触发这个方法:
// 音量滑块
UISlider *volumeslider = [[UISlider alloc]initWithFrame:CGRectMake(, , , )];
volumeslider.value = 0.9;
[volumeslider addTarget:self action:@selector(volumeslidervaluechange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:volumeslider];
9、实现volumeslidervaluechange:方法,当滑块值改变时,改变音频播放器的音量大小:
-(void)volumeslidervaluechange:(UISlider*)sender{
player.volume = sender.value;
}
模拟运行并点击按钮就可以听到美妙的音乐了:
以下是完整代码:
//
// ViewController.m
// ceshi
//
// Created by mac on 16/5/24.
// Copyright © 2016年 mzw. All rights reserved.
// #import "ViewController.h"
#import <AVFoundation/AVFoundation.h> @interface ViewController (){
AVAudioPlayer *player;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// 设置当前view背景色
self.view.backgroundColor = [UIColor lightGrayColor]; // 获取项目树中的音频文件
NSString *filepath = [[NSBundle mainBundle]pathForResource:@"bbb" ofType:@"mp3"];
// 把获取的音频文件转化为nsdata类型
NSData *data = [[NSData data]initWithContentsOfFile:filepath]; // 初始化一个音频播放器
player = [[AVAudioPlayer alloc]initWithData:data error:nil]; // 创建一个button,点击button开始或暂停音频播放器的播放
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
myBtn.backgroundColor = [UIColor orangeColor];
myBtn.layer.cornerRadius = ;
myBtn.layer.masksToBounds = YES;
myBtn.enabled = YES;
[myBtn setTitle:@"开始播放" forState:UIControlStateNormal];
[myBtn addTarget:self action:@selector(myBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myBtn]; // 音量滑块
UISlider *volumeslider = [[UISlider alloc]initWithFrame:CGRectMake(, , , )];
volumeslider.value = 0.9;
[volumeslider addTarget:self action:@selector(volumeslidervaluechange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:volumeslider]; } -(void)myBtnAction :(UIButton*)sender{
// 如果音频播放器处于播放状态,点击后就暂停播放,如果处于未播放状态,就开始播放
if (player.playing) {
[player pause];
[sender setTitle:@"开始按钮" forState:UIControlStateNormal];
}else{
[player play];
[sender setTitle:@"暂停按钮" forState:UIControlStateNormal];
}
} -(void)volumeslidervaluechange:(UISlider*)sender{
player.volume = sender.value;
} @end