颤动的音频播放器播放声音在IOS中无效

问题描述 投票:0回答:1

我正在使用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,value:0}
  • flutter:_platformCallHandler调用audio.onError {playerId:273e1d27-b6e8-4516-bb3f-967a41dff308,value: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),
      ),
    );
  }
}
ios flutter audio-player
1个回答
2
投票

如果要使用本地文件,则必须使用AudioCache

查看文档,它在底部说:

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
  }
}

重要:

它会自动将“assets /”放在您的路径前面。这意味着如果你想加载assets/Jingle_Bells.mp3,你只需要放audioPlayer.play("Jingle_Bells.mp3");。如果你改为输入audioPlayer.play("assets/Jingle_Bells.mp3");,AudioPlayers实际上会加载assets/assets/Jingle_Bells.mp3

``

© www.soinside.com 2019 - 2024. All rights reserved.