講解iOS開發中對音效和音樂播放的簡單實現_IOS

來源:互聯網
上載者:User

音效的播放

一、簡單介紹

簡單來說,音頻可以分為2種

(1)音效

又稱“短音頻”,通常在程式中的播放時間長度為1~2秒

在應用程式中起到點綴效果,提升整體使用者體驗

(2)音樂

  比如遊戲中的“背景音樂”,一般播放時間較長

架構:播放音頻需要用到AVFoundation.framework架構

二、音效的播放

1.獲得音效檔案的路徑

複製代碼 代碼如下:

  NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

2.載入音效檔案,得到對應的音效ID

複製代碼 代碼如下:

  SystemSoundID soundID = 0;

  AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);

3.播放音效

複製代碼 代碼如下:

  AudioServicesPlaySystemSound(soundID);

 

注意:音效檔案只需要載入1次

4.音效播放常見函數總結

載入音效檔案

複製代碼 代碼如下:

  AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

釋放音效資源
複製代碼 代碼如下:

  AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效
複製代碼 代碼如下:

  AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效帶點震動
複製代碼 代碼如下:

  AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

 

三、程式樣本

先匯入需要依賴的架構

匯入需要播放的音效檔案素材

說明:AVFoundation.framework架構中的東西轉換為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.載入音效檔案,建立音效ID(SoundID,一個ID對應一個音效檔案)
    SystemSoundID soundID=0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
   
    //把需要銷毀的音效檔案的ID傳遞給它既可銷毀
    //AudioServicesDisposeSystemSoundID(soundID);
   
    //3.播放音效檔案
    //下面的兩個函數都可以用來播放音效檔案,第一個函數伴隨有震動效果
    AudioServicesPlayAlertSound(soundID);
    //AudioServicesPlaySystemSound(<#SystemSoundID inSystemSoundID#>)
}

@end


說明:點擊螢幕可以播放音效檔案。

音樂的播放

一、簡單說明

  音樂播放用到一個叫做AVAudioPlayer的類,這個類可以用於播放手機本地的音樂檔案。

注意:

  (1)該類(AVAudioPlayer)只能用於播放本地音頻。

  (2)時間比較短的(稱之為音效)使用AudioServicesCreateSystemSoundID來建立,而本地時間較長(稱之為音樂)使用AVAudioPlayer類。

二、程式碼範例

  AVAudioPlayer類依賴於AVFoundation架構,因此使用該類必須先匯入AVFoundation架構,並包含其標頭檔(包含主標頭檔即可)。

匯入必要的,需要播放的音頻檔案到項目中。

程式碼範例:

複製代碼 代碼如下:

//
//  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.建立播放器(注意:一個AVAudioPlayer只能播放一個url)
    AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
   
    //3.緩衝
    [audioPlayer prepareToPlay];
   
    //4.播放
    [audioPlayer play];
}

@end


代碼說明:運行程式,點擊模擬器介面,卻並沒有能夠播放音頻檔案,原因是代碼中建立的AVAudioPlayer播放器是一個局部變數,應該調整為全域屬性。

可將代碼調整如下,即可播放音頻:

複製代碼 代碼如下:

#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.建立播放器(注意:一個AVAudioPlayer只能播放一個url)
    self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
   
    //3.緩衝
    [self.audioplayer prepareToPlay];
   
    //4.播放
    [self.audioplayer play];
}

@end


注意:一個AVAudioPlayer只能播放一個url,如果想要播放多個檔案,那麼就得建立多個播放器。

三、相關說明

建立一個項目,在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.建立播放器(注意:一個AVAudioPlayer只能播放一個url)
    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
   
    //3.緩衝
    [self.player prepareToPlay];

}

- (IBAction)play {
    //開始播放/繼續播放
    [self.player play];
}

- (IBAction)pause {
    //暫停
    [self.player pause];
}

- (IBAction)stop {
    //停止
    //注意:如果點擊了stop,那麼一定要讓播放器重新建立,否則會出現一些莫名其面的問題
    [self.player stop];
}
@end


注意:如果點了“停止”,那麼一定要播放器重新建立,不然的話會出現莫名其妙的問題。

  點擊了stop之後,播放器實際上就不能再繼續使用了,如果還繼續使用,那麼後續的一些東西會無法控制。

推薦代碼:

複製代碼 代碼如下:

#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.建立播放器(注意:一個AVAudioPlayer只能播放一個url)
        self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
       
        //3.緩衝
        [self.player prepareToPlay];
    }
    return _player;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)play {
    //開始播放/繼續播放
    [self.player play];
}

- (IBAction)pause {
    //暫停
    [self.player pause];
}

- (IBAction)stop {
    //停止
    //注意:如果點擊了stop,那麼一定要讓播放器重新建立,否則會出現一些莫名其面的問題
    [self.player stop];
    self.player=Nil;
}
@end


四、播放多個檔案

點擊,url,按住common建查看。

可以發現,這個url是唯讀,因此只能通過initWithContentsOfUrl的方式進行設定,也就意味著一個播放器對象只能播放一個音頻檔案。

那麼如何?播放多個音頻檔案呢?

可以考慮封裝一個播放音樂的工具類,下一篇文章將會介紹具體怎麼實現。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.