Google Speech API no response from swift code, but objective-c code works

311 views Asked by At

I am trying to take an audio file url (that plays with a player) and get it transcribed by Google. However, for some reason, I run into the issue of getting an empty response from google with a 200 response code. I have some code in obj-c from Google that works, and I'm having trouble figuring out what the difference is. Any ideas?

Working obj-c code:

NSString *service = @"https://speech.googleapis.com/v1/speech:recognize";
service = [service stringByAppendingString:@"?key="];
service = [service stringByAppendingString:API_KEY];

NSData *audioData = [NSData dataWithContentsOfFile:[self soundFilePath]];
NSDictionary *configRequest = @{@"encoding":@"LINEAR16",
                                @"sampleRateHertz":@(SAMPLE_RATE),
                                @"languageCode":@"en-US",
                                @"maxAlternatives":@30};
NSDictionary *audioRequest = @{@"content":[audioData     base64EncodedStringWithOptions:0]};
NSDictionary *requestDictionary = @{@"config":configRequest,
                                    @"audio":audioRequest};
NSError *error;
NSData *requestData = [NSJSONSerialization     dataWithJSONObject:requestDictionary
                                                      options:0
                                                        error:&    error];

NSString *path = service;
NSURL *URL = [NSURL URLWithString:path];
NSMutableURLRequest *request = [NSMutableURLRequest     requestWithURL:URL];
// if your API key has a bundle ID restriction, specify the     bundle ID like this:
[request addValue:[[NSBundle mainBundle] bundleIdentifier]     forHTTPHeaderField:@"X-Ios-Bundle-Identifier"];
NSString *contentType = @"application/json";
[request addValue:contentType     forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
  NSLog(@"RESULT: %@", request.description);
NSURLSessionTask *task =
[[NSURLSession sharedSession]
 dataTaskWithRequest:request
 completionHandler:
 ^(NSData *data, NSURLResponse *response, NSError *error) {
   dispatch_async(dispatch_get_main_queue(),
                  ^{
                    NSString *stringResult = [[NSString alloc]     initWithData:data     encoding:NSUTF8StringEncoding];
                    _textView.text = stringResult;
                    NSLog(@"RESULT: %@", stringResult);
                  });
 }];
[task resume];    

Swift code:

func transcribeFile(url: URL) {
    var audioData: Data?
    do {
        audioData = try Data(contentsOf: url)
    } catch {
        print("couldn't transcribe")
    }
    if audioData == nil {
        return
    }
    sendRequestRequest(audioData: audioData!)


}

func sendRequestRequest(audioData: Data) {
    /**
     Request
     post https://speech.googleapis.com/v1/speech:recognize
     */

    // Add Headers
    let headers = [
        "X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier!,
        "Content-Type":"application/json",
        ]

    // JSON Body
    let body: [String : Any] = [
        "audio": [
            "content": audioData.base64EncodedString()
        ],
        "config": [
            "encoding": "LINEAR16",
            "sampleRateHertz": 16000,
            "languageCode": "en-US",
            "maxAlternatives": 30
        ]
    ]

    // Fetch Request
    Alamofire.request("https://speech.googleapis.com/v1/speech:recognize?key=APIKEY", method: .post, parameters: body, encoding: JSONEncoding.default, headers: headers)
        .validate(statusCode: 200..<300)
        .responseJSON { response in
            if (response.result.error == nil) {
                debugPrint("HTTP Response Body: \(response.data)")
            }
            else {
                debugPrint("HTTP Request failed: \(response.result.error)")
            }
    }
}
0

There are 0 answers