iPhone 播放音频声音文件


iphone开发中播放声音文件主要使用AVAudioPlayer 类,它的功能非常强大支持播放音频的格式也非常的多,我们可以把它看成一个高级的音乐播放器,它支持的播放格式有
■ AAC
■ AMR(AdaptiveMulti-Rate, aformatforspeech)
■ ALAC(AppleLossless)
■ iLBC(internetLowBitrateCodec, anotherformatforspeech)
■ IMA4(IMA/ADPCM)
■ linearPCM(uncompressed)
■ µ-lawanda-law
■ MP3(MPEG-1audiolayer3

今天主要介绍一下播放mp3 .




       AVAudioPlayer 是 AVFoundation.framework 中定义的一个类,所以使用要先在工程中引入AVFoundation.framework 如图所示点击"+"号将AVFoundation导入。




      

 将音频文件放入资源文件夹中





下面我开始介绍代码中如何调用
AVAudioPlayer 播放音频文件

 
声明类
  1. //   
  2. //  playSoundViewController.h   
  3. //  playSound   
  4. //   
  5. //  Created by  宣雨松 on 11-7-10.   
  6. //  Copyright 2011年 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import <AVFoundation/AVFoundation.h>  
  11. @interface playSoundViewController : UIViewController {  
  12.       
  13.     IBOutlet UIButton * playSound;//播放音乐     
  14.     IBOutlet UIButton * playPause;//播放暂停     
  15.     IBOutlet UIButton * playStop;//播放停止     
  16.     //定义一个声音的播放器   
  17.     AVAudioPlayer *player;  
  18. }  
  19.   
  20. -(IBAction)playSoundPressed:(id)pressed;  
  21. -(IBAction)playPausePressed:(id)pressed;  
  22. -(IBAction)playStopPressed:(id)pressed;  
  23. @end  
实现类
  1. //   
  2. //  playSoundViewController.m   
  3. //  playSound   
  4. //   
  5. //  Created by  宣雨松 on 11-7-10.   
  6. //  Copyright 2011年 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #import "playSoundViewController.h"  
  10.   
  11. @implementation playSoundViewController  
  12.   
  13. - (void)dealloc  
  14. {  
  15.     [super dealloc];  
  16.     //程序的严谨性 在显示对象关闭后把相应的对象清空   
  17.     //时刻谨记   
  18.     [playSound release];  
  19.     [player release];  
  20. }  
  21.   
  22. - (void)didReceiveMemoryWarning  
  23. {  
  24.     // Releases the view if it doesn't have a superview.   
  25.     [super didReceiveMemoryWarning];  
  26.       
  27.     // Release any cached data, images, etc that aren't in use.   
  28. }  
  29.   
  30. #pragma mark - View lifecycle  
  31.   
  32.   
  33. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.   
  34. - (void)viewDidLoad  
  35. {  
  36.     [super viewDidLoad];  
  37.     //在这里实现声音的播放代码   
  38.     //找到mp3在资源库中的路径 文件名称为sound 类型为mp3   
  39.     NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];  
  40.     //在这里判断以下是否能找到这个音乐文件   
  41.     if (path) {  
  42.         //从path路径中 加载播放器   
  43.         player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:nil];  
  44.         //初始化播放器   
  45.         [player prepareToPlay];  
  46.           
  47.         //设置播放循环次数,如果numberOfLoops为负数 音频文件就会一直循环播放下去   
  48.         player.numberOfLoops = -1;  
  49.           
  50.         //设置音频音量 volume的取值范围在 0.0为最小 0.1为最大 可以根据自己的情况而设置   
  51.         player.volume = 0.5f;  
  52.           
  53.         NSLog(@"播放加载");  
  54.     }  
  55.       
  56. }  
  57.   
  58. -(void)playSoundPressed:(id)pressed  
  59. {  
  60.     //点击按钮后开始播放音乐   
  61.     //当player有值的情况下并且没有在播放中 开始播放音乐   
  62.     if (player)   
  63.     {  
  64.         if (![player isPlaying])   
  65.         {  
  66.             [player play];  
  67.             NSLog(@"播放开始");  
  68.         }  
  69.     }  
  70. }  
  71.   
  72. -(void)playPausePressed:(id)pressed  
  73. {  
  74.     //暂停播放声音   
  75.     if (player) {  
  76.         if ([player isPlaying]) {  
  77.             [player pause];  
  78.             NSLog(@"播放暂停");  
  79.         }  
  80.     }  
  81. }  
  82.   
  83. -(void)playStopPressed:(id)pressed  
  84. {  
  85.     //停止播放声音   
  86.     if (player) {  
  87.         if ([player isPlaying]) {  
  88.             [player stop];  
  89.             NSLog(@"播放停止");  
  90.         }  
  91.     }  
  92. }  
  93.   
  94.   
  95. - (void)viewDidUnload  
  96. {  
  97.     [super viewDidUnload];  
  98.     // Release any retained subviews of the main view.   
  99.     // e.g. self.myOutlet = nil;   
  100. }  
  101.   
  102. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  103. {  
  104.     // Return YES for supported orientations   
  105.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  106. }  
  107.   
  108. @end  

相关内容