代码之家  ›  专栏  ›  技术社区  ›  Domness

iPhone SDK:通过按钮播放单个WAV

  •  3
  • Domness  · 技术社区  · 17 年前

    我目前正在尝试这段代码:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"dream" ofType:@"m4a"];  
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
    theAudio.delegate = self;  
    [theAudio play];
    

    但是,SDK表示ViewController不实现AVAudioPlayer代理。

    有人知道如何使用2.2 SDK播放WAV(或M4a)吗?

    4 回复  |  直到 9 年前
        1
  •  9
  •   Noah Witherspoon    17 年前

    您需要添加 <AVAudioPlayerDelegate> 在控制器类声明的末尾,按照以下行:

    @interface MyViewController : UIViewController <AVAudioPlayerDelegate>
    

    这可能会产生更多关于你需要实现的委托方法的警告——遵循这些警告,你就可以开始了。

        2
  •  1
  •   Noah Witherspoon    17 年前

    有趣。尝试 NSURL -URLWithString: -这就是我与您正在使用的捆绑方法结合使用的方法。除此之外,我没有看到任何可能导致问题的东西。你正在设备上进行测试,对吗?

        3
  •  1
  •   Community Mohan Dere    9 年前

    答案 I posted here 肯定会帮助你。只需将音频服务用于短波声音,除非你需要并发,否则它会更快。

        4
  •  -1
  •   dave dave    17 年前

    iPhoneToot.com有自己的itootSound类和VIDEO,向您展示如何用2行代码播放wav文件。在iPhoneToot.com上查看它们。它将为您节省数小时甚至数天的时间!!