【问题标题】:NSSound on the iphoneiPhone上的NSSound
【发布时间】:2008-12-23 04:44:39
【问题描述】:

我一直在寻找如何在 iphone 上播放声音,我认为是这样的:

[[NSSound soundNamed:@"cat.mp3"] play];

但是 NSSound 在 AppKit 上……有什么建议吗?我知道对此有一个非常简单的答案,但今天我的搜索没有呈现任何结果......

【问题讨论】:

    标签: iphone cocoa-touch core-audio


    【解决方案1】:

    新的 AVFoundation 类是在 iPhone 上播放声音的最简单方法,尽管它仅适用于固件 2.2:

    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
    [audioPlayer play]; 
    

    你只需要在使用它的类中导入这个:

    #import <AVFoundation/AVAudioPlayer.h>
    

    【讨论】:

    • 太好了,我今晚试试这个
    【解决方案2】:

    Audio Toolbox 系统声音非常适合短时间的异步播放。

    // Initialize
    CFBundleRef mainBundle;
    mainBundle = CFBundleGetMainBundle ();
    
    // Init each sound
    CFURLRef      tapURL;
    SystemSoundID tapSound;
    tapURL = CFBundleCopyResourceURL(mainBundle, CFSTR("tap"), CFSTR("aif"), NULL);
    AudioServicesCreateSystemSoundID(tapURL, &tapSound);
    
    // Play the sound async
    AudioServicesPlaySystemSound(tapSound);
    

    我还没有使用AudioServicesPlaySystemSound 制作 MP3。但它可以完美播放 AIF 文件。

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多