Open uniquejava opened 4 years ago
cyper实战:
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:web_socket_channel/io.dart';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() => runApp(MyApp());
final instanceId = 'f84c9d83-9a35-4370-a1d1-xxxx';
final location = 'jp-tok';
final wssUrl =
'wss://api.$location.speech-to-text.watson.cloud.ibm.com/instances/$instanceId/v1/recognize';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'WebSocket Demo';
return MaterialApp(
title: title,
home: MyHomePage(
title: title,
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, @required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = TextEditingController();
WebSocketChannel _channel;
FlutterSound flutterSound = new FlutterSound();
void connect() {
print('HTTP_PROXY: ${Platform.environment['HTTP_PROXY']}');
print('NO_PROXY: ${Platform.environment['NO_PROXY']}');
var clientId = 'apiKey';
var clientSecret = '81p9-xxxxxxxxxxxxxx';
var basicAuth =
'Basic ' + base64Encode(utf8.encode('$clientId:$clientSecret'));
_channel = IOWebSocketChannel.connect(
wssUrl,
headers: {'Authorization': basicAuth},
);
_channel.stream.listen(
(dynamic message) {
debugPrint('message $message');
},
onDone: () {
debugPrint('ws channel closed');
},
onError: (error) {
debugPrint('ws error $error');
},
);
}
void record() {}
void stop() {}
void disconnect() {
_channel.sink.close();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Form(
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Send a message'),
),
),
if (_channel != null)
StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Text(snapshot.hasData ? '${snapshot.data}' : ''),
);
},
),
RaisedButton(
child: Text('connect'),
onPressed: connect,
),
RaisedButton(
child: Text('record'),
onPressed: record,
),
RaisedButton(
child: Text('stop'),
onPressed: stop,
),
RaisedButton(
child: Text('disconnect'),
onPressed: disconnect,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
_channel.sink.add(_controller.text);
}
}
@override
void dispose() {
if (_channel != null) {
if (_channel.sink != null) {
_channel.sink.close();
}
}
super.dispose();
}
}
流程:
var IAM_access_token = '{access_token}';
var wsURI = '{ws_url}/v1/recognize'
+ '?access_token=' + IAM_access_token
+ '&model=en-US_BroadbandModel';
var websocket = new WebSocket(wsURI);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
function onOpen(evt) {
var message = {
action: 'start',
keywords: ['colorado', 'tornado', 'tornadoes'],
keywords_threshold: 0.5,
max-alternatives: 3
};
websocket.send(JSON.stringify(message));
// Prepare and send the audio file.
websocket.send(blob);
websocket.send(JSON.stringify({action: 'stop'}));
}
function onClose(evt) {
console.log(evt.data);
}
function onMessage(evt) {
console.log(evt.data);
}
function onError(evt) {
console.log(evt.data);
}
<script src="scripts/watson-speech/dist/watson-speech.js"></script>
fetch('/api/speech-to-text/token')
.then(function(response) {
return response.json();
}).then(function (token) {
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone(Object.assign(token, {
objectMode: false
}));
stream.setEncoding('utf8'); // get text instead of Buffers for on data events
stream.on('data', function(data) {
console.log(data);
});
stream.on('error', function(err) {
console.log(err);
});
document.querySelector('#stop').onclick = stream.stop.bind(stream);
}).catch(function(error) {
console.log(error);
});
https://speech-to-text-demo.mybluemix.net/
https://cloud.ibm.com/apidocs/speech-to-text/speech-to-text#websocket_methods
https://cloud.ibm.com/docs/speech-to-text?topic=speech-to-text-websockets#websockets
https://flutter.dev/docs/cookbook/networking/web-sockets
https://www.didierboelens.com/2018/06/web-sockets-build-a-real-time-game/
插件