5

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.

enter image description here

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];
}
4
  • And code of to play sound is??
    – Tirth
    Feb 4, 2015 at 8:56
  • It is library of Google_TTS
    – Jagat Dave
    Feb 4, 2015 at 8:57
  • Check your application info.plist file and set property Application does not run in background.
    – Tirth
    Feb 4, 2015 at 9:08
  • In my application info.plist file i added the following: Application does not run in background : NO
    – Jagat Dave
    Feb 4, 2015 at 9:13

2 Answers 2

6

Add the following code in info.plist file...

Application does not run in background : NO

Required background modes : App plays audio or streams audio/video using AirPlay

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;
        }
    }
}
1
  • how does that even work without playing any music file. your code is pretty much missing and if that even make sense for TTS at all?
    – Emil
    Aug 13, 2023 at 17:47
1

Add "App plays audio or streams audio/video using AirPlay" under "Required background modes" property on your info.plist

Hope this will help...

3
  • I add "App plays audio or streams audio/video using AirPlay" and "App provides Voice over IP services" under "Required background modes" property on my info.plist...It is work properly in simulator but not in real ios device
    – Jagat Dave
    Feb 5, 2015 at 8:40
  • @AhmetAkkök Yes...It is working properly in real device.
    – Jagat Dave
    Jun 6, 2017 at 4:42
  • @Jagat Dave Thank you, I understand that you have already provided the accepted answer here, appreciated 👍 Jun 6, 2017 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.