您的位置:首页 > 移动开发 > IOS开发

IOS播放音频 AVAudioPlayer(实例)

2014-10-17 17:07 513 查看
1. AVFoundation

Build Phases => Link Binary With Libraies => + => AVFoundation.framework => add

firstviewcontroller.h

C代码







#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface FirstViewController : UIViewController

{

__weak IBOutlet UILabel *label;

AVAudioPlayer *player;

}

- (IBAction)toplay:(id)sender;

@end

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FirstViewController : UIViewController
{
__weak IBOutlet UILabel *label;
AVAudioPlayer *player;
}

- (IBAction)toplay:(id)sender;

@end


firstviewcontroller.m

C代码







- (IBAction)toplay:(id)sender

{

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/test.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *error;

player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

player.numberOfLoops = -1;

[player play];

[label setText:@"Play ..."];

}

- (IBAction)toplay:(id)sender
{
NSURL  *url = [NSURL fileURLWithPath:[NSString  stringWithFormat:@"%@/test.mp3",  [[NSBundle mainBundle]  resourcePath]]];

NSError  *error;
player  = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

player.numberOfLoops = -1;
[player play];

[label setText:@"Play ..."];
}


或者:

C代码







NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];

NSError *error;

player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:&error];

[player prepareToPlay];

player.numberOfLoops = -1;

[player play];

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
NSError  *error;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:&error];
[player prepareToPlay];
player.numberOfLoops = -1;
[player play];


test.mp3 拖放到 Supporting Files 文件夹。

播放,暂停和停止

C代码







[player play]; //播放

[player pause]; //暂停

[player stop]; //停止

[player play];  //播放
[player pause]; //暂停
[player stop];  //停止


更多功能:

1. 音量:

Java代码







player.volume=0.8;//0.0~1.0之间

player.volume=0.8;//0.0~1.0之间


2. 循环次数

C代码







player.numberOfLoops = 3;//默认只播放一次 负数(-1)为无限循环

player.numberOfLoops = 3;//默认只播放一次 负数(-1)为无限循环


3.播放位置

C代码







player.currentTime = 15.0;//可以指定从任意位置开始播放

player.currentTime = 15.0;//可以指定从任意位置开始播放


3.1 显示当前时间

C代码







NSLog(@"%f seconds played so far", player.currentTime);

NSLog(@"%f seconds played so  far", player.currentTime);


4.声道数

C代码







NSUInteger channels = player.numberOfChannels;//只读属性

NSUInteger channels = player.numberOfChannels;//只读属性


5.持续时间

C代码







NSTimeInterval duration = player.dueration;//获取采样的持续时间

NSTimeInterval duration = player.dueration;//获取采样的持续时间


6.仪表计数

C代码







player.meteringEnabled = YES;//开启仪表计数功能

[ player updateMeters];//更新仪表读数

//读取每个声道的平均电平和峰值电平,代表每个声道的分贝数,范围在-100~0之间。

for(int i = 0; i<player.numberOfChannels;i++){

float power = [player averagePowerForChannel:i];

float peak = [player peakPowerForChannel:i];

}

player.meteringEnabled = YES;//开启仪表计数功能
[ player updateMeters];//更新仪表读数
//读取每个声道的平均电平和峰值电平,代表每个声道的分贝数,范围在-100~0之间。
for(int i = 0; i<player.numberOfChannels;i++){
float power = [player averagePowerForChannel:i];
float peak = [player peakPowerForChannel:i];
}


7. 初始化播放器

C代码







[player prepareToPlay];

[player prepareToPlay];


8. 判断是否正在播放

C代码







[player isPlaying]

[player isPlaying]


9、代理方法

加入播放出现异常,或者被更高级别的系统任务打断,我们的程序还没来得及收场就挂了,怎么办?不急,我们可以通过几个委托方法很好地处理所有的情形。

首先给player设置委托是必须的:

C代码







player.delegate = self;

player.delegate = self;


C代码







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

//播放结束时执行的动作

}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{

//解码错误执行的动作

}

- (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{

//处理中断的代码

}

- (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{

//处理中断结束的代码

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{
//播放结束时执行的动作
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{
//解码错误执行的动作
}
- (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{
//处理中断的代码
}
- (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{
//处理中断结束的代码
}


参考:

http://blog.csdn.net/xys289187120/article/details/6595919

/content/565108.html

视频:

http://www.youtube.com/watch?v=kCpw6iP90cY

2. AudioToolbox

Build Phases => Link Binary With Libraies => + => AudioToolbox.framework => add

firstviewcontroller.h

C代码







#import <UIKit/UIKit.h>

#import <AudioToolbox/AudioToolbox.h>

@interface FirstViewController : UIViewController

{

}

- (IBAction)toplay:(id)sender;

@end

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

@interface FirstViewController : UIViewController
{
}

- (IBAction)toplay:(id)sender;

@end


firstviewcontroller.m

C代码







- (IBAction)toplay:(id)sender

{

CFBundleRef mainBundle = CFBundleGetMainBundle();

CFURLRef soundFileURLRef;

soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR ("wav"), NULL);

UInt32 soundID;

AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);

AudioServicesPlaySystemSound(soundID);

}

- (IBAction)toplay:(id)sender
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR ("wav"), NULL);

UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}


视频:

http://www.youtube.com/watch?v=lSJhYx28Krg&feature=youtu.be
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: