国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

實例解析iOS開發中系統音效以及自定義音效的應用

2019-10-21 18:57:06
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS開發中系統音效以及自定義音效的應用,代碼基于傳統的Objective-C,需要的朋友可以參考下
 

一、訪問聲音服務

添加框架AudioToolBox以及要播放的聲音文件,另外還需要在實現聲音服務的類中導入該框架的接口文件:
#import <AudioToolbox/AudioToolbox.h>

播放系統聲音,需要兩個函數是AudioServicesCreateSystemSoundID和AudioServicesPlaySystemSound,還需要聲明一個類型為SystemSoundID類型的變量,它表示要使用的聲音文件。

 

復制代碼代碼如下:

 

-(IBAction) playSysSound:(id)sender { 
          
        SystemSoundID sourceID; 
        //調用NSBundle類的方法mainBundle返回一個NSBundle對象,該對象對應于當前程序可執行二進制文件所屬的目錄 
        NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"]; 
        //一個指向文件位置的CFURLRef對象和一個指向要設置的SystemSoundID變量的指針 
        AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID); 
        AudioServicesPlaySystemSound(soundID); 
    }

 


二、提醒音和震動

 

1、提醒音

和系統聲音的差別:

如果手機處于靜音狀態,則提醒音將自動觸發震動;

播放提醒音需要的函數是AudioServicesPlayAlertSound而不是AudioServicesPlaySystemSound。

2、震動

只需要調用AudioServicesPlaySystemSound()方法,傳入kSystemSoundID_Vibrate常量即可。

如果設備不支持震動(如iPad 2),那么也沒關系,只是不會震動。

三、AVFoundation framwork

對于壓縮的Audio文件,或者超過30秒的音頻文件,可以使用AVAudioPlayer類。

1、AVAudioPlayer也需要知道音頻文件的路徑;

2、這個類對應的AVAudioPlayerDelegate有兩個委托方法:

1)、audioDidFinishPlaying:successfully:當音頻播放完成之后觸發;

2)、audioPlayerEndInterruption:當程序被應用外部打斷后,重新回到應用程序的時候觸發。

四、MediaPlayer framwork

可以使用MPMoviePlayerController播放電影文件(好像只能播放H.264、MPEG-4 Part2 video格式),還可以播放互聯網上的視頻文件。

五、調用和自定義音效實例實例
需求大致分為三種:
1.震動
2.系統音效(無需提供音頻文件)
3.自定義音效(需提供音頻文件)


我的工具類的封裝:

復制代碼代碼如下:

//  
//  WQPlaySound.h  
//  WQSound  
//  
//  Created by 念茜 on 12-7-20.  
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
//  
  
#import <UIKit/UIKit.h>  
#import <AudioToolbox/AudioToolbox.h>  
  
@interface WQPlaySound : NSObject  
{  
    SystemSoundID soundID;  
}  
  
/** 
 *  @brief  為播放震動效果初始化 
 * 
 *  @return self 
 */  
-(id)initForPlayingVibrate;  
  
/** 
 *  @brief  為播放系統音效初始化(無需提供音頻文件) 
 * 
 *  @param resourceName 系統音效名稱 
 *  @param type 系統音效類型 
 * 
 *  @return self 
 */  
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;  
  
/** 
 *  @brief  為播放特定的音頻文件初始化(需提供音頻文件) 
 * 
 *  @param filename 音頻文件名(加在工程中) 
 * 
 *  @return self 
 */  
-(id)initForPlayingSoundEffectWith:(NSString *)filename;  
  
/** 
 *  @brief  播放音效 
 */  
-(void)play;  
  
@end  

復制代碼代碼如下:

//  
//  WQPlaySound.m  
//  WQSound  
//  
//  Created by 念茜 on 12-7-20.  
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
//  
  
#import "WQPlaySound.h"  
  
@implementation WQPlaySound  
  
-(id)initForPlayingVibrate  
{  
    self = [super init];  
    if (self) {  
        soundID = kSystemSoundID_Vibrate;  
    }  
    return self;      
}  
  
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type  
{  
    self = [super init];  
    if (self) {  
        NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type];  
        if (path) {  
            SystemSoundID theSoundID;  
            OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);  
            if (error == kAudioServicesNoError) {  
                soundID = theSoundID;  
            }else {  
                NSLog(@"Failed to create sound ");  
            }  
        }  
          
    }  
    return self;  
}  
  
-(id)initForPlayingSoundEffectWith:(NSString *)filename  
{  
    self = [super init];  
    if (self) {  
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];  
        if (fileURL != nil)  
        {  
            SystemSoundID theSoundID;  
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);  
            if (error == kAudioServicesNoError){  
                soundID = theSoundID;  
            }else {  
                NSLog(@"Failed to create sound ");  
            }  
        }  
    }  
    return self;  
}  
  
-(void)play  
{  
    AudioServicesPlaySystemSound(soundID);  
}  
  
-(void)dealloc  
{   
    AudioServicesDisposeSystemSoundID(soundID);  
}  
@end  

調用方法步驟:
1.加入AudioToolbox.framework到工程中
2.調用WQPlaySound工具類
2.1震動
復制代碼代碼如下:

WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];  
[sound play];  

2.2系統音效,以Tock為例
復制代碼代碼如下:

WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];  
[sound play];  

2.3自定義音效,將tap.aif音頻文件加入到工程
復制代碼代碼如下:

WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"];  
[sound play];  
 


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松潘县| 宾阳县| 仁寿县| 土默特右旗| 渭源县| 葵青区| 舒兰市| 清远市| 建湖县| 曲水县| 遂宁市| 墨竹工卡县| 石泉县| 萨嘎县| 勐海县| 西昌市| 张掖市| 临泽县| 许昌县| 襄城县| 历史| 绵阳市| 肥东县| 楚雄市| 乌鲁木齐县| 文昌市| 淳化县| 深州市| 茌平县| 瓦房店市| 汝州市| 铜川市| 祁连县| 玉林市| 方城县| 化隆| 黄浦区| 阳江市| 久治县| 报价| 德安县|