ios - flutter 音频播放器播放声音在 IOS 中不起作用

标签 ios flutter audio-player

我正在使用 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

``

关于ios - flutter 音频播放器播放声音在 IOS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800637/

相关文章:

iOS - 未确定推送通知

ios - 为什么点击 UICollectionViewCell 上的文本会消失?

java - 修复我的 Android 音乐播放器上的搜索栏

swift - MPMusicPlayerController setQueueWithStoreIDs 播放索引?

android - 如何同步播放多首歌曲?

ios - Xcode附加到进程不显示NSLog

iOS Facebook SDK 安装 - 应用事件

flutter - 如何在 flutter 中播放来自 URL 的音频?

flutter - Flutter 是否有在移动和 Web 应用程序之间共享代码的好方法?

flutter - 如何获得泛型类型?