播放音頻聲音文件
?
iphone開(kāi)發(fā)中播放聲音文件主要使用AVAudioPlayer 類(lèi),它的功能非常強(qiáng)大支持播放音頻的格式也非常的多,我們可以把它看成一個(gè)高級(jí)的音樂(lè)播放器,它支持的播放格式有
■ AAC
■ AMR(AdaptiveMulti-Rate, aformatforspeech)
■ ALAC(AppleLossless)
■ iLBC(internetLowBitrateCodec, anotherformatforspeech)
■ IMA4(IMA/ADPCM)
■ linearPCM(uncompressed)
■ μ-lawanda-law
■ MP3(MPEG-1audiolayer3
今天主要介紹一下播放mp3 .
?
AVAudioPlayer 是 AVFoundation.framework 中定義的一個(gè)類(lèi),所以使用要先在工程中引入AVFoundation.framework 如圖所示點(diǎn)擊”+”號(hào)將AVFoundation導(dǎo)入。
?
將音頻文件放入資源文件夾中
下面我開(kāi)始介紹代碼中如何調(diào)用AVAudioPlayer 播放音頻文件
?
聲明類(lèi)
//
// 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;//播放音樂(lè)
IBOutlet UIButton * playPause;//播放暫停
IBOutlet UIButton * playStop;//播放停止
//定義一個(gè)聲音的播放器
AVAudioPlayer *player;
}
-(IBAction)playSoundPressed:(id)pressed;
-(IBAction)playPausePressed:(id)pressed;
-(IBAction)playStopPressed:(id)pressed;
@end
?
實(shí)現(xiàn)類(lèi)
//
// playSoundViewController.m
// playSound
//
// Created by 宣雨松 on 11-7-10.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "playSoundViewController.h"
@implementation playSoundViewController
- (void)dealloc
{
[super dealloc];
//程序的嚴(yán)謹(jǐn)性 在顯示對(duì)象關(guān)閉后把相應(yīng)的對(duì)象清空
//時(shí)刻謹(jǐn)記
[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];
//在這里實(shí)現(xiàn)聲音的播放代碼
//找到mp3在資源庫(kù)中的路徑 文件名稱(chēng)為sound 類(lèi)型為mp3
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
//在這里判斷以下是否能找到這個(gè)音樂(lè)文件
if (path) {
//從path路徑中 加載播放器
player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:nil];
//初始化播放器
[player prepareToPlay];
//設(shè)置播放循環(huán)次數(shù),如果numberOfLoops為負(fù)數(shù) 音頻文件就會(huì)一直循環(huán)播放下去
player.numberOfLoops = -1;
//設(shè)置音頻音量 volume的取值范圍在 0.0為最小 0.1為最大 可以根據(jù)自己的情況而設(shè)置
player.volume = 0.5f;
NSLog(@"播放加載");
}
}
-(void)playSoundPressed:(id)pressed
{
//點(diǎn)擊按鈕后開(kāi)始播放音樂(lè)
//當(dāng)player有值的情況下并且沒(méi)有在播放中 開(kāi)始播放音樂(lè)
if (player)
{
if (![player isPlaying])
{
[player play];
NSLog(@"播放開(kāi)始");
}
}
}
-(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
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

