ios - 应用程序处于后台模式时的文本转语音功能?

标签 ios objective-c text-to-speech background-mode

我正在开发一个 TextToSpeech 应用程序。我在 UITextField 中写了一段,然后按下了 Speak 按钮。声音根据 UITextField 中写入的文本播放。

enter image description here

但是,当应用程序处于后台模式时,音频将停止播放。如何在后台模式下继续播放声音?类似于音频播放器如何在后台播放歌曲。

我将以下代码用于文本到语音:

#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];
}

最佳答案

info.plist文件中添加如下代码...

应用程序不在后台运行:否

所需的后台模式:应用使用 AirPlay 播放音频或流式传输音频/视频

然后在AppDelegate.m文件中添加如下代码

- (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;
        }
    }
}

关于ios - 应用程序处于后台模式时的文本转语音功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317036/

相关文章:

ios - 画直线 Google map iOS

ios - RestKit请求映射-发布对象数组

ios - iOS 7 UITableViewCell 中与 AutoLayout 冲突的约束

ios - 部分改变UIView的背景颜色

c# - 如何使用 Microsoft 语音对象库将 wav 字节数组写入响应?

javascript - 我可以通过 JavaScript 使用 NSSpeechSynthesizer 吗?

android - 如何让android设备 "speak"中文?

ios - Kingfisher - 无法从 Web 加载图像 URL

ios - 通过 CGMutablePathRef 动画绘制在 drawRect 中的框

ios - 单例是否在 block 内创建保留循环?