1. AVFoundation

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

firstviewcontroller.h

  1. #import <UIKit/UIKit.h>
  2. #import <AVFoundation/AVFoundation.h>
  3. @interface FirstViewController : UIViewController
  4. {
  5. __weak IBOutlet UILabel *label;
  6. AVAudioPlayer *player;
  7. }
  8. - (IBAction)toplay:(id)sender;
  9. @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

  1. - (IBAction)toplay:(id)sender
  2. {
  3. NSURL  *url = [NSURL fileURLWithPath:[NSString  stringWithFormat:@"%@/test.mp3",  [[NSBundle mainBundle]  resourcePath]]];
  4. NSError  *error;
  5. player  = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
  6. player.numberOfLoops = -1;
  7. [player play];
  8. [label setText:@"Play ..."];
  9. }
- (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 ..."];
}

或者:

  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
  2. NSError  *error;
  3. player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:&error];
  4. [player prepareToPlay];
  5. player.numberOfLoops = -1;
  6. [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 文件夹。

播放,暂停和停止

  1. [player play];  //播放
  2. [player pause]; //暂停
  3. [player stop];  //停止
[player play];  //播放
[player pause]; //暂停
[player stop]; //停止

更多功能:

1. 音量:

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

2. 循环次数

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

3.播放位置

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

3.1 显示当前时间

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

4.声道数

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

5.持续时间

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

6.仪表计数

  1. player.meteringEnabled = YES;//开启仪表计数功能
  2. [ player updateMeters];//更新仪表读数
  3. //读取每个声道的平均电平和峰值电平,代表每个声道的分贝数,范围在-100~0之间。
  4. for(int i = 0; i<player.numberOfChannels;i++){
  5. float power = [player averagePowerForChannel:i];
  6. float peak = [player peakPowerForChannel:i];
  7. }
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. 初始化播放器

  1. [player prepareToPlay];
[player prepareToPlay];

8. 判断是否正在播放

  1. [player isPlaying]
[player isPlaying]

9、代理方法

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

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

  1. player.delegate = self;
player.delegate = self;  
  1. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{
  2. //播放结束时执行的动作
  3. }
  4. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{
  5. //解码错误执行的动作
  6. }
  7. - (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{
  8. //处理中断的代码
  9. }
  10. - (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{
  11. //处理中断结束的代码
  12. }
- (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

  1. #import <UIKit/UIKit.h>
  2. #import <AudioToolbox/AudioToolbox.h>
  3. @interface FirstViewController : UIViewController
  4. {
  5. }
  6. - (IBAction)toplay:(id)sender;
  7. @end
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h> @interface FirstViewController : UIViewController
{
} - (IBAction)toplay:(id)sender; @end

firstviewcontroller.m

  1. - (IBAction)toplay:(id)sender
  2. {
  3. CFBundleRef mainBundle = CFBundleGetMainBundle();
  4. CFURLRef soundFileURLRef;
  5. soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR ("wav"), NULL);
  6. UInt32 soundID;
  7. AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
  8. AudioServicesPlaySystemSound(soundID);
  9. }
- (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

IOS播放音频 AVAudioPlayer(实例)的更多相关文章

  1. iOS 播放音频的几种方法

    Phone OS 主要提供以下了几种播放音频的方法: System Sound Services AVAudioPlayer 类 Audio Queue Services OpenAL 1. Syst ...

  2. iOS播放器 - AVAudioPlayer

    今天记录一下AVAudioPlayer,这个播放器类苹果提供了一些代理方法,主要用来播放本地音频. 其实也可以用来播放网络音频,只不过是将整个网络文件下载下来而已,在实际开发中会比较耗费流量不做推荐. ...

  3. IOS 播放音频

    1,播放短音频 #import <AudioToolbox/AudioToolbox.h>#import "GLYViewController.h"static voi ...

  4. iOS 播放音频文件

    //        播放音乐 NSString *path = [[NSBundle mainBundle] pathForResource:@"1670" ofType:@&qu ...

  5. IOS 播放音频流媒体

    #pragma mark - 加载播放数据 - (void)loadData:(NSString *)musicUrl { NSURL *playURL = [NSURL URLWithString: ...

  6. Android应用开发学习笔记之播放音频

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...

  7. iOS 9音频应用播放音频之第一个ios9音频实例2

    iOS 9音频应用播放音频之第一个ios9音频实例2 ios9音频应用关联 iOS9音频应用中对于在主视图上添加的视图或控件,在使用它们时必须要与插座变量进行关联.ios9插座变量其实就是为主视图中的 ...

  8. iOS 9音频应用播放音频之第一个ios9音频实例

    iOS 9音频应用播放音频之第一个ios9音频实例 第一个ios9音频实例 为了让开发者可以对上面的内容有更加深入的了解,本节将实现播放音频的第一个实例.在此实例中会涉及到项目的创建.界面设计.关联以 ...

  9. iOS AVAudioPlayer播放音频时声音太小

    iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...

随机推荐

  1. PL/SQL developer 使用技巧汇总

    为了快速的使用PL/SQL developer 进行 oracle数据库相关开发,将一些使用频率较高的使用技巧进行汇总如下,以下转自网络和自己的测试 1.切换schema --switch schem ...

  2. [转]DataTable用中使用Compute 实现简单的DataTable数据的统计

    本文转自:http://blog.csdn.net/zwxrain/article/details/252285 調用格式: object DataTable.Compute(string expre ...

  3. [需再总结]SSH整合代码生成器

    package cn.itcast.invoice.util.generator; import java.io.BufferedWriter; import java.io.File; import ...

  4. 开源IDE CodeBlocks的下载安装、配置、简单编程

    如果没有集成开发环境(IDE),在linux下开发程序将非常繁琐,IDE是指将编辑.编译.调试等功能集成在一个桌面环境上,这样就大大方便了用户.IDE一般包括代码编辑器.编译器.调试器和图形界面用户工 ...

  5. mysql数据库千万级别数据的查询优化和分页测试

    原文地址:原创 mysql数据库千万级别数据的查询优化和分页测试作者:于堡舰 本文为本人最近利用几个小时才分析总结出的原创文章,希望大家转载,但是要注明出处 http://blog.sina.com. ...

  6. Servlet & JSP - UrlRewriteFilter

    重写 URL 的好处有很多: 静态化页面,有利于搜索引擎收录. 隐藏真实的 URL,提高安全性. 当网站的结构发生变化时,无需要求用户修改书签. UrlRewriteFilter 的简单应用 1. M ...

  7. React Native之ViewPagerAndroid跳转页面问题

    前言: 网上目前react-native的教程较少,加上许多帖子还是用的ES5(2015年6月已发布ES6标准),有些细节很难找到答案,这里把遇到的问题做一个分享,让学习者尽量少踩坑. 出现问题: 1 ...

  8. MyEclipse中配置SWT/JFace/SWT-Designer 艰辛路程

    我最近受一个老师所托,写一个小系统,为了更加熟练使用Java,我决定用Java写一个PC软件. 我是一个比较追求完美的孩子,所以虽然老师对界面没啥要求,但是为了加快速度和界面美观,果断选择SWT/JF ...

  9. jQuery选项卡插件、Tabs插件

    效果图: <!DOCTYPE html> <html> <head> <title></title> <script src=&quo ...

  10. PHP学习笔记 - 进阶篇(10)

    PHP学习笔记 - 进阶篇(10) 异常处理 抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw抛出,异常抛出之后,后面的代码将不会再被 ...