我想制作一个应用程序,可以在iPhone静音模式下创建声音、音乐或系统声音。 在静音模式下是否有可能播放任何类型的声音,无论是音乐还是系统提示音?
我想制作一个应用程序,可以在iPhone静音模式下创建声音、音乐或系统声音。 在静音模式下是否有可能播放任何类型的声音,无论是音乐还是系统提示音?
虽然并不建议这样做,但我有什么资格说你不能这么做呢。你可能有一个很好的理由来播放声音。
如果你正在使用音频会话(Audio Sessions),那么请在文件开头包含<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类参考。
AVPlayerViewController
之前设置AVAudioSession
类别时,我遇到了这个问题。如果我在presentViewController
调用的完成块中设置类别(并在实际调用play()
之前),则在两个设备系列上都可以正常工作。 - Jerrot对于Swift 2、3、4.2版本
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
是的,您可以在手机静音时播放声音。只需使用AVAudioPlayer类。
默认情况下,播放音频会忽略iPhone上的静音开关设置。换句话说,如果您调用播放声音的操作,而iPhone上的静音(硬件)开关被设置为静音,则仍然会听到声音。
这就是您想要的。现在您知道了当手机处于静音模式时如何播放音频会依旧有声音,您只需要知道如何创建一个音频会话来播放声音,像这样:
取自此网站:http://iosdevelopertips.com/audio/playing-short-sounds-audio-session-services.html
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"RapidFire" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);
要使这个工作,您需要导入 header 文件,并将 AudioToolbox.framework 添加到您的项目中。适用于 iOS 6 及以上版本
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
// 处理错误
}
- Guy只需将以下内容放入viewDidLoad
中:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory), &sessionCategory);
如果需要在静音模式下或后台播放声音,请尝试以下方法:
将以下代码放入application:didFinishLaunchingWithOpitons:中:
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
并且在 applicationDidEnterBackground 中加入以下代码:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Swift - 在手机静音时播放音频/视频前调用此函数以强制播放。这也会使当前播放的其他音频静音。
如果不想静音其他音频,可以将.duckOthers
更改为.mixWithOthers
。
func setupAudio() {
let audioSession = AVAudioSession.sharedInstance()
_ = try? audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
_ = try? audioSession.setActive(true)
}
对于Objective C,您可以使用以下代码播放系统声音:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
*The system path could be modified.*
NSString *path = @"/System/Library/Audio/UISounds/begin_record.caf";
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player play];