所以我现在正在尝试的是当应用程序在后台接收远程通知(或者可能从暂停状态唤醒)时播放消息.
应用程序从暂停模式唤醒后,声音根本无法播放.
当应用程序在前台时,在didReceiveRemoteNotification:调用方法后立即播放声音.
didReceiveRemoteNotification:当应用程序从挂起模式唤醒时调用方法时,立即播放声音的适当方法是什么?
这是一些代码(语音管理器类):
-(void)textToSpeechWithMessage:(NSString*)message andLanguageCode:(NSString*)languageCode{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
DLog(@"Activating audio session");
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:&error]) {
DLog(@"Unable to set audio session category: %@", error);
}
BOOL result = [audioSession setActive:YES error:&error];
if (!result) {
DLog(@"Error activating audio session: %@", error);
}else{
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:message];
[utterance setRate:0.5f];
[utterance setVolume:0.8f];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:languageCode];
[self.synthesizer speakUtterance:utterance];
}
Run Code Online (Sandbox Code Playgroud)
}
-(void)textToSpeechWithMessage:(NSString*)message{
[self textToSpeechWithMessage:message andLanguageCode:[[NSLocale preferredLanguages] objectAtIndex:0]];
}
Run Code Online (Sandbox Code Playgroud)
后来在 …
objective-c ios avspeechsynthesizer background-mode avspeechutterance
要求 -我正在构建一个超本地应用程序,它将根据用户的位置为用户提供服务.在应用程序中我可以获得他当前的位置并相应地显示优惠,但我现在需要的是根据他的位置向用户发送推送通知.所以我想找出用户的位置并根据他的位置发送优惠.
我已经阅读了有关重大变更位置服务的 Apple文档,但后来这个答案说,一旦应用程序被杀,它将无法运行.
我还阅读了有关跟踪用户位置的信息,但这对我来说无效.我在后台没有获得超过5个更新.
我目前的代码viewDidLoad-
if (self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
self.locationManager.allowsBackgroundLocationUpdates = true;
self.locationManager.pausesLocationUpdatesAutomatically = false;
if ([CLLocationManager authorizationStatus] != AVAuthorizationStatusAuthorized) {
[self.locationManager requestAlwaysAuthorization];
}
}
[self.locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
我的委托方法看起来像这样 -
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) {
// Get updated data according to location
} else {
// Send location to server
}
}
Run Code Online (Sandbox Code Playgroud)
请建议一些适当的方式,我可以生活在1公里左右的精度. …
我在应用程序中使用CADisplayLink作为计时器。我之所以使用CADisplayLink,是因为我的应用非常依赖CADisplayLink的准确性/准确性。在我看来,NSTimer不是合适的替代品。
问题是,有时我需要CADisplayLink在应用程序处于后台时触发并调用其选择器。这有可能吗?我知道CADisplayLink链接到应用程序UI的刷新率。这是否意味着当我的应用程序在后台运行时,没有UI,因此在这种情况下无法使用CADisplayLink?或者,是否可以通过更改添加到其中的运行循环或通过更改forMode参数来以某种方式使用CADisplayLink ?还是使用某些GCD欺骗/变通方法?
注意:我的应用程序正在使用AudioPlayer,因此启用了后台功能。我可以在后台运行其他代码。为了测试,我用NSTimer切换了CADisplayLink,然后触发了NSTimer并调用了它的选择器。我只是无法启动CADisplayLink,也找不到任何有关在后台使用CADisplayLink的权威性文档
这是与显示链接有关的当前代码:
func beginTimer(){
dispatch_async(dispatch_get_main_queue()){
self.displayLink = CADisplayLink(target: self, selector: "update")
self.displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
}
func update(){
delegate?.executeCallback
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,当应用程序在后台时,它将数据发送到服务器.以下是负责数据发送的代码:
-(bool) sendStats: (MCStatsSender*) val{
if(![self checkInternet]){ //Using Reachability here
return false;
}
NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:
self.propertyA.value, "key1",
val.data, "key2",
nil];
[myNetworkManager doRequest:[myRequestManager createWithStringAndDictionary:MY_URL Data:inputData handler:myHandler user:val]];
return true;
}
Run Code Online (Sandbox Code Playgroud)
所以这inputData是一个带字符串的简单字典.
一种方法doRequest基于NSURLSession,基本上如下所示:
-(void) doRequest: (MCRequest*) request{
[tasks addObject:request];
if(m_session == nil){
NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"key-%lu",reqid]];
m_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
NSURLSessionDataTask* task = [m_session dataTaskWithRequest:request.generatedRequest];
request.reqId = task.taskIdentifier;
[task resume];
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,一切都通过Wi-Fi工作,应用程序进入后台,几分钟后,自定义蓝牙设备发送一些数据并从挂起模式唤醒应用程序.iOS应用程序收到数据后,如果设备通过3G连接,则无法将其发送到服务器.我很肯定通过蓝牙发送的数据是收到的,因为它存储在本地数据库中.
另外还有一个重要的事实.如果应用程序通过Xcode运行,即使设备通过3G连接,应用程序也会从后台发送数据.为此,我运行一个应用程序,然后点击"主页"按钮将其置于后台.
不知道有什么区别,以及为什么app通过电缆连接到Mac时会有不同的行为,为什么数据不通过3G(甚至2G)发送?
附加信息:
我不是要上传文件,而只是将JSON发送到服务器.
我猜这是与 相关的后台模式ExternalAccessory.framework。
但是有关外部附件的文档说:“如果您的应用程序在附件通知到达时在后台暂停,则该通知将被放入队列中。当您的应用程序再次开始运行(无论是在前台还是后台)时,队列中的通知发送到您的应用程序。通知也会尽可能地合并和过滤,以消除任何不相关的事件。例如,如果在您的应用程序暂停时连接了配件并随后断开连接,您的应用程序最终将不会收到任何发生此类事件的指示.”。
这意味着外部配件通信将排队直到应用程序进入前台模式,但其他后台模式(如 )Location在Bluetooth LE后台模式下实时工作。所以我怀疑只是排队是唯一能做的事情。是不是真的?
我正在开发一款TextToSpeech应用.我在a中写了一段UITextField,然后按下Speak按钮.声音根据文中所写的文字播放UITextField.

但是,当应用程序处于后台模式时,音频将停止播放.如何在后台模式下继续播放声音?类似于音频播放器如何在后台播放歌曲.
我使用以下代码进行文本到语音转换:
#import "ViewController.h"
#import "Google_TTS_BySham.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic,strong)Google_TTS_BySham *google_TTS_BySham;
@property (nonatomic,strong)IBOutlet UITextField *txtString;
@end
@implementation ViewController
#pragma mark - View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Button Tapped event
- (IBAction)btnSpeakTapped:(id)sender{
NSString *str = [NSString stringWithFormat:@"%@",_txtString.text];
self.google_TTS_BySham = [[Google_TTS_BySham alloc] init];
[self.google_TTS_BySham speak:str];
}
Run Code Online (Sandbox Code Playgroud) 我正在使用最新的适用于 iOS 的 Google Cast SDK(版本 3.1.1.10003),我们的应用程序正在远程控制 Google Cast 设备,例如它会改变它们的音量。当我们的应用程序进入后台时,它也需要这样做。
但是,在应用程序进入后台时GCKSessionManager调用suspendSessionWithReason:。也就是说,会话将被暂停,因此我们的应用程序无法再控制它。
GCKSessionManager当应用程序进入后台时,如何不暂停会话?
编辑:我在下面提供了一个解决方案,但由于重新连接时出现严重延迟,这对用户不友好。
即使应用程序在后台,我的应用程序也希望每 5 秒更新一次有关用户位置的服务器。我为它实现了后台获取。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
application.setMinimumBackgroundFetchInterval(5.0)
return true
}
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.NewData)
UpdateData()
}
func UpdateData(){
print("UpdateData executes")
// function to update data when ever this method triggers
}
Run Code Online (Sandbox Code Playgroud)
但问题是