iOS播放简短的音乐

2015-11-05 0 517
iOS播放简短的音乐

这段代码能十分简单地播放简短的音效。在需要播放简单的声音文件,并且不需要重复播放时,可以使用这种方法。

#import <audiotoolbox audioservices.h="">
-(void) playSound : (NSString *) fName : (NSString *) ext
{
    NSString *path  = [[NSBundle *mainBundle] pathForResource : fName ofType :ext];
    if ([[NSFileManager defaultManager] fileExistsAtPath : path])
    {
        NSURL *pathURL = [NSURL fileURLWithPath : path];
        SystemSoundID audioEffect;
        AudioServicesCreateSystemSoundID((CFURLRef) pathURL, &audioEffect;);
        AudioServicesPlaySystemSound(audioEffect);
    }
    else
    {
        NSLog(@"error, file not found: %@", path);
    }
    //在非ARC模式下,还需要释放 audioEffect
    AudioServicesDisposeSystemSoundID(audioEffect);
}
</audiotoolbox>

遇见资源网 objective-c iOS播放简短的音乐 http://www.ox520.com/18116.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务