GPH*_*GPH 2 audio-player ios flutter
我正在使用 flutter 插件音频播放器:^0.7.8,以下代码在 Android 中有效,但在 IOS 中无效。我在真实的 ios 设备中运行代码并单击按钮。它假设播放 mp3 文件,但根本没有声音。请帮助解决这个问题。
我已经设置了 info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
这里是从控制台打印出来的:
这里有我的代码:
class _MyHomePageState extends State<MyHomePage> {
AudioPlayer audioPlugin = AudioPlayer();
String mp3Uri;
@override
void initState() {
AudioPlayer.logEnabled = true;
_load();
}
Future<Null> _load() async {
final ByteData data = await rootBundle.load('assets/demo.mp3');
Directory tempDir = await getTemporaryDirectory();
File tempFile = File('${tempDir.path}/demo.mp3');
await tempFile.writeAsBytes(data.buffer.asUint8List(), flush: true);
mp3Uri = tempFile.uri.toString();
print('finished loading, uri=$mp3Uri');
}
void _playSound() {
if (mp3Uri != null) {
audioPlugin.play(mp3Uri, isLocal: true,
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Audio Player Demo Home Page'),
),
body: Center(),
floatingActionButton: FloatingActionButton(
onPressed: _playSound,
tooltip: 'Play',
child: const Icon(Icons.play_arrow),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果要使用本地文件,则必须使用AudioCache.
查看文档,它在底部说:
音频缓存
为了播放本地资源,您必须使用 AudioCache 类。
Flutter 没有提供一种简单的方法来在你的资产上播放音频,但这个类有很大帮助。它实际上将资产复制到设备中的临时文件夹中,然后在该文件夹中作为本地文件播放。
它用作缓存,因为它会跟踪复制的文件,以便您可以毫不拖延地重播。
为了播放音频,这就是我想出的我们需要做的事情:
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
AudioCache audioPlayer = AudioCache();
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override initState(){
super.initState();
audioPlayer.play("Jingle_Bells.mp3");
}
@override Widget build(BuildContext context) {
//This we do not care about
}
}
Run Code Online (Sandbox Code Playgroud)
重要的:
它会自动将“assets/”放在您的路径前面。这意味着如果您想加载assets/Jingle_Bells.mp3,您只需将audioPlayer.play("Jingle_Bells.mp3");. 如果您audioPlayer.play("assets/Jingle_Bells.mp3");改为输入,则实际上会加载 AudioPlayers assets/assets/Jingle_Bells.mp3。
``
| 归档时间: |
|
| 查看次数: |
6814 次 |
| 最近记录: |