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

标签 iphone objective-c ipad avaudioplayer

我想制作一个应用程序,当 iPhone 处于静音模式时,它可以创建声音、音乐或系统声音。静音模式下是否可以播放任何类型的声音,无论是音乐还是系统铃声?

最佳答案

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

如果您使用 Audio Session ,请包含 <AVFoundation/AVFoundation.h>在文件的开头和

[[AVAudioSession sharedInstance]
                setCategory: AVAudioSessionCategoryPlayback
                      error: nil];

应该可以解决问题。请注意,如果您播放音乐或声音,iPod 播放将会暂停。

完成此操作后,可能在播放声音的类之一的初始化中,您可以像这样实例化声音:

// probably an instance variable: AVAudioPlayer *player;
NSString *path = [[NSBundle mainBundle] pathForResource...];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url];

完成后,您可以随时使用它:

[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类引用。

关于iphone - 即使在静音模式下也能在 iPhone 上播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57952348/

相关文章:

ios - 当设备旋转时,我的应用程序的主视图不会调整大小以填满屏幕

iphone - 将 View 发送到后面

ios - 如何将 NSDictionary 转换为 JSON 对象?

c++ - 如何在 ios 平台上使用 c++ 实现 GameCenter

ipad - 为什么 iOS7 中的 UIAlertController 收到 nil 值?

iphone - 来自多任务时如何使手电筒/闪光灯可靠

iphone - 如何隐藏 iPhone UITextView 的键盘 - 通过 return 键

ios - 如何通过在不同的 View Controller 上按下按钮来更改 TableView 数据?

ios - 如何在 SpriteKit 上制作切换按钮

iphone - 如何创建具有多个SDK版本的iOS应用?