0

変換されたgooglettsを使用しようとしていますが、問題が発生しているようです。構造的には正しくクラスを作ったと思います。しかし、NSURLConnectionが有用なデータを返しているとは思いません。誰かが私がそれが機能しているかどうかを確認するのを手伝ってくれますか?そうでない場合はその理由を教えてください。

connectiondidfinishloadingセクションで、0を返すダウンロード長NSLog(@ "hmmmm%lu"、(unsigned long)[downloadedData length])をチェックしています。そのため、機能していないと思います。

目標は、ダウンロードしたものをアプリに話させることです

ありがとう!!!

ビューコントローラにすべてを起動するボタンがあります

#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
4

3 に答える 3

2

これは非同期で発生していることに注意してください。を呼び出した後、サイズdownloadedDataを0に設定しますNSURLConnection

動く:

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

に電話する前にNSURLConnection

于 2013-03-12T00:45:23.560 に答える
1

NSURLConnectionメソッドに問題はありません。検索文字列が間違っています。そのはず:

NSString *search = [NSString stringWithFormat:@"http://translate.google.com/translate_tts?tl=en&q=%@",searchString];
于 2013-03-12T02:25:51.767 に答える
0

URLConnectionを開始していません。[[NSURLConnection alloc] initWithRequest:request delegate:self];このように変更します[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

于 2013-03-12T01:05:39.370 に答える