尝试向speechrecognizer

时间:2016-03-07 19:10:51

标签: amazon alexa-voice-service

我正在尝试创建一个AVS应用程序,它将通过内置于swift的IOS应用程序与AVS进行交互。我已经使用amazon处理登录,通过“AIMobileLib.getAccessTokenForScopes([”alexa:all“],withOverrideParams:nil,delegate:self)”获取访问令牌,并使用该令牌向https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize发出POST请求。使用以下代码来执行此操作

创建audioDat - :

audioData = NSData(contentsOfURL: fileURL)
print("the sound file is at \(fileURL.path!)")
let soundFileURL = NSURL(fileURLWithPath: fileURL.path!)
let recordSettings =
[AVFormatIDKey: NSNumber(unsignedInt:kAudioFormatLinearPCM),
AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 1,
AVSampleRateKey: 16000.0,
AVLinearPCMIsBigEndianKey: "YES",
AVLinearPCMIsFloatKey: "YES"]

HTTP put request

func postRecording() {
//urlString += "\(self.accessToken!)"
let finalURL = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

let request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: finalURL)!)
//request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData
request.HTTPShouldHandleCookies = false
request.timeoutInterval = 60
request.HTTPMethod = "POST"
print("\nAccessToken:\(self.accessToken!)")
request.addValue("access-alexa-na.amazon.com", forHTTPHeaderField: "Host")
request.addValue("Bearer \(self.accessToken!)", forHTTPHeaderField: "Authorization")
let boundry: String = "BOUNDARY1234"
let contentType: String = "multipart/form-data; boundary=\(boundry)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
request.addValue("access-alexa-na.amazon.com", forHTTPHeaderField: "Host")
let httpBody: NSMutableData = NSMutableData.init()
//PG: JSON multipart header
httpBody.appendData("--\(boundry)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

httpBody.appendData("Content-Disposition: form-data; name=\"request\"".dataUsingEncoding(NSUTF8StringEncoding)!)
httpBody.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
httpBody.appendData("Content-Type: application/json; charset=UTF-8".dataUsingEncoding(NSUTF8StringEncoding)!)
httpBody.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
//PG: JSON multipart body
httpBody.appendData(self.createMetadata()!.dataUsingEncoding(NSUTF8StringEncoding)!)
httpBody.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
//PG: JSON Audio multipart header
httpBody.appendData("--\(boundry)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

httpBody.appendData("Content-Disposition: form-data; name=\"audio\"".dataUsingEncoding(NSUTF8StringEncoding)!)
httpBody.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
httpBody.appendData("Content-Type: audio/L16; rate=16000; channels=1".dataUsingEncoding(NSUTF8StringEncoding)!)
//PG: Audio multipart body
// httpBody.appendData(self.audioData!)
httpBody.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
//PG: Terminating boundary term
httpBody.appendData("--\(boundry)--".dataUsingEncoding(NSUTF8StringEncoding)!)
//PG: POST request to AMZN URL
var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session: NSURLSession = NSURLSession(configuration: config)

request.HTTPBody = httpBody
do{
let string1 = NSString(data: httpBody, encoding: NSUTF8StringEncoding)
print(string1!)
} catch {
print("could not write httpbody")
}

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error has occurred in http request : \(error)")
return
}
print("response : \(response?.description)")
var parseError: NSError?
do{
let responseObject: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
if let responseDictionary = responseObject as? NSDictionary {
print(responseDictionary)
} else {
print("response is not a dictionary")
}
} catch {
print("error has occurred in http request")
}
}
task.resume()
}

并得到以下回复 - :

response : Optional("<NSHTTPURLResponse: 0x7f8c28c14010> { URL: https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize } { status code: 400, headers {\n Connection = \"keep-alive\";\n \"Content-Encoding\" = gzip;\n \"Content-Length\" = 185;\n \"Content-Type\" = \"application/json\";\n Date = \"Mon, 07 Mar 2016 16:34:37 GMT\";\n Server = Server;\n Vary = \"Accept-Encoding,User-Agent\";\n \"x-amzn-ErrorType\" = \"BadRequestException:https://developer.amazon.com/edw/home.html\";\n \"x-amzn-RequestId\" = \"7baced67-e482-11e5-ad81-0dd2128b4b85\";\n} }")
{
error = {
code = "com.amazon.alexahttpproxy.exceptions.BadRequestException";
message = "No content to map due to end-of-input\n at [Source: [LineReaderInputStreamAdaptor: [pos: 0][limit: 0][]]; line: 1, column: 1]";
};

}

0 个答案:

没有答案