您的位置:首页 > 其它

iPhone 播放音频声音文件

2011-07-10 13:14 357 查看
[align=center]播放音频声音文件

iphone开发中播放声音文件主要使用AVAudioPlayer 类,它的功能非常强大支持播放音频的格式也非常的多,我们可以把它看成一个高级的音乐播放器,它支持的播放格式有
[/align]
■ 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: