且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

即使在静音模式下也能在 iPhone 上播放声音

更新时间:2022-06-11 00:15:23

这是不可取的,但我凭什么说你不能这样做.您可能有充分的理由播放声音.

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.

如果您使用的是音频会话,则在文件开头包含

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.