我試圖使用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
是否所有工作日誌? – rdelmar 2013-03-12 02:07:46