我的问题是关于我在处理媒体播放器时经常遇到的一个问题,不仅是音频文件,还有视频文件。如果在未重新启动应用程序的情况下按下“播放按钮”10次以上,应用程序将停止,LogCat将显示错误消息“E/MediaPlayer:错误(1,-19)”。
我在媒体播放器上搜索了Stack和Android开发者页面,但无法找到解决此问题的方法。下面的代码似乎让我按了大约16次播放按钮,但没有更多。这比我尝试过的其他方法要多得多。
这个特殊的代码播放简短的动物声音,让我侄子识别。我觉得我在媒体播放器上遗漏了一些东西,但我无法确定。
<RelativeLayout
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"
android:background="@drawable/gradient"
tools:context="com.curiousca.griddemo.MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="@+id/bluejay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_draw"
android:layout_margin="5dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:onClick="onClick"
android:text="Bluejay" />
</GridLayout>
`
public void onClick(View view) {
int id = view.getId();
String songId = "";
songId = view.getResources().getResourceEntryName(id);
int resourceId = getResources().getIdentifier(songId, "raw", getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(this, resourceId);
if (mediaPlayer != null){
mediaPlayer.start();
}
//Log.i("Click", String.valueOf(view.getId()));
Log.i("tap", songId);
}