在iPhone应用程序中播放声音片段的最简单方法是什么?

时间:2022-05-19 12:16:23

I want to be able to play a sound clip in an iPhone OS app. I've seen info on both NSSound as well as AVFoundation as being means for getting sound clips played on an iPhone OS device, but I'm still not clear on the subject and could use some help. No need to spell it out for me step-by-step in actual code, but if someone could give me a hint as to the general direction (i.e. which classes I should be focusing on) in which I should start moving I'll fill in the blanks myself. So, what's the SIMPLEST way to play a sound clip in an iPhone app?

我希望能够在iPhone OS应用程序中播放声音片段。我已经看到NSSound和AVFoundation的信息都是在iPhone OS设备上播放声音片段的方法,但我仍然不清楚这个主题并且可以使用一些帮助。不需要在实际代码中逐步拼出我,但是如果有人能给我一个关于我应该开始移动的大方向(即我应该关注哪些类)的提示我会填写在我自己的空白。那么,在iPhone应用程序中播放声音片段的简单方法是什么?

2 个解决方案

#1


Apple has an article on this subject, see: this link

Apple有一篇关于这个主题的文章,请参阅:此链接

AVAudioPlayer is the simplest way to play sounds of any length, looping or not, however it requires iPhone OS 2.2 or higher. A simple example:

AVAudioPlayer是播放任何长度,循环或不循环声音的最简单方式,但它需要iPhone OS 2.2或更高版本。一个简单的例子:

NSString *soundFilePath =
                [[NSBundle mainBundle] pathForResource: @"sound"
                                                ofType: @"wav"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

AVAudioPlayer *newPlayer =
                [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                                       error: nil];
[fileURL release];

[newPlayer play];

[newPlayer release];

It will play virtually any file format (aiff,wav,mp3,aac) Keep in mind that you can only play one mp3/aac file at a time.

它几乎可以播放任何文件格式(aiff,wav,mp3,aac)请记住,你一次只能播放一个mp3 / aac文件。

#2


Here's the simplest way I know of:

这是我所知道的最简单的方法:

  1. Convert your sound file to caf (use afconvert commandline tool) and add to your project.

    将您的声音文件转换为caf(使用afconvert命令行工具)并添加到您的项目中。

    caf stands for Core Audio Format (I think...)

    caf代表核心音频格式(我认为...)

  2. Look for SoundEffect.h and .m in Apple's sample code. I believe both Metronome and BubbleLevel have it.

    在Apple的示例代码中查找SoundEffect.h和.m。我相信节拍器和BubbleLevel都有它。

  3. Copy to your project
  4. 复制到您的项目

  5. Write code like below:

    编写如下代码:

    SoundEffect *SimpleSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"soundfile" ofType:@"caf"]];
    [SimpleSound play];
    [SimpleSound release];
    

Study SoundEffect.m to get an idea of simple sound handling.

研究SoundEffect.m以了解简单的声音处理。

#1


Apple has an article on this subject, see: this link

Apple有一篇关于这个主题的文章,请参阅:此链接

AVAudioPlayer is the simplest way to play sounds of any length, looping or not, however it requires iPhone OS 2.2 or higher. A simple example:

AVAudioPlayer是播放任何长度,循环或不循环声音的最简单方式,但它需要iPhone OS 2.2或更高版本。一个简单的例子:

NSString *soundFilePath =
                [[NSBundle mainBundle] pathForResource: @"sound"
                                                ofType: @"wav"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

AVAudioPlayer *newPlayer =
                [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                                       error: nil];
[fileURL release];

[newPlayer play];

[newPlayer release];

It will play virtually any file format (aiff,wav,mp3,aac) Keep in mind that you can only play one mp3/aac file at a time.

它几乎可以播放任何文件格式(aiff,wav,mp3,aac)请记住,你一次只能播放一个mp3 / aac文件。

#2


Here's the simplest way I know of:

这是我所知道的最简单的方法:

  1. Convert your sound file to caf (use afconvert commandline tool) and add to your project.

    将您的声音文件转换为caf(使用afconvert命令行工具)并添加到您的项目中。

    caf stands for Core Audio Format (I think...)

    caf代表核心音频格式(我认为...)

  2. Look for SoundEffect.h and .m in Apple's sample code. I believe both Metronome and BubbleLevel have it.

    在Apple的示例代码中查找SoundEffect.h和.m。我相信节拍器和BubbleLevel都有它。

  3. Copy to your project
  4. 复制到您的项目

  5. Write code like below:

    编写如下代码:

    SoundEffect *SimpleSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"soundfile" ofType:@"caf"]];
    [SimpleSound play];
    [SimpleSound release];
    

Study SoundEffect.m to get an idea of simple sound handling.

研究SoundEffect.m以了解简单的声音处理。