solarissf
solarissf

Reputation: 1277

NSUrlConnection doesnt seem to be working,

I'm trying to use googletts converted and seem to be running into trouble. I think I made the class correctly in terms of structure. But I dont think the NSURLConnection is returning any useful data. Can someone please help me check if its working and if not why?

In the connectiondidfinishloading section I am checking download length NSLog(@"hmmmm %lu", (unsigned long)[downloadedData length]) which is returning 0. Thats why I think its not working.

The goal is to have the app speak what is downloaded

Thanks!!!

I just have a button in my view controller that launches everything

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "RJGoogleTTS.h"


@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *userLabel;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geoCoder;

@property (strong, nonatomic) RJGoogleTTS *googleTTS;

- (IBAction)geoCodeLocation:(id)sender;

- (IBAction)rjGoogleButton:(id)sender;
@end

here is the implementation file

- (IBAction)rjGoogleButton:(id)sender {

    googleTTS = [[RJGoogleTTS alloc]init];

    [googleTTS convertTextToSpeech:@"How are you today user?"];

}

RJGoogleTTS.h

#import <Foundation/Foundation.h>

@protocol RJGoogleTTSDelegate <NSObject>

@required
- (void)receivedAudio:(NSMutableData *)data;
- (void)sentAudioRequest;

@end

@interface RJGoogleTTS : NSObject {
    id <RJGoogleTTSDelegate> delegate;
    NSMutableData *downloadedData;
}

@property (nonatomic, retain) id <RJGoogleTTSDelegate> delegate;
@property (nonatomic, retain) NSMutableData *downloadedData;

- (void)convertTextToSpeech:(NSString *)searchString;

@end

.m

#import "RJGoogleTTS.h"
#import <AVFoundation/AVFoundation.h>

@implementation RJGoogleTTS
@synthesize delegate, downloadedData;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)convertTextToSpeech:(NSString *)searchString {
    NSString *search = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?q=%@", searchString];
    search = [search stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSLog(@"Search: %@", search);
    self.downloadedData = [[NSMutableData alloc] initWithLength:0];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:search]];
    [request setValue:@"Mozilla/5.0" forHTTPHeaderField:@"User-Agent"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    [delegate sentAudioRequest];




}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"did you receive response user");
    [self.downloadedData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"did you receive data user");
    [self.downloadedData appendData:data];

//    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithData:downloadedData error:nil];
//    [audioPlayer play];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Failure");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"it worked user!!!");
    [delegate receivedAudio:self.downloadedData];
    NSLog(@"hmmmm %d", [self.downloadedData length]);



//    NSString *txt = [[NSString alloc] initWithData:downloadedData encoding: NSASCIIStringEncoding];
//    NSLog(@"%@hello",txt);

}

@end

Upvotes: 0

Views: 129

Answers (3)

rdelmar
rdelmar

Reputation: 104092

There's nothing wrong with your NSURLConnection methods. Your search string is wrong. It should be:

NSString *search = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?tl=en&q=%@",searchString];

Upvotes: 1

Eric Qian
Eric Qian

Reputation: 2256

You didn't start the URLConnection. Change [[NSURLConnection alloc] initWithRequest:request delegate:self]; like this [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

Upvotes: 0

Richard Brown
Richard Brown

Reputation: 11444

Remember that this is happening asynchronously. You set the size of downloadedData to 0 after your call the NSURLConnection.

Move:

self.downloadedData = [[NSMutableData alloc] initWithLength:0];

before your call to NSURLConnection.

Upvotes: 2

Related Questions