Question

I am using Google Text To Speech API to speak an NSString and I am doing the following:

-(void) playSound {

    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSLog(@"YES");
    }

    NSString *stringer = [NSString stringWithFormat:@"%@", URLArray[playerInt]];
    NSLog(@"%i",playerInt);

    NSURL *url = [NSURL URLWithString:[stringer stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url] ;
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" forHTTPHeaderField:@"User-Agent"];
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    [data writeToFile:path atomically:YES];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:nil];
    player.delegate = self;
    [player prepareToPlay];
    [player play];

    NSLog(@"%@", stringer);
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)audioPlayer successfully:(BOOL)flag
{
    [self playSound];
}

Let me explain the code. I am using this as the google text to speech api: http://translate.google.com/translate_tts?&tl=en-US&ie=UTF-8&q=Hello

I need to have large paragraphs spoken so they are being broken into multiple links which are in the array URLArray. Then since URLArray is an array with a bunch of links, I am setting int playerInt = -1 initially (because we do playerInt+=1 afterwards setting it to 0). So as soon as the first sound is finished playing, it restarts it and sets playerInt one integer greater (so 1) and speaks URLArray[playerInt] so the second link.

There are a couple problems with this. First of all, when I run this, the audio is not even playing. Secondly, the audioPlayerDidFinishPlaying method is not being called.

Any help is appreciated.

Était-ce utile?

La solution

You seem to set things up correctly here, so for the first part I would check to see if this:

[[NSFileManager defaultManager] fileExistsAtPath:path]

returns YES. It might not have finished saving the file before you start playing it. Or for the worst case, it would take a little longer for sendSynchronousRequest to fail.

For the second part:

audioPlayerDidFinishPlaying is not being called because you need to set this up:

player.delegate = self;

Hope this helps.

Autres conseils

Going from what you've provided, it looks like you haven't activated the audio session. Try adding

[[AVAudioSession sharedInstance] setActive:YES error:nil];

If you're using ARC, you will need to retain the audio so it doesn't automatically set to nil, add the following property into the header.

@property (nonatomic, strong) AVAudioPlayer *player;

I answered this to a similar issue before and it worked for them AVAudioPlayer not playing sound IOS

Hope this helps for you. Cheers Jim

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top