iOS中关于系统音效和自定义音效的应用详解

来源:爱站网时间:2021-04-30编辑:网友分享
在iOS开发中,音效有分系统音效和自定义音效,这两种音效应用分别该怎么去调用呢?以下内容就是为大家详细解答有关iOS中关于系统音效和自定义音效的应用,有需要的朋友可以继续往下阅读。

一、访问声音服务

添加框架AudioToolBox以及要播放的声音文件,另外还需要在实现声音服务的类中导入该框架的接口文件:
#import

播放系统声音,需要两个函数是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  
#import  
 
@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开发中系统音效以及自定义音效的应用,如果需要运用到这项功能的朋友可以直接参考以上调用步骤。

上一篇:IOS开发之UIButton设置button上的文字图片上下垂直居中对齐的方法

下一篇:iOS Label怎么设置文字渐变色

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载