iPhone 播放音频声音文件

系统 1870 0

播放音频声音文件

 

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 播放音频文件

 

声明类

      //
//  playSoundViewController.h
//  playSound
//
//  Created by  宣雨松 on 11-7-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
 
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface playSoundViewController : UIViewController {
 
 IBOutlet UIButton * playSound;//播放音乐  
 IBOutlet UIButton * playPause;//播放暂停  
 IBOutlet UIButton * playStop;//播放停止  
 //定义一个声音的播放器
 AVAudioPlayer *player;
}
 
-(IBAction)playSoundPressed:(id)pressed;
-(IBAction)playPausePressed:(id)pressed;
-(IBAction)playStopPressed:(id)pressed;
@end
    

 

实现类

      //
//  playSoundViewController.m
//  playSound
//
//  Created by  宣雨松 on 11-7-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
 
#import "playSoundViewController.h"
 
@implementation playSoundViewController
 
- (void)dealloc
{
 [super dealloc];
 //程序的严谨性 在显示对象关闭后把相应的对象清空
 //时刻谨记
 [playSound release];
 [player release];
}
 
- (void)didReceiveMemoryWarning
{
 // Releases the view if it doesn't have a superview.
 [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}
 
#pragma mark - View lifecycle
 
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
 [super viewDidLoad];
 //在这里实现声音的播放代码
 //找到mp3在资源库中的路径 文件名称为sound 类型为mp3
 NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
 //在这里判断以下是否能找到这个音乐文件
 if (path) {
 //从path路径中 加载播放器
 player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:nil];
 //初始化播放器
 [player prepareToPlay];
 
 //设置播放循环次数,如果numberOfLoops为负数 音频文件就会一直循环播放下去
 player.numberOfLoops = -1;
 
 //设置音频音量 volume的取值范围在 0.0为最小 0.1为最大 可以根据自己的情况而设置
 player.volume = 0.5f;
 
 NSLog(@"播放加载");
 }
 
}
 
-(void)playSoundPressed:(id)pressed
{
 //点击按钮后开始播放音乐
 //当player有值的情况下并且没有在播放中 开始播放音乐
 if (player) 
 {
 if (![player isPlaying]) 
 {
 [player play];
 NSLog(@"播放开始");
 }
 }
}
 
-(void)playPausePressed:(id)pressed
{
 //暂停播放声音
 if (player) {
 if ([player isPlaying]) {
 [player pause];
 NSLog(@"播放暂停");
 }
 }
}
 
-(void)playStopPressed:(id)pressed
{
 //停止播放声音
 if (player) {
 if ([player isPlaying]) {
 [player stop];
 NSLog(@"播放停止");
 }
 }
}
 
 
- (void)viewDidUnload
{
 [super viewDidUnload];
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 
@end
    

 

原文链接: http://blog.csdn.net/xys289187120/article/details/6595919

 

iPhone 播放音频声音文件


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论