用户登录
用户注册

分享至

iOS播放简短的音乐

  • 作者: 日洛琋姗
  • 来源: 51数据库
  • 2022-08-17

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

#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>

软件
前端设计
程序设计
Java相关