講解iOS開發(fā)中對音效和音樂播放的簡單實現(xiàn)
音效的播放
一、簡單介紹
簡單來說,音頻可以分為2種
(1)音效
又稱“短音頻”,通常在程序中的播放時長為1~2秒
在應(yīng)用程序中起到點綴效果,提升整體用戶體驗
(2)音樂
比如游戲中的“背景音樂”,一般播放時間較長
框架:播放音頻需要用到AVFoundation.framework框架
二、音效的播放
1.獲得音效文件的路徑
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加載音效文件,得到對應(yīng)的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
AudioServicesPlaySystemSound(soundID);
注意:音效文件只需要加載1次
4.音效播放常見函數(shù)總結(jié)
加載音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
釋放音效資源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效帶點震動
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
三、程序示例
先導(dǎo)入需要依賴的框架
導(dǎo)入需要播放的音效文件素材
說明:AVFoundation.framework框架中的東西轉(zhuǎn)換為CF需要使用橋接。
代碼示例:
YYViewController.m文件
//
// YYViewController.m
// 14-音效播放
//
// Created by apple on 14-8-8.
// Copyright (c) 2014年 yangyong. All rights reserved.
//
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@end
@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,一個ID對應(yīng)一個音效文件)
SystemSoundID soundID=0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
//把需要銷毀的音效文件的ID傳遞給它既可銷毀
//AudioServicesDisposeSystemSoundID(soundID);
//3.播放音效文件
//下面的兩個函數(shù)都可以用來播放音效文件,第一個函數(shù)伴隨有震動效果
AudioServicesPlayAlertSound(soundID);
//AudioServicesPlaySystemSound(<#SystemSoundID inSystemSoundID#>)
}
@end
說明:點擊屏幕可以播放音效文件。
音樂的播放
一、簡單說明
音樂播放用到一個叫做AVAudioPlayer的類,這個類可以用于播放手機本地的音樂文件。
注意:
?。?)該類(AVAudioPlayer)只能用于播放本地音頻。
(2)時間比較短的(稱之為音效)使用AudioServicesCreateSystemSoundID來創(chuàng)建,而本地時間較長(稱之為音樂)使用AVAudioPlayer類。
二、代碼示例
AVAudioPlayer類依賴于AVFoundation框架,因此使用該類必須先導(dǎo)入AVFoundation框架,并包含其頭文件(包含主頭文件即可)。
導(dǎo)入必要的,需要播放的音頻文件到項目中。
代碼示例:
//
// YYViewController.m
// 15-播放音樂
//
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@end
@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)建播放器(注意:一個AVAudioPlayer只能播放一個url)
AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[audioPlayer prepareToPlay];
//4.播放
[audioPlayer play];
}
@end
代碼說明:運行程序,點擊模擬器界面,卻并沒有能夠播放音頻文件,原因是代碼中創(chuàng)建的AVAudioPlayer播放器是一個局部變量,應(yīng)該調(diào)整為全局屬性。
可將代碼調(diào)整如下,即可播放音頻:
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *audioplayer;
@end
@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)建播放器(注意:一個AVAudioPlayer只能播放一個url)
self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[self.audioplayer prepareToPlay];
//4.播放
[self.audioplayer play];
}
@end
注意:一個AVAudioPlayer只能播放一個url,如果想要播放多個文件,那么就得創(chuàng)建多個播放器。
三、相關(guān)說明
新建一個項目,在storyboard中放三個按鈕,分別用來控制音樂的播放、暫停和停止。
程序代碼如下:
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@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)建播放器(注意:一個AVAudioPlayer只能播放一個url)
self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[self.player prepareToPlay];
}
- (IBAction)play {
//開始播放/繼續(xù)播放
[self.player play];
}
- (IBAction)pause {
//暫停
[self.player pause];
}
- (IBAction)stop {
//停止
//注意:如果點擊了stop,那么一定要讓播放器重新創(chuàng)建,否則會出現(xiàn)一些莫名其面的問題
[self.player stop];
}
@end
注意:如果點了“停止”,那么一定要播放器重新創(chuàng)建,不然的話會出現(xiàn)莫名其妙的問題。
點擊了stop之后,播放器實際上就不能再繼續(xù)使用了,如果還繼續(xù)使用,那么后續(xù)的一些東西會無法控制。
推薦代碼:
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
@implementation YYViewController
#pragma mark-懶加載
-(AVAudioPlayer *)player
{
if (_player==Nil) {
//1.音頻文件的url路徑
NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
//2.創(chuàng)建播放器(注意:一個AVAudioPlayer只能播放一個url)
self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.緩沖
[self.player prepareToPlay];
}
return _player;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)play {
//開始播放/繼續(xù)播放
[self.player play];
}
- (IBAction)pause {
//暫停
[self.player pause];
}
- (IBAction)stop {
//停止
//注意:如果點擊了stop,那么一定要讓播放器重新創(chuàng)建,否則會出現(xiàn)一些莫名其面的問題
[self.player stop];
self.player=Nil;
}
@end
四、播放多個文件

點擊,url,按住common建查看。
可以發(fā)現(xiàn),這個url是只讀的,因此只能通過initWithContentsOfUrl的方式進(jìn)行設(shè)置,也就意味著一個播放器對象只能播放一個音頻文件。
那么如何實現(xiàn)播放多個音頻文件呢?
可以考慮封裝一個播放音樂的工具類,下一篇文章將會介紹具體怎么實現(xiàn)。
- iOS App中實現(xiàn)播放音效和音樂功能的簡單示例
- 實例解析iOS中音樂播放器應(yīng)用開發(fā)的基本要點
- iOS開發(fā)中音頻工具類的封裝以及音樂播放器的細(xì)節(jié)控制
- iOS實現(xiàn)播放遠(yuǎn)程網(wǎng)絡(luò)音樂的核心技術(shù)點總結(jié)
- ios開發(fā):一個音樂播放器的設(shè)計與實現(xiàn)案例
- iOS利用AVPlayer播放網(wǎng)絡(luò)音樂的方法教程
- iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實例代碼
- iOS視頻添加背景音樂同時保留原音
- 運用iOS教你輕松制作音樂播放器
- iOS實現(xiàn)獲取系統(tǒng)iTunes音樂的方法示例
相關(guān)文章
iOS Crash常規(guī)跟蹤方法及Bugly集成運用詳細(xì)介紹
這篇文章主要介紹了iOS Crash常規(guī)跟蹤方法及Bugly集成運用詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10詳解iOS的Core Animation框架中的CATransform3D圖形變換
CATransform3D一般用于操作view的layer的,是Core Animation的結(jié)構(gòu)體,可以用來做比較復(fù)雜的3D操作,這里我們就帶大家來詳解iOS的Core Animation框架中的CATransform3D圖形變換2016-07-07IOS開發(fā)之判斷兩個數(shù)組中數(shù)據(jù)是否相同實例詳解
這篇文章主要介紹了IOS開發(fā)之判斷兩個數(shù)組中數(shù)據(jù)是否相同實例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02iOS實現(xiàn)動態(tài)元素的引導(dǎo)圖效果
這篇文章給大家介紹了iOS實現(xiàn)動態(tài)元素的引導(dǎo)圖效果的步驟,文章給出了示例代碼介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09iOS UIButton擴大按鈕響應(yīng)區(qū)域的解決方法
這篇文章主要為大家詳細(xì)介紹了iOS UIButton擴大按鈕響應(yīng)區(qū)域的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08