2013-03-12 79 views
0

我试图使用googletts转换并似乎遇到了麻烦。我认为我在结构上做了正确的课。但我不认为NSURLConnection正在返回任何有用的数据。有人可以帮助我检查它的工作,如果不是为什么?NSURLConnection的似乎并不奏效,

在connectiondidfinishloading部分我检查下载长度 的NSLog(@“hmmmm%录”,(无符号长)downloadedData长度]) 它返回0。这就是为什么我认为它不工作。

的目标是让应用程序说什么下载

谢谢!

我只是在我的视图控制器一个按钮,启动一切

#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 

这里是实现文件

- (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 
+0

是否所有工作日志? – rdelmar 2013-03-12 02:07:46

回答

1

没有什么不对您NSURLConnection的方法。您的搜索字符串是错误的。它应该是:

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

天才....所以现在看来​​我正在获取数据。 2013-03-12 09:44:18.332 LocalWeatherV3 [1748:c07] hmmmm 7632 ... 7632是字节,如果数据在零之前。你会不会知道我究竟如何看待内容?或者因为这是音频,没有什么可以看到的? – solarissf 2013-03-12 13:49:00

+0

我在最后加入了DidFinishLoading部分,AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithData:downloadedData error:nil]; [audioPlayer play]; .....但我听不到任何声音,有什么想法? – solarissf 2013-03-12 14:02:41

+0

@solarissf,对不起,我不知道为什么这是行不通的。 – rdelmar 2013-03-12 15:24:14

2

请记住,这是异步发生的。在您致电NSURLConnection后,您将downloadedData的尺寸设置为0。

移动:

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

您的来电NSURLConnection之前。

+0

我改变了顺序,请看上面。你是这个意思吗? – solarissf 2013-03-12 00:54:33

+0

我也得到在警告[[NSURLConnection的页头] initWithRequest:请求委托:自我]; ..表达UNUSED – solarissf 2013-03-12 00:55:06

+0

我宁愿要么使用性能或IVAR。你之前使用过这个属性,所以我使用'self.downloadedData'。该NSURLConnection的调用返回一个NSURLConnection的对象,但你没有设置一个,像'NSURLConnection的*连接= [[NSURLConnection的页头] initWithRequest:要求委托:自我]' – 2013-03-12 01:04:32

0

你没有启动的URLConnection。更改[[NSURLConnection alloc] initWithRequest:request delegate:self];这样[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

+0

我把立即开始..什么都没有改变 – solarissf 2013-03-12 01:13:33

+0

这是不正确 - 使用initWithRequest:代表:相当于使用initWithRequest:委托:startImmediately:和传球是最后一个参数。 – rdelmar 2013-03-12 02:05:05