颤振音频播放器播放声音在 IOS 中不起作用 [英] flutter audio player play sound not working in IOS

查看:29
本文介绍了颤振音频播放器播放声音在 IOS 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 flutter 插件 audioplayers: ^0.7.8, 以下代码在 Android 中有效,但在 IOS 中无效.我在真实的 ios 设备中运行代码并单击按钮.它假设播放 mp3 文件,但根本没有声音.请帮助解决这个问题.

I am using flutter plugin audioplayers: ^0.7.8, the below code is working in Android but not working in IOS. I run the code in real ios device and click the button. It suppose the play the mp3 file, but no sound at all. Please help to solve this issue.

我已经设置了 info.plist

I already set up the info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

这里是从控制台打印出来的:

Here with the print out from the console:

  • flutter:加载完毕,uri=file:///var/mobile/Containers/Data/Application/E3A576E2-0F21-44CF-AF99-319D539767D0/Library/Caches/demo.mp3
  • 正在将文件同步到 iPhone 设备...
  • flutter: _platformCallHandler call audio.onCurrentPosition {playerId: 273e1d27-b6e8-4516-bb3f-967a41dff308, value: 0}
  • flutter: _platformCallHandler call 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),
      ),
    );
  }
}

推荐答案

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

If you want to use a local file, you have to use AudioCache.

查看文档,它在底部说:

Looking at the documentation, it says at the bottom:

音频缓存

为了播放本地资源,您必须使用 AudioCache 类.

Flutter 没有提供一种简单的方法来在你的资产上播放音频,但这个类有很大帮助.它实际上将资产复制到设备中的一个临时文件夹,然后在那里作为本地文件播放.

它用作缓存,因为它跟踪复制的文件,以便您可以毫不拖延地重播.

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.

为了播放音频,这就是我想出的我们需要做的:

To get audio to play, this is what I figured out what we need to do:

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.

It automatically places 'assets/' in front of your path. This means you if you wanted to load assets/Jingle_Bells.mp3, you would simply put audioPlayer.play("Jingle_Bells.mp3");. If you entered audioPlayer.play("assets/Jingle_Bells.mp3"); instead, AudioPlayers would actually load assets/assets/Jingle_Bells.mp3 instead.

``

这篇关于颤振音频播放器播放声音在 IOS 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆