[译]Flutter 特性丰富的音频播放器 just_audio (二) - 平台特定配置

892 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情


本文翻译自: just_audio | Flutter Package (flutter-io.cn)

译时版本:0.9.31


平台特定配置

Android

要使应用能够访问网络上的音频文件,在 AndroidManifest.xml 文件中添加下面的权限:

    <uses-permission android:name="android.permission.INTERNET"/>

如果想要连接非 HTTPS 的 URL ,或者使用依赖代理的特性如Header、缓存或流音频资源,也需要把下面的属性添加到 'applicafion' 元素中:

    <application ... android:usesCleartextTraffic="true">

如果需要访问播放器的 AudioSession ID,可以监听 AudioPlayer.androidAudioSessionIdStream 。 注意无论何时设置了新的 AudioAttributes (音频属性),AudioSession ID 都会改变。

如果在应用中有多个插件使用 ExoPlayer 解码媒体,当这些插件使用 ExoPlayer 的不同版本时,有可能会发生 Duplicate class 错误。 这种情况下,可能需要对每个单独的插件报告 issue 让它们升级到最新版本的 ExoPlayer ,或者自己降级一个或多个应用中的插件直到所有插件的版本都能正常匹配。 一些情况下,某个插件使用了 ExoPlayer API 中的非破坏性变更的话,也可以通过修改应用里的 android/app/build.gradle 文件并插入所需 Exoplayer 版本的依赖来强制所有的插件都使用 ExoPlayer 的相同版本:

dependencies {
    def exoplayer_version = "...specify-version-here...."
    implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version"
    implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version"
    implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version"
    implementation "com.google.android.exoplayer:exoplayer-smoothstreaming:$exoplayer_version"
}

iOS 

使用默认配置,App Store 会检测到你的应用使用了 AVAudioSession API ,它包含一个 microphone API, 然后由于隐私原因,会要求你描述 microphone 在应用中的用途。如果应用确实使用了 microphone ,可以如下编辑 Info.plist 文件添加对用途的描述:

<key>NSMicrophoneUsageDescription</key>
<string>... explain why the app uses the microphone here ...</string>

但是如果应用没有使用 microphone ,可以传递一个编译选项不去编译 任何 microphone 代码,这样 App Store 就不会要求上面的使用描述。要做到这一点,如下编辑  ios/Podfile :

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    
    # ADD THE NEXT SECTION
    target.build_configurations.each do |config|
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
        'AUDIO_SESSION_MICROPHONE=0'
      ]
    end
    
  end
end

如果想要连接非 HTHPS 的 URL ,或者使用依赖代理的特性如 Headers 、缓存或流音频资源,在 Info.plist 文件中添加如下内容:

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

依赖服务器 Header (例, Content-Type 、 Content-Length 和 字节范围) 的 macOS 播放器会要求知道如何解码文件然后在哪里可以报告其时长。文件在 iOS 上依赖其扩展名。

macOS

要使 macOS 应用能够访问网络上的音频文件,在 DebugProfile.entitlements 和 Release.entitlements 文件中添加如下内容:

    <key>com.apple.security.network.client</key>
    <true/>

如果想要连接非 HTHPS 的 URL ,或者使用依赖代理的特性如 Headers 、缓存或流音频资源,在 Info.plist 文件中添加如下内容:

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

依赖服务器 Header (例, Content-Type 、 Content-Length 和 字节范围)的 macOS 播放器会要求知道如何解码文件然后在哪里可以报告其时长。文件在 macOS 上依赖其扩展名。

Windows

可以在 pubspec.yaml 中 just_audio 的旁边添加额外的依赖支持 Windows 。这里有一些可互换的选项:

示例:

dependencies:
  just_audio: any # 改为版本号
  just_audio_windows: any # 改为版本号

对于 Windows 上的实现的 issue ,可以分别在依赖的 github issue 页面提出。

Linux

可以在 pubspec.yaml 中 just_audio 的旁边添加额外的依赖支持 Linux 。这里有一些可互换的选项:

dependencies:
  just_audio: any # 改为版本号
  just_audio_mpv: any # 改为版本号

对于 Linux 上的实现的 issue ,可以分别在依赖的 github issue 页面提出。


开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情