更新时间:2022-06-11 00:15:11
这不可行,但是我该说谁不能这样做。您可能有充分的理由播放声音。
It's not advisable, but who am I to say you can't do it. You may have a good reason to be playing sound.
如果您使用的是音频会话,请包含< AVFoundation / AVFoundation.h>
在文件的开头和
If you are using Audio Sessions, then include <AVFoundation/AVFoundation.h>
at the start of your file and
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: nil];
应该可以解决问题。请注意,如果您播放音乐或声音,则iPod播放将暂停。
should do the trick. Note if you play music or sounds, then iPod playback will be paused.
完成此操作后,可能会在某个类的初始化中播放声音,你可以实例化这样的声音:
Once this has been done, probably somewhere in the initialization of one of your classes that plays the sounds, you can instantiate sounds like this:
// probably an instance variable: AVAudioPlayer *player;
NSString *path = [[NSBundle mainBundle] pathForResource...];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url];
完成后,您可以随时随地使用它:
When that's done, you can play with it any time you want with:
[player play]; // Play the sound
[player pause]; // Pause the sound halfway through playing
player.currentTime += 10 // skip forward 10 seconds
player.duration // Get the duration
还有其他不错的东西。查看 AVAudioPlayer 类引用。
And other nice stuff. Look up the AVAudioPlayer Class reference.