在静音模式下如何开始播放音频&锁定在 iOS 6 中? [英] How do I start playing audio when in silent mode & locked in iOS 6?

查看:30
本文介绍了在静音模式下如何开始播放音频&锁定在 iOS 6 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 打开应用
  2. 打开静音模式".
  3. 按锁定按钮"
  4. 数小时后应用仍可开始播放声音,在此期间不播放任何音频.

执行此操作的应用

许多警报应用程序都设法做到了这一点&我不认为他们正在使用静音音频来保持应用程序运行,因为如果您实际上在家中退出应用程序,它们不会发出声音.

Apps that do this

A lot of alarm apps have manage to do this & I dont think they are using silent audio to keep the app running as they do not sound if you actually exit the app with home.

  • 闹钟专业版
  • 我的时钟
  • 海浪警报
  • 报警
  • iHome
  • ...

...他们是在被锁定后保持循环运行还是通知(无法在静音状态下播放声音)启动应用程序以播放音频或其他方法?

...Are they keeping a loop running after being locked some how or it a notification(which cant play sound in silent) starting the app back up to play the audio, or some other method?

AVAudioPlayer 使用:

AVAudioPlayer using:

AudioSessionInitialize(nil, nil, nil, nil);
AudioSessionSetActive(YES);

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

并将 Info.plist 设置为:

And setting Info.plist to:

必需的背景模式(UIBackGroundModes) - 应用播放音频(音频)

Required background modes(UIBackGroundModes) - App plays audio (audio)

当应用程序正在运行并在屏幕上时,我甚至可以在静音状态下播放音频.如果音频已经在运行,则可以使用主页按钮关闭应用程序,然后音频将运行.但是,如果应用程序没有播放音频,并且屏幕被锁定,则所有线程都将被终止,并且永远不会播放音频.这些应用如何解决这个问题?

I can play audio even in silent when the app is running and on screen. If the audio is already running the app can be closed with home button and audio will run. BUT if the app is not playing audio, and the screen is locked, all threads are killed and audio is never played. How do theses apps manage to work around this?

A.使用带有无限循环的beginBackgroundTaskWithExpirationHandler:"来保持应用程序无限期运行.

优点:

  • 看起来您可以在很多情况下完成这项工作,即使用户按下 Home 键也是如此.

缺点:

  • 据我所知,这违反了苹果的政策.
  • 将使用更多资源

评论:

我几乎可以使用它,并且可能会进行一些调整.这似乎不是所有这些其他警报所做的事情,因为如果您按 Home BTN,它们不会继续运行.这表明他们使用某种方法允许他们在锁定但不在 BG 中时运行.(这是我需要的)

I've almost got this to work and might with some tweaking. This does not seem to be what all these other alarms are doing as they do not keep on running if you press home BTN. Which suggest that they use some method that gives them permission to run while locked but not in the BG. (Which is what I need)

此外,当您询问您还剩多少时间跑步时,您会得到大约 10 分钟.通过在其中放置一个无限循环,数字实际上会下降到 0,然后连续数小时进入负数.(经过测试)我不确定这在现实世界中或在应用程序接受方面会如何表现.

Also, When you ask how much time you have left running you get aprox 10 min. By dropping an infinite loop in there the numbers will actually run down to 0 and then go into the negatives for hours on end.(tested) Im not sure how this would behave in the real world or in terms of app acceptance.

B.使用静音音频循环来伪装成连续音频播放媒体中心

优点:

  • 在锁定时工作,并在大多数情况下继续运行.

缺点:

  • 如果被另一个媒体中心打断或在其他情况下可能会失败.
  • 也可以违反苹果政策.

评论:

这可以在很多情况下工作,但到目前为止并不理想.而且因为就像我再说一遍,必须有另一种方法没有记录在案.

This can work I a lot of situations but is by far not ideal. And since Like I say again, there has to be another method that is not documented.

使用列出的 APP 进行测试表明它们没有使用我刚刚描述的两种方法中的任何一种.方法A"似乎更接近,但如果实施将不会表现出这些应用程序的行为方式.

Testing with the listed APPs suggests that they are not using any of the two methods I just described. Method 'A' seems to be closer but if implemented would not behave how these apps behave.

我使用苹果开发者票来获取更多信息,我也会通过这些方式发布任何新发现.

I used a apple developer ticket to get more info, I'll post any new findings along those means as well.

感谢您的任何见解,并感谢您迄今为止的参与.

Any insight is appreciated, and for your participation thus far.

推荐答案

您需要在 plist 文件中进行一些更改.

You need to make couple of changes in plist file.

即1) 将必需的后台模式设置为应用播放音频

i.e. 1) Set Required background mode to App plays audio

2) 设置应用程序不在后台运行为YES.

2) set Application does not run in background to YES.

 NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

那么,你需要在 AppDelege 中写这么多代码

Then, you need to write these much code in AppDelege

现在,您可以在手机屏幕锁定或在后台轻松播放音频.

Now, you can easily run audio while phone screen locks or in background.

这篇关于在静音模式下如何开始播放音频&锁定在 iOS 6 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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