新聞中心
iOS開(kāi)發(fā)中對(duì)音效和音樂(lè)播放的簡(jiǎn)單實(shí)現(xiàn)
一、簡(jiǎn)單介紹
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),嘉陵企業(yè)網(wǎng)站建設(shè),嘉陵品牌網(wǎng)站建設(shè),網(wǎng)站定制,嘉陵網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,嘉陵網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
簡(jiǎn)單來(lái)說(shuō),音頻可以分為2種
(1)音效
又稱“短音頻”,通常在程序中的播放時(shí)長(zhǎng)為1~2秒
在應(yīng)用程序中起到點(diǎn)綴效果,提升整體用戶體驗(yàn)
(2)音樂(lè)
比如游戲中的“背景音樂(lè)”,一般播放時(shí)間較長(zhǎng)
框架:播放音頻需要用到AVFoundation.framework框架
二、音效的播放
1.獲得音效文件的路徑
復(fù)制代碼 代碼如下:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加載音效文件,得到對(duì)應(yīng)的音效ID
復(fù)制代碼 代碼如下:
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), soundID);
3.播放音效
復(fù)制代碼 代碼如下:
AudioServicesPlaySystemSound(soundID);
注意:音效文件只需要加載1次
4.音效播放常見(jiàn)函數(shù)總結(jié)
加載音效文件
復(fù)制代碼 代碼如下:
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
釋放音效資源
復(fù)制代碼 代碼如下:
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效
復(fù)制代碼 代碼如下:
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效帶點(diǎn)震動(dòng)
復(fù)制代碼 代碼如下:
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
三、程序示例
先導(dǎo)入需要依賴的框架
導(dǎo)入需要播放的音效文件素材
說(shuō)明:AVFoundation.framework框架中的東西轉(zhuǎn)換為CF需要使用橋接。
代碼示例:
復(fù)制代碼 代碼如下:
YYViewController.m文件
//
// YYViewController.m
// 14-音效播放
//
// Created by apple on 14-8-8.
// Copyright (c) 2014年 yangyong. All rights reserved.
//
#import "YYViewController.h"
#import
@interface YYViewController ()
@end
復(fù)制代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.獲得音效文件的全路徑
NSURL *url=[[NSBundle mainBundle]URLForResource:@"buyao.wav" withExtension:nil];
//2.加載音效文件,創(chuàng)建音效ID(SoundID,一個(gè)ID對(duì)應(yīng)一個(gè)音效文件)
SystemSoundID soundID=0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, soundID);
//把需要銷毀的音效文件的ID傳遞給它既可銷毀
//AudioServicesDisposeSystemSoundID(soundID);
//3.播放音效文件
//下面的兩個(gè)函數(shù)都可以用來(lái)播放音效文件,第一個(gè)函數(shù)伴隨有震動(dòng)效果
AudioServicesPlayAlertSound(soundID);
//AudioServicesPlaySystemSound(#systemsoundid)
}
@end
說(shuō)明:點(diǎn)擊屏幕可以播放音效文件。
音樂(lè)的播放
一、簡(jiǎn)單說(shuō)明
音樂(lè)播放用到一個(gè)叫做AVAudioPlayer的`類,這個(gè)類可以用于播放手機(jī)本地的音樂(lè)文件。
注意:
(1)該類(AVAudioPlayer)只能用于播放本地音頻。
(2)時(shí)間比較短的(稱之為音效)使用AudioServicesCreateSystemSoundID來(lái)創(chuàng)建,而本地時(shí)間較長(zhǎng)(稱之為音樂(lè))使用AVAudioPlayer類。
二、代碼示例
AVAudioPlayer類依賴于AVFoundation框架,因此使用該類必須先導(dǎo)入AVFoundation框架,并包含其頭文件(包含主頭文件即可)。
導(dǎo)入必要的,需要播放的音頻文件到項(xiàng)目中。
代碼示例:
復(fù)制代碼 代碼如下:
//
// YYViewController.m
// 15-播放音樂(lè)
//
#import "YYViewController.h"
#import
@interface YYViewController ()
@end
復(fù)制代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.音頻文件的url路徑
NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
//2.創(chuàng)建播放器(注意:一個(gè)AVAudioPlayer只能播放一個(gè)url)
AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[audioPlayer prepareToPlay];
//4.播放
[audioPlayer play];
}
@end
代碼說(shuō)明:運(yùn)行程序,點(diǎn)擊模擬器界面,卻并沒(méi)有能夠播放音頻文件,原因是代碼中創(chuàng)建的AVAudioPlayer播放器是一個(gè)局部變量,應(yīng)該調(diào)整為全局屬性。
可將代碼調(diào)整如下,即可播放音頻:
復(fù)制代碼 代碼如下:
#import "YYViewController.h"
#import
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *audioplayer;
@end
復(fù)制代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.音頻文件的url路徑
NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
//2.創(chuàng)建播放器(注意:一個(gè)AVAudioPlayer只能播放一個(gè)url)
self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[self.audioplayer prepareToPlay];
//4.播放
[self.audioplayer play];
}
@end
注意:一個(gè)AVAudioPlayer只能播放一個(gè)url,如果想要播放多個(gè)文件,那么就得創(chuàng)建多個(gè)播放器。
三、相關(guān)說(shuō)明
新建一個(gè)項(xiàng)目,在storyboard中放三個(gè)按鈕,分別用來(lái)控制音樂(lè)的播放、暫停和停止。
程序代碼如下:
復(fù)制代碼 代碼如下:
#import "YYViewController.h"
#import
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1.音頻文件的url路徑
NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
//2.創(chuàng)建播放器(注意:一個(gè)AVAudioPlayer只能播放一個(gè)url)
self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[self.player prepareToPlay];
}
- (IBAction)play {
//開(kāi)始播放/繼續(xù)播放
[self.player play];
}
- (IBAction)pause {
//暫停
[self.player pause];
}
- (IBAction)stop {
//停止
//注意:如果點(diǎn)擊了stop,那么一定要讓播放器重新創(chuàng)建,否則會(huì)出現(xiàn)一些莫名其面的問(wèn)題
[self.player stop];
}
@end
注意:如果點(diǎn)了“停止”,那么一定要播放器重新創(chuàng)建,不然的話會(huì)出現(xiàn)莫名其妙的問(wèn)題。
點(diǎn)擊了stop之后,播放器實(shí)際上就不能再繼續(xù)使用了,如果還繼續(xù)使用,那么后續(xù)的一些東西會(huì)無(wú)法控制。
推薦代碼:
復(fù)制代碼 代碼如下:
#import "YYViewController.h"
#import
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
復(fù)制代碼 代碼如下:
@implementation YYViewController
#pragma mark-懶加載
-(AVAudioPlayer *)player
{
if (_player==Nil) {
//1.音頻文件的url路徑
NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
//2.創(chuàng)建播放器(注意:一個(gè)AVAudioPlayer只能播放一個(gè)url)
self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[self.player prepareToPlay];
}
return _player;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)play {
//開(kāi)始播放/繼續(xù)播放
[self.player play];
}
- (IBAction)pause {
//暫停
[self.player pause];
}
- (IBAction)stop {
//停止
//注意:如果點(diǎn)擊了stop,那么一定要讓播放器重新創(chuàng)建,否則會(huì)出現(xiàn)一些莫名其面的問(wèn)題
[self.player stop];
self.player=Nil;
}
@end
四、播放多個(gè)文件
點(diǎn)擊,url,按住common建查看。
可以發(fā)現(xiàn),這個(gè)url是只讀的,因此只能通過(guò)initWithContentsOfUrl的方式進(jìn)行設(shè)置,也就意味著一個(gè)播放器對(duì)象只能播放一個(gè)音頻文件。
那么如何實(shí)現(xiàn)播放多個(gè)音頻文件呢?
可以考慮封裝一個(gè)播放音樂(lè)的工具類,下一篇文章將會(huì)介紹具體怎么實(shí)現(xiàn)。
xcode開(kāi)發(fā)音樂(lè)放在那
xcode開(kāi)發(fā)音樂(lè)放在resource文件夾下面。ios開(kāi)發(fā)中,所有的美術(shù)、音樂(lè)、存儲(chǔ)數(shù)據(jù)文件都放在resource文件夾下面。
iOS 開(kāi)發(fā)的app與音樂(lè)播放語(yǔ)音沖突問(wèn)題
我們常常會(huì)在使用app的時(shí)候,邊聽(tīng)音樂(lè)(網(wǎng)易云音樂(lè),qq音樂(lè)等)邊使用軟件,如果我們?cè)赼pp中使用了聲音,例如“?!钡囊宦?提醒,就會(huì)導(dǎo)致音樂(lè)的停止播放。而像微信中的語(yǔ)音播放,會(huì)在播放完成后音樂(lè)恢復(fù)播放,這樣的體驗(yàn)就很好,那么需要怎么做呢?其實(shí)很簡(jiǎn)單,只需要一句話就可以。
當(dāng)你的app中的聲音播放完畢后,加上這一句話,被打斷的音樂(lè)便會(huì)恢復(fù)播放了。
當(dāng)然還可以設(shè)置讓app的聲音和其他音樂(lè)兼容(默認(rèn)是不兼容的)
withOptions后面的屬性是一個(gè)枚舉,不同的類型會(huì)有不同的效果,自己試試吧!
當(dāng)前名稱:ios開(kāi)發(fā)音樂(lè),IOS音樂(lè)
鏈接URL:http://www.dlmjj.cn/article/dsijspj.html