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

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

问题描述

我正在使用Flutter插件音频播放器:^ 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:

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

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

看文档,它在底部说:

AudioCache

AudioCache

要播放本地资产,必须使用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/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天全站免登陆