栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

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

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实例解析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; 

 
 
-(id)initForPlayingVibrate; 
 
 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type; 
 
 
-(id)initForPlayingSoundEffectWith:(NSString *)filename; 
 
 
-(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]; 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/167513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号