2

我创建了一个列表视图,其中包含由 ImageView 标记定义的图像列表。当用户单击列表视图中的每个图像时,我想播放与该图像关联的音频。当我点击第一张图片时,使用下面的代码,媒体播放器被初始化并播放音频。但是,当我第二次单击同一图像以再次播放音频时,应用程序崩溃并出现 IllegalStateException,其日志如日志文件中所示。所以我认为我的媒体播放器状态有问题,但不确定。

如果我从列表视图图像转到另一个活动并在该活动中播放音频然后返回到列表视图并单击另一个图像播放音频,我有这段代码工作,但我不想这样做,因为创建一个每次选择图像时的 mediaPlayer 对象可能不利于记忆,我认为我想要做的应该是可能的。

我有一些使用媒体控制器控制媒体播放器的基本方法,如果我在触摸屏幕时将其置于不同的活动中,但 ontouch 事件也不再响应,则通常会出现这种情况。理想情况下,而不是单击图像来停止媒体播放器,我想调出媒体控制器来控制音频。

如果音频已经在播放并且我再次单击图像,我添加了一些代码来检查音频是否正在播放,然后释放并停止媒体播放器,但这并没有解决任何问题。我已经把它留在了日志中,其中包含这段代码。

我正在使用的手机的 sdk 为 4.0.4。我正在 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
4

2 回答 2

1

如果您正在为您的活动使用单个媒体播放器实例并计划重新使用它,请不要释放它。如果你释放它,你必须先获得另一个实例才能使用它。但是,您可以在准备之前重置媒体播放器。

于 2012-09-18T11:25:40.547 回答
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条语句/行

于 2012-09-18T11:25:00.140 回答