簡體   English   中英

在我的 flutter 應用程序中使用音頻播放器 package 時遇到問題

[英]Trouble using the audioplayers package in my flutter app

我上周從 flutter 開始,我在音頻播放器 package 上遇到了這個問題。 我正在 Udemy 使用 Dart 進行 Flutter 開發訓練營。 我幾乎關注了視頻中的所有內容,但我收到了一條我無法解決的巨大錯誤消息。

這是我的代碼:

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
void main() => runApp(XylophoneApp());

class XylophoneApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.lightBlue,
        body: SafeArea(
          child: Container(
            child: Center(
              child: TextButton(
                onPressed: (){
                  final player = AudioCache();
                  player.play('assets/note1');
                },
                child: Text(
                  'click me'
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

這是我收到的錯誤消息:正在運行 Gradle 任務“assembleDebug”... e:在依賴項中發現了不兼容的類。 從類路徑中刪除它們或使用“-Xskip-metadata-version-check”來抑制錯誤 e: C:/Users/victo/.gradle/caches/transforms-2/files-2.1/24fa3aa8d2270e5eb067bbe36e9b7563/jetified-kotlin-std-lib 1.5.10.jar./META-INF/kotlin-stdlib:kotlin_module。 使用不兼容版本的 Kotlin 編譯模塊。 其元數據的二進制版本是 1.5,1。 預期版本是 1.1.15...(消息比這大得多)

  • 出了什么問題:任務“:audioplayers:compileDebugKotlin”執行失敗。

編譯錯誤。 有關詳細信息,請參閱日志

  • 嘗試:使用 --stacktrace 選項運行以獲取堆棧跟蹤。 使用 --info 或 --debug 選項運行以獲得更多日志 output。 運行 --scan 以獲得完整的見解。

  • https://help.gradle.org獲得更多幫助

21 秒內構建失敗異常:Gradle 任務 assembleDebug 失敗,退出代碼為 1

我看過這條消息 The binary version of its metadata is 1.5.1, expected version is 1.1.15 ,但我不知道如何處理它或如何解決它。

這是我的 pubspec.yaml:

name: xylophone
description: A new Flutter application.

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  audioplayers: ^0.19.1
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter


flutter:

  uses-material-design: true

  assets:
    - assets/

在 gradle 文件中,轉到 build.gradle(項目:YourApp)。 然后,更改以下代碼(在 buildscript 中):

ext.kotlin_version = '1.3.50'

ext.kotlin_version = '1.4.32'

或者最新版本的 Kotlin 可用,並確保在 Android Studio 上更新 Kotlin 版本

按照說明操作后,您的錯誤將得到解決。

您可以查看錯誤日志Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15 Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15但嘗試1.1.15 1.3.50版本卸載應用程序並運行flutter clean並再次運行

嘗試在 build.gradle 中降級到較低的 kotlin 版本

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

}

如果萬一不起作用,也請參考此答案

遷移指南

依賴項:音頻播放器:^0.xx

依賴項:音頻播放器:^1.0.1

https://github.com/bluefireteam/audioplayers/blob/main/migration_guide.md

試試這個[1]:https://i.stack.imgur.com/jR5PG.png

AudioCache 已死,源源不斷主要變化之一是我希望“殺死”AudioCache API,因為它給用戶造成了巨大的混亂(盡管我們盡最大努力記錄一切)。

我們仍然有 AudioCache 類,但它的 API 專門用於將資產文件轉換為本地文件、緩存它們並提供路徑。 然而,最終用戶通常不需要使用它,因為 AudioPlayer 本身現在能夠播放來自任何來源的音頻。

什么是源? 這是一個密封類,可以是以下之一:

UrlSource:從 Internet 的遠程 URL 獲取音頻 DeviceFileSource:訪問用戶設備中的文件,可能由文件選擇器選擇 AssetSource:播放與您的應用程序捆綁的資產,通常在資產目錄中 BytesSource(僅限某些平台):直接傳遞音頻的字節(從任何地方讀取)。 如果您使用 AssetSource,AudioPlayer 將自動使用其 AudioCache 實例(如果未更改,則默認為全局緩存)。 這統一了 AudioPlayer 下的所有播放 API,並為大多數用戶完全刪除了 AudioCache 細節。

如果您在我的案例^1.0.1中使用最新版本的音頻播放器,那么您的代碼應該看起來像這樣

onPressed: () async {
                final player = AudioPlayer();
                await player.play(AssetSource('note1.wav'));
              },

祝你在學習中一切順利

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM