iphone,即使在静音或静音模式下如何播放声音? [英] iphone, how to play sound even in silent or mute mode?

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

问题描述

作为主题......是否可能?

as topic... is it possible ?

谢谢

我已按如下方式附上代码,请检查哪一步错误。谢谢。

again, I have attached the code as follows, please check which step is wrong .thanks.

    //@step
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory =   kAudioSessionCategory_MediaPlayback;
OSStatus error = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,                                            sizeof(sessionCategory),&sessionCategory);

if (error) 
    printf("ERROR AudioSessionSetProperty ! %d\n", error);

//@step 
NSString* filePath = @"AlarmClockBell.caf";
[Util restoreResourceFile:filePath];
filePath =[Util getFileFullPathFromSysDoc:filePath];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];   
NSError* error ;
AVAudioPlayer * audioPalyer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: &error];
if (nil == audioPalyer) 
{
    AppTrace3(self, @"Faild to play", soundFileURL, error);
    return FALSE;
}
[audioPalyer prepareToPlay];
[audioPalyer setVolume: 5 ];
[audioPalyer setDelegate: self];
 audioPalyer.numberOfLoops = 10;

[audioPalyer play];

谢谢......

推荐答案

如果查看音频会话类别下的文档,您将找到许多模式,您可以设置这些模式以告诉系统您的应用计划如何使用音频。默认值为 AVAudioSessionCategorySoloAmbient ,它跟踪铃声/静音开关和屏幕锁定。

If you look in the docs under Audio Session Categories, you'll find a number of modes that you can set to tell the system how your app plans to use audio. The default is AVAudioSessionCategorySoloAmbient which tracks the ring/silent switch and the screen lock.

要让您的应用忽略响铃/静音开关设置,您可以尝试更改类别:

To have your app ignore the ring/silent switch settings, you could try changing the category:

#import <AudioToolbox/AudioToolbox.h>

AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                         sizeof(sessionCategory),&sessionCategory);

如果你想让iPod音频继续在后台播放,你还​​需要检查 kAudioSessionProperty_OverrideCategoryMixWithOthers

If you want to allow iPod audio to continue playing in the background, you'll also want to check kAudioSessionProperty_OverrideCategoryMixWithOthers.

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

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