IOS播放音频 AVAudioPlayer(实例)
1. AVFoundation
Build Phases => Link Binary With Libraies => + => AVFoundation.framework => add
firstviewcontroller.h
- #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
- - (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 ..."];
}
或者:
- 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 文件夹。
播放,暂停和停止
- [player play]; //播放
- [player pause]; //暂停
- [player stop]; //停止
[player play]; //播放
[player pause]; //暂停
[player stop]; //停止
更多功能:
1. 音量:
- player.volume=0.8;//0.0~1.0之间
player.volume=0.8;//0.0~1.0之间
2. 循环次数
- player.numberOfLoops = 3;//默认只播放一次 负数(-1)为无限循环
player.numberOfLoops = 3;//默认只播放一次 负数(-1)为无限循环
3.播放位置
- player.currentTime = 15.0;//可以指定从任意位置开始播放
player.currentTime = 15.0;//可以指定从任意位置开始播放
3.1 显示当前时间
- NSLog(@"%f seconds played so far", player.currentTime);
NSLog(@"%f seconds played so far", player.currentTime);
4.声道数
- NSUInteger channels = player.numberOfChannels;//只读属性
NSUInteger channels = player.numberOfChannels;//只读属性
5.持续时间
- NSTimeInterval duration = player.dueration;//获取采样的持续时间
NSTimeInterval duration = player.dueration;//获取采样的持续时间
6.仪表计数
- 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. 初始化播放器
- [player prepareToPlay];
[player prepareToPlay];
8. 判断是否正在播放
- [player isPlaying]
[player isPlaying]
9、代理方法
加入播放出现异常,或者被更高级别的系统任务打断,我们的程序还没来得及收场就挂了,怎么办?不急,我们可以通过几个委托方法很好地处理所有的情形。
首先给player设置委托是必须的:
- player.delegate = self;
player.delegate = self;
- - (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
http://blog.csdn.net/iukey/article/details/7295962
视频:
http://www.youtube.com/watch?v=kCpw6iP90cY
2. AudioToolbox
Build Phases => Link Binary With Libraies => + => AudioToolbox.framework => add
firstviewcontroller.h
- #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
- - (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
- MsicPlayer.zip (28.8 KB)
IOS播放音频 AVAudioPlayer(实例)的更多相关文章
- iOS 播放音频的几种方法
Phone OS 主要提供以下了几种播放音频的方法: System Sound Services AVAudioPlayer 类 Audio Queue Services OpenAL 1. Syst ...
- iOS播放器 - AVAudioPlayer
今天记录一下AVAudioPlayer,这个播放器类苹果提供了一些代理方法,主要用来播放本地音频. 其实也可以用来播放网络音频,只不过是将整个网络文件下载下来而已,在实际开发中会比较耗费流量不做推荐. ...
- IOS 播放音频
1,播放短音频 #import <AudioToolbox/AudioToolbox.h>#import "GLYViewController.h"static voi ...
- iOS 播放音频文件
// 播放音乐 NSString *path = [[NSBundle mainBundle] pathForResource:@"1670" ofType:@&qu ...
- IOS 播放音频流媒体
#pragma mark - 加载播放数据 - (void)loadData:(NSString *)musicUrl { NSURL *playURL = [NSURL URLWithString: ...
- Android应用开发学习笔记之播放音频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...
- iOS 9音频应用播放音频之第一个ios9音频实例2
iOS 9音频应用播放音频之第一个ios9音频实例2 ios9音频应用关联 iOS9音频应用中对于在主视图上添加的视图或控件,在使用它们时必须要与插座变量进行关联.ios9插座变量其实就是为主视图中的 ...
- iOS 9音频应用播放音频之第一个ios9音频实例
iOS 9音频应用播放音频之第一个ios9音频实例 第一个ios9音频实例 为了让开发者可以对上面的内容有更加深入的了解,本节将实现播放音频的第一个实例.在此实例中会涉及到项目的创建.界面设计.关联以 ...
- iOS AVAudioPlayer播放音频时声音太小
iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...
随机推荐
- spl_autoload_register()和__autoload()区别
这篇文章主要介绍了spl_autoload_register()和__autoload()区别,需要的朋友可以参考下 关于spl_autoload_register()和__autoload(), ...
- delphi xe5 android tts(Text To Speech)
TTS是Text To Speech的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话. 以下代码实现xe5 开发的文本转语音的方法 和访问蓝牙一样,这里用javaclass的接口实现 接 ...
- 关于JDK中的运算符和变量
类名首字母必须大写.多个单词组成的类名,每个单词的首字母大写. 只要起名称就要让他有意义.Java中的关键字都是由小写字母组成的. 在项目中给标示符起名字在公司中大都有固定的规则.一般加上标示符和$符 ...
- poj 3177 边连通分量
思路: dfs求出所有点的low值,然后对每个连通分量进行缩点,可以通过low来进行缩点.虽然在同一连通分量里可能存在不同的low值,但这并不影响缩点.将每个连通分量缩为一个点后,只要求出这个缩点后的 ...
- Windows 7 IE11 F12 不能正常使用
打开任意网站,按下F12,或者右键鼠标,按L键.出现上面的图的情况!解决办法如下:需要安装下面的补丁(KB3008923) 32位系统:http://www.microsoft.com/en-us/d ...
- django 学习-7 模型数据操作
1.首先还是创建办一个项目和一个应用 django.admin.py startproject ssj cd ssj django.admin.py startapp sdj 那 ...
- React-Native错误笔记-EPERM
运行react-native run-android时出现错误 EPERM:operation not permitted,lstat .............. 解决办法:用Android Stu ...
- 删除mssqlserver表数据,使id从0开始
********************************* 注意备份好数据! *************************** 1.删除表数据 delete 表名 2.执行 dbcc c ...
- linux c/c++ IP字符串转换成可比较大小的数字
由www.169it.com搜集整理 IP字符串转换成可比较大小的数字,具体代码如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include "stdio. ...
- Convert Windows 32bit dirver to Windows 64bit
Pre-condition: 1.source code(vc6.0+WDK based) Development environment: 2.VS2013 3.WDK 8/8.1 Steps: 1 ...