源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

实例解析iOS开发中系统音效以及自定义音效的应用

  • 时间:2020-11-12 15:06 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:实例解析iOS开发中系统音效以及自定义音效的应用
[b]一、访问声音服务[/b] 添加框架AudioToolBox以及要播放的声音文件,另外还需要在实现声音服务的类中导入该框架的接口文件: #import <AudioToolbox/AudioToolbox.h> 播放系统声音,需要两个函数是AudioServicesCreateSystemSoundID和AudioServicesPlaySystemSound,还需要声明一个类型为SystemSoundID类型的变量,它表示要使用的声音文件。
[u]复制代码[/u] 代码如下:
-(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);      }
[b]二、提醒音和震动[/b] 1、提醒音 和系统声音的差别: 如果手机处于静音状态,则提醒音将自动触发震动; 播放提醒音需要的函数是AudioServicesPlayAlertSound而不是AudioServicesPlaySystemSound。 2、震动 只需要调用AudioServicesPlaySystemSound()方法,传入kSystemSoundID_Vibrate常量即可。 如果设备不支持震动(如iPad 2),那么也没关系,只是不会震动。 [b]三、AVFoundation framwork[/b] 对于压缩的Audio文件,或者超过30秒的音频文件,可以使用AVAudioPlayer类。 1、AVAudioPlayer也需要知道音频文件的路径; 2、这个类对应的AVAudioPlayerDelegate有两个委托方法: 1)、audioDidFinishPlaying:successfully:当音频播放完成之后触发; 2)、audioPlayerEndInterruption:当程序被应用外部打断后,重新回到应用程序的时候触发。 [b]四、MediaPlayer framwork[/b] 可以使用MPMoviePlayerController播放电影文件(好像只能播放H.264、MPEG-4 Part2 video格式),还可以播放互联网上的视频文件。 [b]五、调用和自定义音效实例实例 [/b]需求大致分为三种: 1.震动 2.系统音效(无需提供音频文件) 3.自定义音效(需提供音频文件) 我的工具类的封装:
[u]复制代码[/u] 代码如下:
//  //  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 
[u]复制代码[/u] 代码如下:
//  //  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震动
[u]复制代码[/u] 代码如下:
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];  [sound play]; 
2.2系统音效,以Tock为例
[u]复制代码[/u] 代码如下:
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];  [sound play]; 
2.3自定义音效,将tap.aif音频文件加入到工程
[u]复制代码[/u] 代码如下:
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"];  [sound play]; 
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部