解决找不到字体的问题
PlayerView在创建的时候回生成一个PlayerControlView,PlayerControlView构造方法中会用到字体。这个字体在某些机型上找不到。导致应用崩溃。
报错信息大概是这样的
Binary XML file line #14: Error inflating class androidx.media3.ui.PlayerView
androidx.media3.ui.PlayerControlView.<init>(MyApplication:83)
androidx.media3.ui.PlayerView.<init>(MyApplication:92)
androidx.media3.ui.PlayerView.<init>(MyApplication:2)
解决方案:
把代码下载到本地,然后以库的module的方式依赖
具体操作就是先打开https://github.com/androidx/media/releases/tag/1.3.1
然后下载zip
然后到项目中找到/libraries/ui
在android studio 中File->new ->import module 选择libraries/ui
然后命名一下module的名字
然后找到PlayerControlView 在 构造方法中关于 Typeface 使用字体的逻辑都删掉
然后修改一下build.gradle
// Copyright (C) 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. plugins {id 'com.android.library' // 库模块 } android {namespace 'androidx.media3.ui'compileSdkVersion 34defaultConfig {minSdkVersion 21targetSdkVersion 33}buildTypes {release {minifyEnabled false}} }dependencies {implementation 'androidx.media3:media3-common:1.3.1'compileOnly 'androidx.recyclerview:recyclerview:1.3.0'compileOnly 'androidx.media:media:1.7.0'implementation 'androidx.annotation:annotation:1.3.0'compileOnly 'org.checkerframework:checker-qual:3.13.0'compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:1.8.20'}ext {releaseArtifactId = 'media3-ui'releaseName = 'Media3 UI module' }
最后在主工程build.gradle引用一下
implementation project(":media3_ui")
然后setting.gradle 申明一下
include(':media3_ui')