颤动:将原始文件发送到谷歌语音API

时间:2018-03-13 14:27:39

标签: audio dart speech-recognition flutter

我试图将原始文件发送到google speech api,但我不知道如何加载原始文件以便将其放入RecognitionAudio json,任何想法?

这是我现在的代码:

import 'package:flutter/services.dart';
import 'package:googleapis/speech/v1.dart';
import 'package:googleapis_auth/auth_io.dart';

void main() {

  clientViaServiceAccount(_credentials, _SCOPES).then((http_client) async {
    var speech = new SpeechApi(http_client);
    RecognizeRequest r = new RecognizeRequest();

    String content = await rootBundle.loadString('assets/res/audio.raw');

RecognitionAudio audio = new RecognitionAudio.fromJson({ 'content': content});
    r.audio = audio;
    RecognitionConfig config = new RecognitionConfig.fromJson({
      'languageCode' : 'en-US',
      'encoding' : 'LINEAR16',
      'sampleRateHertz' : 44100,
    });
    r.config = config;
    speech.speech.recognize(r).then((results) {
      for (var result in results.results) {
        print(result.toString());
      }
    });

  });
}

并在pubspec.yaml上添加了

assets:
    - assets/res/audio.raw

这是控制台中的错误 E / flutter(3740):[错误:flutter / runtime / dart_init.cc(167)]隔离

(169749064) 'flutter_assets:_spawn()' exited with an error
E/flutter ( 3740): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 3740): FormatException: Bad UTF-8 encoding 0xff (at offset 1)
E/flutter ( 3740): #0      _Utf8Decoder.convert (dart:convert/utf.dart:574)
E/flutter ( 3740): #1      Utf8Decoder.convert (dart:convert/utf.dart:350)
E/flutter ( 3740): #2      Utf8Codec.decode (dart:convert/utf.dart:72)
E/flutter ( 3740): #3      CachingAssetBundle._utf8decode (package:flutter/src/services/asset_bundle.dart:172)
E/flutter ( 3740): #4      _spawn.<anonymous closure> (package:flutter/src/foundation/isolates.dart:94)
E/flutter ( 3740): #5      Timeline.timeSync (dart:developer/timeline.dart:168)
E/flutter ( 3740): #6      _spawn (package:flutter/src/foundation/isolates.dart:91)
E/flutter ( 3740): #7      _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:282)
E/flutter ( 3740): #8      _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)
E/flutter ( 3740): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 3740): DetailedApiRequestError(status: 400, message: RecognitionAudio not set.)
E/flutter ( 3740): #0      _validateResponse.<anonymous closure> (package:_discoveryapis_commons/src/clients.dart:880)
E/flutter ( 3740): #1      _RootZone.runUnary (dart:async/zone.dart:1381)
E/flutter ( 3740): #2      _FutureListener.handleValue (dart:async/future_impl.dart:129)
E/flutter ( 3740): #3      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633)
E/flutter ( 3740): #4      _Future._propagateToListeners (dart:async/future_impl.dart:662)
E/flutter ( 3740): #5      _Future._complete (dart:async/future_impl.dart:467)
E/flutter ( 3740): #6      _cancelAndValue (dart:async/stream_pipe.dart:63)
E/flutter ( 3740): #7      Stream.first.<anonymous closure> (dart:async/stream.dart:1158)
E/flutter ( 3740): #8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1316)
E/flutter ( 3740): #9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:330)
E/flutter ( 3740): #10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:257)
E/flutter ( 3740): #11     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68)
E/flutter ( 3740): #12     _EventSinkWrapper.add (dart:async/stream_transformers.dart:15)
E/flutter ( 3740): #13     _JsonStringDecoderSink.close (dart:convert-patch/dart:convert/convert_patch.dart:1459)
E/flutter ( 3740): #14     _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:94)
E/flutter ( 3740): #15     _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:141)
E/flutter ( 3740): #16     _RootZone.runGuarded (dart:async/zone.dart:1304)
E/flutter ( 3740): #17     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:383)
E/flutter ( 3740): #18     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:393)
E/flutter ( 3740): #19     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:277)
E/flutter ( 3740): #20     _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:96)
E/flutter ( 3740): #21     _EventSinkWrapper.close (dart:async/stream_transformers.dart:23)
E/flutter ( 3740): #22     _StringAdapterSink.close (dart:convert/string_conversion.dart:281)
E/flutter ( 3740): #23     _Utf8ConversionSink.close (dart:convert/string_conversion.dart:336)
E/flutter ( 3740): #24     _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:94)
E/flutter ( 3740): #25     _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:141)
E/flutter ( 3740): #26     _RootZone.runGuarded (dart:async/zone.dart:1304)
E/flutter ( 3740): #27     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:383)
E/flutter ( 3740): #28     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:393)
E/flutter ( 3740): #29     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:277)
E/flutter ( 3740): #30     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:106)
E/flutter ( 3740): #31     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:172)
E/flutter ( 3740): #32     _RootZone.runGuarded (dart:async/zone.dart:1304)
E/flutter ( 3740): #33     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:383)
E/flutter ( 3740): #34     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:393)
E/flutter ( 3740): #35     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:277)
E/flutter ( 3740): #36     _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:96)
E/flutter ( 3740): #37     _EventSinkWrapper.close (dart:async/stream_transformers.dart:23)
E/flutter ( 3740): #38     _ByteAdapterSink.close (dart:convert/byte_conversion.dart:70)
E/flutter ( 3740): #39     _FilterSink.close (dart:io/data_transformer.dart:604)
E/flutter ( 3740): #40     _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:94)
E/flutter ( 3740): #41     _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:141)
E/flutter ( 3740): #42     _RootZone.runGuarded (dart:async/zone.dart:1304)
E/flutter ( 3740): #43     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:383)
E/flutter ( 3740): #44     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:393)
E/flutter ( 3740): #45     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:277)
E/flutter ( 3740): #46     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:106)
E/flutter ( 3740): #47     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:172)
E/flutter ( 3740): #48     _RootZone.runGuarded (dart:async/zone.dart:1304)
E/flutter ( 3740): #49     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:383)
E/flutter ( 3740): #50     _BufferingStreamSubscrip

我猜我加载内容的方式是错误的

0 个答案:

没有答案