IOS開發之音效/音頻播放

來源:互聯網
上載者:User

標籤:

 音頻播放:     1.音效播放     2.音樂播放 

(1)音頻播放 :

        《1》系統聲音 短小的聲音 使用的架構 AudioToolBox

        《2》1、AudioServicesPlaySystemSound 播放系統聲音

            2、AudioServicesPlayAlertSound播放系統聲音有震動

        《3》使用:

            1.聲明聲音的ID  通過聲音的id來區分是哪一個音效  無符號整形

            2.建立聲音的服務 (告訴系統有一個可以使用的soundID) 需要音效檔路徑和聲音的ID

            3.播放聲音   聲音ID

  代碼如下:
-(void)playSound{        //    根據這個數字來區分是哪一個系統聲音的    SystemSoundID soundID = 1;    //    建立一個系統聲音的服務    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([[NSBundle mainBundle]URLForResource:@"勝利.aiff" withExtension:nil]), &soundID);    //    播放系統聲音    AudioServicesPlaySystemSound(soundID);    AudioServicesPlayAlertSound(soundID);     }

    注意:這裡只是播放短的音效,比如提示音,較長的音樂不能播放,使用的是純C語言寫的,    

 

 

 

(2)音頻播放 :音樂 音頻  指的是那些音樂類似的長檔案

        《1》介紹

            1.   使用架構  AVFoundation架構  AVaudioplayer

            2.注意:

                     (1)必須聲明屬性、全域變數的音樂對象才可以播放

                     (2)在退出播放頁面的時候 一定要把播放對象制空 同時把delegate制空

            3.屬性

 

        《2》使用

            1.資源檔路徑

            2.初始化播放器

            3.設定播放器

            4.預播放

            5.播放

 

-(void)playMusicWithName:(NSString *)name{    NSError *error;//    建立一個音樂播放對象    avplayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:name withExtension:nil]  error:&error];//   如果有錯誤  列印出來    if (error) {        NSLog(@"%@",error);    }    //    預播放    [avplayer prepareToPlay];    NSLog(@"%lu",(unsigned long)avplayer.numberOfChannels);    //    如果想設定播放速率  必須得先開啟允許設定avplayer.enableRate = YES;//    速率rate//    player.rate = 10;    //    峰值  獲得峰值  必須設定enableMeters=YES;//    更新峰值//    [avplayer updateMeters];    NSLog(@"%f",[avplayer peakPowerForChannel:2]);//當前峰值    NSLog(@"%f",[avplayer averagePowerForChannel:2]);//獲得平均峰值//    聲道 (-1.0 左聲道  0.0中間  1.0右聲道)//    player.pan = -1.0;//    迴圈次數 0播放一次  負數迴圈播放 正數設定數字+1avplayer.numberOfLoops = 0;//avplayer.delegate = self;//    設定音樂播放器 當前播放的時間//    player.currentTime = 50;//    duration 總時間長度  唯讀NSLog(@"樂音的總時間長度%f",avplayer.duration);NSLog(@"通道的數量%ld",avplayer.numberOfChannels);    NSLog(@"裝置目前時間%f",avplayer.deviceCurrentTime);//    設定播放次數    avplayer.numberOfLoops = -1;        //     播放//    [avplayer play];}


     這裡也需要提示一下幾個代理方法在音樂播放的時候可以調用:

//播放完成的時候調用

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{}

//解碼錯誤的時候調用

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{}

//被打擾開始中斷的時候調用

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{}

//中斷結束的時候調用

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{}

 

          好了,感覺自己理解的可以試著去做一個音樂播放器了 ,嘗試一下

 

IOS開發之音效/音頻播放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.