如何在不带flutter的mp3扩展的情况下流式传输由http url提供的实时音频?它不是一个远程文件,而是一个类似网络广播的流媒体url。
发布于 2019-12-19 16:40:58
您可以使用package https://pub.dev/packages/url_audio_stream
github https://github.com/tcronis/url_audio_stream
代码片段
AudioStream stream = new AudioStream("https://your_url_goes_here.com");
stream.start();
stream.pause();
stream.resume();
stream.stop();完整的示例代码
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:url_audio_stream/url_audio_stream.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
}
static AudioStream stream = new AudioStream("your url");
Future<void> callAudio(String action) async{
if(action == "start"){
stream.start();
}else if(action == "stop"){
stream.stop();
}else if(action == "pause"){
stream.pause();
}else{
stream.resume();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
new RaisedButton(
child: new Text("Start"),
onPressed: (){
callAudio("start");
},
),
new RaisedButton(
child: new Text("Stop"),
onPressed: (){
callAudio("stop");
},
),
new RaisedButton(
child: new Text("Pause"),
onPressed: (){
callAudio("pause");
},
),
new RaisedButton(
child: new Text("Resume"),
onPressed: (){
callAudio("resume");
},
)
],
)
),
),
);
}
}发布于 2019-12-19 19:58:59
它是有效的,但对于某些url,我需要在AndroidManifest.xml中做一些更改才能使其正常工作。
android:usesCleartextTraffic="true“
发布于 2020-06-29 00:51:44
https://stackoverflow.com/questions/59405813
复制相似问题