gpt4 book ai didi

ios - NSUrlConnection 似乎没有工作,

转载 作者:行者123 更新时间:2023-11-28 20:22:35 24 4
gpt4 key购买 nike

我正在尝试使用已转换的 googletts,但似乎遇到了麻烦。我认为我在结构方面正确地上课了。但我认为 NSURLConnection 没有返回任何有用的数据。有人可以帮我检查它是否有效吗?如果不能,为什么?

在 connectiondidfinishloading 部分我正在检查下载长度NSLog(@"hmmmm %lu", (unsigned long)[下载数据长度])返回 0。这就是为什么我认为它不起作用。

目标是让应用说出下载的内容

谢谢!!!

我的 View Controller 中只有一个按钮可以启动所有内容

#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

最佳答案

请记住,这是异步发生的。在调用 NSURLConnection 之后,您将 downloadedData 的大小设置为 0。

移动:

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

在调用 NSURLConnection 之前。

关于ios - NSUrlConnection 似乎没有工作,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15351275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com