颤振音频播放器播放声音在 IOS 中不起作用
flutter audio player play sound not working in IOS
我正在使用 flutter 插件 audioplayers: ^0.7.8, 下面的代码在 Android 中有效,但在 IOS 中无效。我 运行 真实 ios 设备中的代码并单击按钮。它假设播放 mp3 文件,但根本没有声音。请帮助解决这个问题。
我已经设置了 info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这里是从控制台打印出来的:
- flutter: 加载完成,uri=file:///var/mobile/Containers/Data/Application/E3A576E2-0F21-44CF-AF99-319D539767D0/Library/Caches/demo.mp3
- 正在将文件同步到设备iPhone...
- flutter: _platformCallHandler 调用 audio.onCurrentPosition {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, 值: 0}
- flutter: _platformCallHandler 调用 audio.onError {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, 值: AVPlayerItemStatus.failed}
这里是我的代码:
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),
),
);
}
}
如果要使用本地文件,则必须使用AudioCache
。
查看文档,它在底部说:
音频缓存
In order to play Local Assets, you must use the AudioCache class.
Flutter does not provide an easy way to play audio on your assets, but this class helps a lot. It actually copies the asset to a temporary folder in the device, where it is then played as a Local File.
It works as a cache because it keep track of the copied files so that you can replay then without delay.
要播放音频,这是我认为我们需要做的事情:
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
}
}
重要提示:
它会自动将 'assets/' 放在您的路径前面。这意味着如果您想加载 assets/Jingle_Bells.mp3
,您只需输入 audioPlayer.play("Jingle_Bells.mp3");
。如果您改为输入 audioPlayer.play("assets/Jingle_Bells.mp3");
,AudioPlayers 实际上会加载 assets/assets/Jingle_Bells.mp3
。
``
我正在使用 flutter 插件 audioplayers: ^0.7.8, 下面的代码在 Android 中有效,但在 IOS 中无效。我 运行 真实 ios 设备中的代码并单击按钮。它假设播放 mp3 文件,但根本没有声音。请帮助解决这个问题。
我已经设置了 info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这里是从控制台打印出来的:
- flutter: 加载完成,uri=file:///var/mobile/Containers/Data/Application/E3A576E2-0F21-44CF-AF99-319D539767D0/Library/Caches/demo.mp3
- 正在将文件同步到设备iPhone...
- flutter: _platformCallHandler 调用 audio.onCurrentPosition {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, 值: 0}
- flutter: _platformCallHandler 调用 audio.onError {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, 值: AVPlayerItemStatus.failed}
这里是我的代码:
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),
),
);
}
}
如果要使用本地文件,则必须使用AudioCache
。
查看文档,它在底部说:
音频缓存
In order to play Local Assets, you must use the AudioCache class.
Flutter does not provide an easy way to play audio on your assets, but this class helps a lot. It actually copies the asset to a temporary folder in the device, where it is then played as a Local File.
It works as a cache because it keep track of the copied files so that you can replay then without delay.
要播放音频,这是我认为我们需要做的事情:
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
}
}
重要提示:
它会自动将 'assets/' 放在您的路径前面。这意味着如果您想加载 assets/Jingle_Bells.mp3
,您只需输入 audioPlayer.play("Jingle_Bells.mp3");
。如果您改为输入 audioPlayer.play("assets/Jingle_Bells.mp3");
,AudioPlayers 实际上会加载 assets/assets/Jingle_Bells.mp3
。
``