目录
前言
- 记录使用audioplayers包时出现的问题,测试环境主要是 安卓11
问题
无法读取assets文件
- 这里是希望读取app自带文件 “file/music.mp3” 作为音乐来播放
- 那么首先就需要确保你是否已经在 pubspec.yaml 中加入了这个文件:
![](https://blog.coolight.cool/wp-content/uploads/2022/11/image.png)
- 但仍然报错,报错代码行:audioPlayer.setSource(AssetSource("file/music.mp3"));
E/flutter (32073): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: assets/file/music.mp3
E/flutter (32073): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:258:7)
E/flutter (32073): <asynchronous suspension>
E/flutter (32073): #1 AudioCache.fetchToMemory (package:audioplayers/src/audio_cache.dart:75:22)
E/flutter (32073): <asynchronous suspension>
E/flutter (32073): #2 AudioCache.load (package:audioplayers/src/audio_cache.dart:101:31)
E/flutter (32073): <asynchronous suspension>
E/flutter (32073): #3 AudioPlayer.setSourceAsset (package:audioplayers/src/audioplayer.dart:237:17)
- 可以看到开头第一句报错,“Unable to load asset: assets/file/music.mp3”,但我们指定的文件是"file/music.mp3",即audioPlayer有一个默认路径前缀"assets/"。
- 解决办法:通过 audioPlayer.audioCache.prefix = ''; 去掉它的默认前缀,然后再调用 audioPlayer.setSource(AssetSource("file/music.mp3")); 读取mp3文件即可。
本地资源/网络资源都无法播放
- 报错信息
......
D/MediaCodecList(32073): codecHandlesFormat: no format, so no extra checks
D/CCodec (32073): allocate(c2.android.mp3.decoder)
I/CCodec (32073): setting up 'default' as default (vendor) store
I/CCodec (32073): Created component [c2.android.mp3.decoder]
D/CCodecConfig(32073): read media type: audio/mpeg
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: algo.buffers.max-count.values
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: output.subscribed-indices.values
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: input.buffers.allocator-ids.values
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: output.buffers.allocator-ids.values
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: algo.buffers.allocator-ids.values
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: output.buffers.pool-ids.values
D/ReflectedParamUpdater(32073): extent() != 1 for single value type: algo.buffers.pool-ids.values
......
D/CCodecBufferChannel(32073): [c2.android.mp3.decoder#231] MediaCodec discarded an unknown buffer
I/hw-BpHwBinder(32073): onLastStrongRef automatically unlinking death recipients
......
- 这个问题可能是因为你使用了lowLatency(低延迟)播放模式,即使用了 audioPlayer.setPlayerMode(PlayerMode.lowLatency);
- audioplayers有两种播放模式PlayerMode:
- mediaPlayer:最常用的播放模式,对时延要求不高的声音输出采用该模式,边加载边播放。
- lowLatency:低延迟播放模式,用于按键音、游戏背景音等对时延要求高场景,一次加载完数据。
- 具体差别我还不了解,但实践中,无论在虚拟机还是真机,一使用lowLatency就无法播放,因此可以试试切换为mediaPlayer使用:
- audioPlayer.setPlayerMode(PlayerMode.mediaPlayer);
网络资源加载失败
- 使用lowLatency播放模式时的报错内容:
- 之所以强调lowLatency,是因为对于同一个问题,mediaPlayer 和 lowLatency 的报错信息是不同的,而且我觉得 lowLatency 的比较清晰。
......
Unexpected error!
D/AudioPlayers(31616): java.io.IOException: Cleartext HTTP traffic to {xxx.域名} not permitted
......
- 那么百度这一句报错后,很多 安卓 原生的文章提到,在高版本的安卓中不允许不加密的明文链接,说人话就是不让你用http链接,需要走https
- 解决办法:
- 方法1:明显的当然就是换https链接了。
- 方法2:
- 修改android/app/src/main/AndriodManifest.xml文件:
![](https://blog.coolight.cool/wp-content/uploads/2022/11/image-1.png)
- -
- 在application标签的属性中添加:android:usesCleartextTraffic="true"
- 注意是 <application 这里></application> ,而不是 <application>不是这里</application>
![](https://blog.coolight.cool/wp-content/uploads/2022/11/image-2.png)
- 方法3:同样的修改 AndroidManifest.xml 文件
- 修改/添加 <manifest> 标签的属性 android:targetSandboxVersion="1"
![](https://blog.coolight.cool/wp-content/uploads/2022/11/image-4.png)
where can you buy doxycycline: doxycycline 50 mg tablets price – doxycycline 600 mg
cipro for sale: cipro pharmacy – ciprofloxacin generic
amoxicillin 500 mg tablets: generic amoxil 500 mg – how much is amoxicillin
buy cipro online canada: п»їcipro generic – buy cipro online canada
clomid cost: clomid generic – how can i get generic clomid
where to buy cipro online: ciprofloxacin 500 mg tablet price – ciprofloxacin generic
order doxycycline online: doxycycline for sale online uk – doxycycline compare prices
buy generic ciprofloxacin: where to buy cipro online – buy cipro online canada
how can i get clomid prices: where to buy generic clomid tablets – can i order generic clomid for sale
cipro online no prescription in the usa: antibiotics cipro – antibiotics cipro
can i order generic clomid without rx: how can i get generic clomid without insurance – can you buy clomid no prescription
buy cipro without rx: buy generic ciprofloxacin – buy cipro cheap
buy doxycycline 200 mg: doxycycline online australia – doxycycline 100mg otc
http://clomiddelivery.pro/# can you buy clomid without prescription
https://clomiddelivery.pro/# can i get clomid pills
buy cipro online canada: cipro 500mg best prices – ciprofloxacin generic
https://ciprodelivery.pro/# cipro ciprofloxacin
http://doxycyclinedelivery.pro/# doxycycline prescription australia
buy cipro online: buy cipro online canada – buy cipro online
amoxicillin canada price: amoxicillin 500 mg without prescription – amoxicillin 775 mg
http://amoxildelivery.pro/# amoxicillin price without insurance
Paxlovid buy online: paxlovid buy – paxlovid pill
https://clomiddelivery.pro/# generic clomid online
clomid buy: can i purchase generic clomid – how to buy clomid for sale
http://clomiddelivery.pro/# cost cheap clomid without prescription
cost of clomid without insurance: cost cheap clomid price – can i buy cheap clomid price
http://doxycyclinedelivery.pro/# how to get doxycycline 100mg
paxlovid cost without insurance: paxlovid for sale – paxlovid buy
paxlovid pill: paxlovid pharmacy – Paxlovid over the counter
https://amoxildelivery.pro/# amoxicillin 500mg prescription
can i buy generic clomid: where buy cheap clomid pill – how can i get generic clomid without insurance
http://ciprodelivery.pro/# where can i buy cipro online
https://amoxildelivery.pro/# buy amoxicillin online no prescription
https://amoxildelivery.pro/# amoxicillin 875 mg tablet
how to buy clomid online: cost clomid without insurance – where to buy clomid online
buy cipro online canada: where to buy cipro online – buy cipro without rx
https://clomiddelivery.pro/# where can i buy cheap clomid without rx