应用处于后台模式时的文字转语音功能? [英] Text To Speech functionality when app is in background mode?
问题描述
我正在使用 TextToSpeech
应用。我在 UITextField
中写了一段,然后按说话按钮。声音根据 UITextField
中的文字进行播放。
I am working on a TextToSpeech
app. I write one paragraph in a UITextField
, then I press the Speak button. Sound plays according to the text written in the UITextField
.
但是,当应用程序处于后台模式时,音频将停止播放。如何在后台模式下继续播放声音?类似于音频播放器如何在后台播放歌曲。
However, when the app is in background mode, the audio stops playing. How can I continue to play the sound in background mode? Similar to how an audio player can play a song in the background.
我使用以下代码进行文字转语音:
I am using the following code for text to speech:
#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];
}
推荐答案
在<\\ n>中添加以下代码code> info.plist file ...
Add the following code in info.plist
file...
应用程序无法在后台运行:否
所需的背景模式:App使用AirPlay播放音频或流音频/视频
然后在 AppDelegate.m
文件中添加以下代码
and then add the following code in AppDelegate.m
file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent {
if (theEvent.type == UIEventTypeRemoteControl) {
switch(theEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
break;
case UIEventSubtypeRemoteControlPause:
[[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
break;
case UIEventSubtypeRemoteControlStop:
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
[[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPause" object:nil];
break;
default:
return;
}
}
}
这篇关于应用处于后台模式时的文字转语音功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!