【问题标题】:MediaPlayer.setDataSource deprecated on android 6android 6 上不推荐使用 MediaPlayer.setDataSource
【发布时间】:2019-10-29 20:21:22
【问题描述】:

我正在使用 MediaPlayer 创建一个应用程序来播放原始文件夹中的音频文件。它在 android 7 及更高版本上运行良好,但在 android 6 上它会崩溃。似乎 MediaPlayer.setDataSource 在 android 6 上已被弃用。那么应该使用什么来使 MediaPlayer 在 android 6 上工作。有什么替代方法可以让我的应用程序在 android 6 上工作。

我从 developer.android.com 下载并尝试运行示例应用,但它也在 android 6 上崩溃。

崩溃日志 -

java.lang.NoSuchMethodError: No virtual method setDataSource(Landroid/content/res/AssetFileDescriptor;)V in class Landroid/media/MediaPlayer; or its super classes (declaration of 'android.media.MediaPlayer' appears in /system/framework/framework.jar)
        at com.example.android.mediaplayersample.MediaPlayerHolder.loadMedia(MediaPlayerHolder.java:86)
        at com.example.android.mediaplayersample.MainActivity.onStart(MainActivity.java:57)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)

【问题讨论】:

  • 帮自己一个忙,使用 ExoPlayer。

标签: android android-mediaplayer audio-streaming


【解决方案1】:

似乎 MediaPlayer.setDataSource 在 android 6 上已被弃用

不,the method that you are trying to call 那时不存在。它是在 API 级别 24 中添加的。

那么应该使用什么来让 MediaPlayer 在 android 6 上运行

对于原始资源,请使用the static create() method that takes a resource ID

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2019-12-15
    相关资源
    最近更新 更多