MediaPlayer 对象应该如何在 android 中工作?

How is a MediaPlayer object supposed to work in android?

我刚开始玩 Android 音频,我尝试使用简单的媒体播放器在应用程序打开时播放声音。好像什么都玩不了。考虑到这是文档中的示例(不起作用),我不知道应该如何进行更深入的调查。我也没有错误来指导我。这是代码:

package com.example.audiotutorial;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(this,R.raw.samplesound);
        mediaPlayer.setVolume(50,50);
        mediaPlayer.start();
    }
}

我的资源中有一个原始文件夹。我尝试使用 mp3、ogg 和 wav 文件。 None 他们成功了。感谢您的任何想法。

谢谢,

LE:关于 logcat,我收到一个巨大的 i/zygote 警告,说我无法粘贴,因为它太长了。结论好像是这样:

2021-02-28 14:27:51.071 8489-8489/com.example.audiotutorial I/zygote: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
    

2021-02-28 14:27:51.071 8489-8489/com.example.audiotutorial I/zygote: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.audiotutorial-eXgKSoGwVETE-MaFh6QPPQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.audiotutorial-eXgKSoGwVETE-MaFh6QPPQ==/lib/x86, /system/lib, /vendor/lib]]

之后我拥有的那个巨大的东西:

2021-02-28 14:27:51.355 8489-8521/com.example.audiotutorial D/OpenGLRenderer: HWUI GL Pipeline
2021-02-28 14:27:51.718 8489-8521/com.example.audiotutorial I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2021-02-28 14:27:51.718 8489-8521/com.example.audiotutorial I/OpenGLRenderer: Initialized EGL, version 1.4
2021-02-28 14:27:51.719 8489-8521/com.example.audiotutorial D/OpenGLRenderer: Swap behavior 1
2021-02-28 14:27:51.721 8489-8521/com.example.audiotutorial W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-02-28 14:27:51.721 8489-8521/com.example.audiotutorial D/OpenGLRenderer: Swap behavior 0
2021-02-28 14:27:51.737 8489-8521/com.example.audiotutorial D/EGL_emulation: eglCreateContext: 0x9d786ae0: maj 2 min 0 rcv 2
2021-02-28 14:27:51.748 8489-8521/com.example.audiotutorial D/EGL_emulation: eglMakeCurrent: 0x9d786ae0: ver 2 0 (tinfo 0x9d789ac0)
2021-02-28 14:27:51.861 8489-8521/com.example.audiotutorial D/EGL_emulation: eglMakeCurrent: 0x9d786ae0: ver 2 0 (tinfo 0x9d789ac0)
2021-02-28 14:27:53.373 8489-8489/com.example.audiotutorial E/libc: Access denied finding property "ro.serialno"
2021-02-28 14:27:53.354 8489-8489/com.example.audiotutorial W/e.audiotutorial: type=1400 audit(0.0:9): avc: denied { read } for name="u:object_r:serialno_prop:s0" dev="tmpfs" ino=5496 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serialno_prop:s0 tclass=file permissive=0
2021-02-28 14:27:53.875 8489-8562/com.example.audiotutorial V/StudioTransport: Agent command stream started.
2021-02-28 14:27:53.877 8489-8561/com.example.audiotutorial V/StudioTransport: Transport agent connected to daemon.
2021-02-28 14:27:54.087 8489-8566/com.example.audiotutorial V/StudioTransport: Handling agent command 1200 for pid: 8489.
2021-02-28 14:27:54.087 8489-8566/com.example.audiotutorial V/StudioTransport: JNIEnv not attached
2021-02-28 14:27:54.084 8489-8489/com.example.audiotutorial W/Thread-2: type=1400 audit(0.0:10): avc: denied { write } for name="perfd" dev="dm-0" ino=65543 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:shell_data_file:s0 tclass=dir permissive=0
2021-02-28 14:27:54.389 8489-8566/com.example.audiotutorial V/StudioTransport: Handling agent command 1200 for pid: 8489.
2021-02-28 14:27:54.438 8489-8566/com.example.audiotutorial V/StudioTransport: Handling agent command 1200 for pid: 8489.
2021-02-28 14:27:55.294 8489-8566/com.example.audiotutorial W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
2021-02-28 14:27:55.371 8489-8566/com.example.audiotutorial I/chatty: uid=10080(com.example.audiotutorial) Thread-2 identical 1 line
2021-02-28 14:27:55.494 8489-8566/com.example.audiotutorial W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
2021-02-28 14:27:55.652 8489-8566/com.example.audiotutorial W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!

一个可能的解释是您的项目试图支持 Android 8 或更低版本。 class OnUnhandledKeyEventListener 被列为在 Android 9(API 级别 28)中引入。

我认为可能存在一些替代配置问题来解释系统无法找到这个 class。无论如何,解决这个问题(系统找到OnUnhandledKeyEventListener的能力)将是调试工作的一个很好的重点。

我发现了问题。我是 运行 我的 Ubuntu 20.04 下 android studio 的 snap 版本,显然它对可以与之交互的系统有一些限制;在这种情况下,它不会使用系统音频。如果其他人有类似的问题,这里是我用来得出结论的步骤:

  1. 我在每一行之后添加了一些日志,以检查程序是否到达该点并正确执行。就我而言,确实如此。
  2. 我添加了一个新变量并检查程序是否“认为”它正在使用 mediaPlayer.isPlaying() 播放声音。然后我记录了这个。在我的例子中它是“真实的”所以我得出结论它必须对模拟器声音输出做一些事情。
  3. 我检查了 android 模拟器是否存在已知的音频问题。就我而言,在 Ubuntu 20.04 上使用 Android Studio 的 snap 版本我发现有。有更多的帖子用户建议从官方网站安装版本...所以我在这里。