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

推荐答案

这是不明智的,但我凭什么说你做不到呢.你可能有充分的理由go 演奏声音.

如果你正在使用音频会话,那么在文件开头加上<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级参考资料.

Objective-c相关问答推荐

如何判断 iPhone 现在在哪个位置(横向或纵向)?

使用自动布局自定义纵横比

iPhone:将日期字符串转换为相对时间戳

如何复制 .xib 文件?

UIApplication.sharedApplication.delegate.window 和 UIApplication.sharedApplication.keyWindow 有什么区别?

使用 arc4random() 时如何 Select 值的范围

为什么 NSDateFormatter 为这 4 个时区返回 nil 日期?

如何将 UInt32 设置为最大值

在 UILabel.attributedText *not* 蓝色和 *not* 下划线

在 ARC 中清零弱引用

判断对象是 NSArray 还是 NSDictionary

使用 UIView animateWithDuration 进行动画处理时无法 UIButton

我们可以在 UIPageViewController 中自定义页面指示器吗?

接口类型不能静态分配?

iOS 7 - 键盘动画

我可以设置 `UILabel` 的 `attributedText` 属性吗

iPhone - 时区便利方法之间的差异

无法加载 xcode 项目,因为它已从另一个项目或工作区打开

获取临时目录中文件的文件路径和 URL

BOOL到 NSString