【发布时间】:2018-05-15 11:23:26
【问题描述】:
在我的应用程序中,我希望有一个简单的方法,它使用 MediaPlayer 类来播放声音资源一次(如果需要,可以再次播放)。
我已经成功地实现了这一点(见下面的代码)并在具有 API 23 的真实硬件设备上成功地对其进行了测试,并且还在 API 级别从 19 一直到 P...的模拟器上成功地测试了它... API 21 除外, 失败的地方。
这不是很奇怪吗?为什么 API 19... 和 23 可以工作,而 21 却不行?
那么我所说的失败是什么意思?好吧,在 API 21 上,如果你按下 play 按钮,没有声音播放,而且似乎什么也没有发生(尽管如果你偷看 logcat,你会看到所有的方式可怕的本地图书馆消息,包括许多“死亡”甚至“墓碑”或两个)。
如果你继续按 play 第二次,同样的事情会发生,在 logcat 中会生成更多内部错误消息......第三次,你最终会崩溃。
我能看到的唯一可能与我可以控制的任何事情有关的错误是 Log.i 中捕获的异常:
"com.example.boober.stackqmediaplayer I/SFX: 错误:java.io.IOException:准备失败。:status=0x64"
所有其他消息和最终堆栈跟踪均由内部原生库生成。
我搜索了这些错误,阅读了 logcat 和 MediaPlayer 文档。希望我只是在做一些根本上愚蠢的事情,有人可以向我指出。
我真的希望有人可以看看。
我已经精简了一个非常简单的问题示例,应该很容易在 Android Studio 中剪切/粘贴/重建,以便您可以重现该问题:
主活动:
public class MainActivity extends Activity {
MediaPlayer ourPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ourPlayer = MediaPlayer.create(this, R.raw.soundeffect);
}
public void play(View v) {
try {
ourPlayer.stop();
ourPlayer.prepare();
ourPlayer.start();
} catch (IOException e) {
Log.i("SFX", "error:" + e.toString());
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:onClick="play"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="PLAY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Res 文件夹(使用任何音频文件):
Build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.boober.stackqmediaplayer"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
【问题讨论】:
-
请检查我的答案,如果有遗漏请告诉我。
标签: android audio crash buffer android-mediaplayer