iOS開發 - 播放系統音效、自定義音效

openkk 12年前發布 | 33K 次閱讀 IOS iOS開發 移動開發

播放系統音效、自定義音效工具類


需求大致分為三種:
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];

轉自:http://blog.csdn.net/yiyaaixuexi/article/details/7870328

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!