我想我可以使用AVAudioPlayer
来播放声音,然而,我需要的只是播放一个简短的声音,我不需要任何循环或对音量等的细粒度控制。
有什么简单的方法可以做到这一点吗?
发布于 2012-04-26 16:09:12
对于短小的声音剪辑(小于30秒),有一个非常好的SystemSounds库。
优点:您不需要单独管理音量设置。声音在单独的线程中播放,音频剪辑的加载和播放速度很快。简而言之,您将此剪辑视为另一个系统声音。
缺点:您不能提供单独的音频控制设置。它与系统声音的设置有关。你不能玩超过30秒。您可能无法应用任何声音过滤器来增强音频效果。
当然有更多的优点和缺点,但这些是我能想到的,在我的头上。
使用此导入:<AudioToolbox/AudioToolbox.h>
添加AudioToolbox框架,然后调用下面的方法,如self playSound,无论您想在何处播放剪辑。
-(void) playSound {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"changeTrack" ofType:@"aif"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
[soundPath release];
}
发布于 2012-04-26 20:19:19
最近,我用这段代码播放了短的mp3音频,效果很好:
在@实现下面声明
NSString *path;
NSURL *url;
//where you are about to add sound
path =[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"quotes_%d",soundTags] ofType:@"mp3"];
url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player setVolume:1.0];
[player play];
//just add AVFoundation framework
发布于 2012-04-26 16:06:45
我使用此代码在iOS上播放了一个简短的aiff声音
#import <AudioToolbox/AudioServices.h>
SystemSoundID completeSound;
NSURL *audioPath = [[NSBundle mainBundle] URLForResource:@"downloadCompleted" withExtension:@"aiff"];
AudioServicesCreateSystemSoundID((CFURLRef)audioPath, &completeSound);
AudioServicesPlaySystemSound (completeSound);
希望这能有所帮助。
https://stackoverflow.com/questions/10329291
复制相似问题