即使在静音模式下也能在 iPhone 上播放声音 [英] Play sound on iPhone even in silent mode

查看:106
本文介绍了即使在静音模式下也能在 iPhone 上播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make an application which creates sound, music, or system sound when an iPhone is in silent mode. Is it possible to play any type of sound whether music or system tones when it is silent mode?

推荐答案

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

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.

这篇关于即使在静音模式下也能在 iPhone 上播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆