我创建了一个列表视图,其中包含ImageView标记定义的图像列表。当用户点击列表视图中的每个图像时,我想播放与该图像相关联的音频。使用下面的代码,当我点击第一张图像时,媒体播放器被初始化并播放音频。但是,当我再次在同一图像上单击以再次播放音频时,应用程序崩溃时会出现IllegalStateException,日志文件中会显示日志。所以我认为我的媒体播放器状态出了问题,但我不确定。
如果我从列表视图图像转到另一个活动并在该活动中播放音频然后返回到列表视图并单击另一个图像来播放音频,我有这段代码工作,但我不想这样做因为每次选择图像时创建一个mediaPlayer对象可能对内存不利,我认为我应该做的事情应该是可能的。
我有一些使用媒体控制器控制媒体播放器的基本方法,当我触摸屏幕时,如果我在不同的活动中使用它,但是ontouch事件也不再响应。理想情况下,然后单击图像以停止媒体播放器,我想调出媒体控制器来控制音频。
如果音频已经播放,我再次点击图像,我添加了一些代码来检查音频是否正在播放,然后释放并停止媒体播放器,但这并没有解决任何问题。我把它放进去了,日志包含了这段代码。
我使用的手机有4.0.4的sdk。我在Eclipse中开发。
提前感谢。
下面是我的活动文件,它在收到onclick事件时播放音频。
package com.example.android.htc.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.MediaController;
public class AudioPlayer extends ListActivity implements OnPreparedListener, MediaController.MediaPlayerControl {
private static final String TAG = "AudioPlayer";
public static final String AUDIO_FILE_NAME = "audioFileName";
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;
private qAyatAdapter m_adapter;
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q_sur);
m_adapter = new qAyatAdapter(this, R.layout.row, getAyats());
setListAdapter(m_adapter);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch(position) {
case 0:
try {
//mediaPlayer.setDataSource(audioFile);
if(isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
//I have tried with this line commented out too but still get the exception the log files for both are included below
mediaPlayer.reset();
}
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.ikhlas);
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getDeclaredLength());
mediaPlayer.prepareAsync();
} catch (IOException e) {
Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
}
case 1:
default:
break;
}
}
});
// audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
// ((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);
}
@Override
protected void onStop() {
super.onStop();
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
mediaController.show();
return false;
}
//--MediaPlayerControl methods----------------------------------------------------
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
//--------------------------------------------------------------------------------
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.main_q_audio_view));
mediaPlayer.start();
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
private List<qAyat> getAyats() {
List<qAyat> ayats = new ArrayList<qAyat>();
ayats.add(new qAyat("1.2", "hello"));
ayats.add(new qAyat("1.3", "Goodbye"));
return ayats;
}
}
列表视图的父布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_q_audio_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
这是我的row.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/q_ayat"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/translation"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
我已经包含了两个日志文件。第一个是使用mediaplayer.reset()方法
09-18 11:58:21.045: D/AudioSystem(32211): AudioSystem::get_audio_flinger() in at 53
09-18 11:58:21.045: D/AudioSystem(32211): gLock get at 55
09-18 11:58:21.045: D/AudioSystem(32211): before defaultServiceManager() at 57
09-18 11:58:21.045: D/AudioSystem(32211): after defaultServiceManager() at 59
09-18 11:58:21.045: D/AudioSystem(32211): service got at 63
09-18 11:58:21.055: D/AudioSystem(32211): leave AudioSystem::get_audio_flinger() at 81
09-18 11:58:21.055: D/AudioSystem(32211): AudioSystem::get_audio_flinger() in at 53
09-18 11:58:21.055: D/AudioSystem(32211): gLock get at 55
09-18 11:58:21.055: D/AudioSystem(32211): leave AudioSystem::get_audio_flinger() at 81
09-18 11:58:21.165: I/Adreno200-EGLSUB(32211): <ConfigWindowMatch:2078>: Format RGBA_8888.
09-18 11:58:21.395: D/OpenGLRenderer(32211): Flushing caches (mode 0)
09-18 11:58:21.395: D/memalloc(32211): /dev/pmem: Unmapping buffer base:0x53893000 size:13619200 offset:12083200
09-18 11:58:21.395: D/memalloc(32211): /dev/pmem: Unmapping buffer base:0x54590000 size:15155200 offset:13619200
09-18 11:58:21.405: D/memalloc(32211): /dev/pmem: Unmapping buffer base:0x52641000 size:5488640 offset:3952640
09-18 11:58:29.313: W/MediaPlayer(32211): info/warning (1, 902)
09-18 11:58:29.323: I/MediaPlayer(32211): Info (1,902)
09-18 11:58:29.323: D/AudioPlayer(32211): onPrepared
09-18 11:58:29.463: D/MediaPlayer(32211): start() in
09-18 11:58:29.734: D/MediaPlayer(32211): start() out
09-18 11:58:29.874: I/Adreno200-EGLSUB(32211): <ConfigWindowMatch:2078>: Format RGBA_8888.
09-18 11:58:32.887: D/memalloc(32211): /dev/pmem: Unmapping buffer base:0x53803000 size:5918720 offset:5611520
09-18 11:58:32.887: D/memalloc(32211): /dev/pmem: Unmapping buffer base:0x53da8000 size:1843200 offset:1536000
09-18 11:58:32.887: D/memalloc(32211): /dev/pmem: Unmapping buffer base:0x53f6a000 size:2150400 offset:1843200
09-18 11:58:36.190: D/MediaPlayer(32211): stop() in
09-18 11:58:36.190: D/MediaPlayer(32211): stop() out
09-18 11:58:36.190: D/MediaPlayer(32211): release() in
09-18 11:58:36.220: D/AudioSystem(32211): AudioSystem::get_audio_flinger() in at 53
09-18 11:58:36.220: D/AudioSystem(32211): gLock get at 55
09-18 11:58:36.220: D/AudioSystem(32211): leave AudioSystem::get_audio_flinger() at 81
09-18 11:58:36.230: D/MediaPlayer(32211): release() out
09-18 11:58:36.230: D/MediaPlayer(32211): reset() in
09-18 11:58:45.509: D/AndroidRuntime(32211): Shutting down VM
09-18 11:58:45.509: W/dalvikvm(32211): threadid=1: thread exiting with uncaught exception (group=0x40ab7228)
09-18 11:58:45.589: E/AndroidRuntime(32211): FATAL EXCEPTION: main
09-18 11:58:45.589: E/AndroidRuntime(32211): java.lang.IllegalStateException
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.media.MediaPlayer._reset(Native Method)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.media.MediaPlayer.reset(MediaPlayer.java:1378)
09-18 11:58:45.589: E/AndroidRuntime(32211): at com.example.android.htc.test.AudioPlayer$1.onItemClick(AudioPlayer.java:55)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.widget.AbsListView.performItemClick(AbsListView.java:1077)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2533)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.widget.AbsListView$1.run(AbsListView.java:3198)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.os.Handler.handleCallback(Handler.java:605)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.os.Looper.loop(Looper.java:154)
09-18 11:58:45.589: E/AndroidRuntime(32211): at android.app.ActivityThread.main(ActivityThread.java:4945)
09-18 11:58:45.589: E/AndroidRuntime(32211): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 11:58:45.589: E/AndroidRuntime(32211): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 11:58:45.589: E/AndroidRuntime(32211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-18 11:58:45.589: E/AndroidRuntime(32211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-18 11:58:45.589: E/AndroidRuntime(32211): at dalvik.system.NativeStart.main(Native Method)
09-18 11:58:48.552: D/Process(32211): killProcess, pid=32211
09-18 11:58:48.562: D/Process(32211): dalvik.system.VMStack.getThreadStackTrace(Native Method)
09-18 11:58:48.562: D/Process(32211): java.lang.Thread.getStackTrace(Thread.java:599)
09-18 11:58:48.562: D/Process(32211): android.os.Process.killProcess(Process.java:788)
09-18 11:58:48.562: D/Process(32211): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
09-18 11:58:48.562: D/Process(32211): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
09-18 11:58:48.572: D/Process(32211): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
09-18 11:58:48.572: D/Process(32211): dalvik.system.NativeStart.main(Native Method)
09-18 11:58:48.982: D/libEGL(32271): loaded /system/lib/egl/libGLES_android.so
09-18 11:58:48.992: D/libEGL(32271): loaded /system/lib/egl/libEGL_adreno200.so
09-18 11:58:48.992: D/libEGL(32271): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-18 11:58:49.002: D/libEGL(32271): loaded /system/lib/egl/libGLESv2_adreno200.so
09-18 11:58:49.012: I/Adreno200-EGLSUB(32271): <ConfigWindowMatch:2078>: Format RGBA_8888.
09-18 11:58:49.022: D/OpenGLRenderer(32271): Enabling debug mode 0
这个是用mediaPlayer.reset方法注释掉的。
09-18 12:01:49.358: W/MediaPlayer(523): info/warning (1, 902)
09-18 12:01:49.358: I/MediaPlayer(523): Info (1,902)
09-18 12:01:49.358: D/AudioPlayer(523): onPrepared
09-18 12:01:49.499: D/MediaPlayer(523): start() in
09-18 12:01:49.509: D/MediaPlayer(523): start() out
09-18 12:01:49.619: I/Adreno200-EGLSUB(523): <ConfigWindowMatch:2078>: Format RGBA_8888.
09-18 12:01:52.612: D/memalloc(523): /dev/pmem: Unmapping buffer base:0x53ac4000 size:5918720 offset:5611520
09-18 12:01:52.612: D/memalloc(523): /dev/pmem: Unmapping buffer base:0x5228f000 size:1843200 offset:1536000
09-18 12:01:52.612: D/memalloc(523): /dev/pmem: Unmapping buffer base:0x54069000 size:2150400 offset:1843200
09-18 12:01:57.236: D/MediaPlayer(523): stop() in
09-18 12:01:57.236: D/MediaPlayer(523): stop() out
09-18 12:01:57.236: D/MediaPlayer(523): release() in
09-18 12:01:57.266: D/AudioSystem(523): AudioSystem::get_audio_flinger() in at 53
09-18 12:01:57.266: D/AudioSystem(523): gLock get at 55
09-18 12:01:57.266: D/AudioSystem(523): leave AudioSystem::get_audio_flinger() at 81
09-18 12:01:57.266: D/MediaPlayer(523): release() out
09-18 12:02:05.434: D/AndroidRuntime(523): Shutting down VM
09-18 12:02:05.434: W/dalvikvm(523): threadid=1: thread exiting with uncaught exception (group=0x40ab7228)
09-18 12:02:05.504: E/AndroidRuntime(523): FATAL EXCEPTION: main
09-18 12:02:05.504: E/AndroidRuntime(523): java.lang.IllegalStateException
09-18 12:02:05.504: E/AndroidRuntime(523): at android.media.MediaPlayer.setDataSource(Native Method)
09-18 12:02:05.504: E/AndroidRuntime(523): at com.example.android.htc.test.AudioPlayer$1.onItemClick(AudioPlayer.java:59)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.widget.AbsListView.performItemClick(AbsListView.java:1077)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2533)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.widget.AbsListView$1.run(AbsListView.java:3198)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.os.Handler.handleCallback(Handler.java:605)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.os.Looper.loop(Looper.java:154)
09-18 12:02:05.504: E/AndroidRuntime(523): at android.app.ActivityThread.main(ActivityThread.java:4945)
09-18 12:02:05.504: E/AndroidRuntime(523): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 12:02:05.504: E/AndroidRuntime(523): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 12:02:05.504: E/AndroidRuntime(523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-18 12:02:05.504: E/AndroidRuntime(523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-18 12:02:05.504: E/AndroidRuntime(523): at dalvik.system.NativeStart.main(Native Method)
09-18 12:02:36.665: D/Process(523): killProcess, pid=523
09-18 12:02:36.665: D/Process(523): dalvik.system.VMStack.getThreadStackTrace(Native Method)
09-18 12:02:36.665: D/Process(523): java.lang.Thread.getStackTrace(Thread.java:599)
09-18 12:02:36.665: D/Process(523): android.os.Process.killProcess(Process.java:788)
09-18 12:02:36.665: D/Process(523): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
09-18 12:02:36.665: D/Process(523): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
09-18 12:02:36.665: D/Process(523): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
09-18 12:02:36.665: D/Process(523): dalvik.system.NativeStart.main(Native Method)
09-18 12:02:36.665: I/Process(523): Sending signal. PID: 523 SIG: 9
09-18 12:02:37.035: D/libEGL(676): loaded /system/lib/egl/libGLES_android.so
09-18 12:02:37.045: D/libEGL(676): loaded /system/lib/egl/libEGL_adreno200.so
09-18 12:02:37.045: D/libEGL(676): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-18 12:02:37.045: D/libEGL(676): loaded /system/lib/egl/libGLESv2_adreno200.so
09-18 12:02:37.065: I/Adreno200-EGLSUB(676): <ConfigWindowMatch:2078>: Format RGBA_8888.
09-18 12:02:37.075: D/OpenGLRenderer(676): Enabling debug mode 0
09-18 12:02:37.115: D/OpenGLRenderer(676): has fontRender patch
09-18 12:02:37.135: D/OpenGLRenderer(676): has fontRender patch
答案 0 :(得分:1)
如果您正在为活动使用单个媒体播放器实例并计划重新使用它,请不要将其释放。如果您将其释放,则必须先使用另一个实例才能使用它。但是,您可以在准备之前重置媒体播放器。
答案 1 :(得分:0)
java.lang.IllegalStateException
它说你的媒体播放器没有正确初始化,无论如何它都处于错误状态。
检查
om.example.android.htc.test.AudioPlayer$1.onItemClick(AudioPlayer.java:59)
排队看看有什么。它不喜欢setDataSource
方法。
以下的多个陈述:
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getDeclaredLength());
请分成1个陈述/行