user1107753
user1107753

Reputation: 1646

Android Media Player cannot play multiple audio files

I have created a list view which contains a list of images defined by the ImageView tag. When the user clicks on each image in the list view I would like to play the audio associated with that image. With the code I have below when I click on the first image the media player is intialised and plays the audio. But when I click for a second time on the same image to play the audio again the application crashes with a IllegalStateException with the logs as shown in the log file. So I am thinking that I have got something wrong with my media player states but am not sure.

i had this bit of code working if I go to another activity from the list view image and play the audio in that activity then return to the list view and click on another image to play audio, but I dont want to do this as creating a mediaPlayer object every time a image is selected is probably bad for memory and I think what I am trying to do should be possible.

I have some basic methods to control the media player using the media controller and this used to come up if i had it in a different activity when I touched the screen but the ontouch event does not respond anymore either. Ideally rather then clicking on the image to stop the media player I want to bring up the media controller to control the audio.

If the audio is already playing and i click the image again I added some code to check if the audio is playing and then release and stop the media player but this did not fix anything. i have left this in and the logs are with this bit of code included.

My phone which I am using has a sdk of 4.0.4. I am developing in Eclipse.

thanks in advance.

Below is my activity file which plays the audio when it recieves the onclick event.

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;
  }
}

The parent layout for the list view

<?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>

This is my row.xml layout

<?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>

I have included two log files. The first one is with the mediaplayer.reset() method

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

And this one is with the mediaPlayer.reset method commented out.

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

Upvotes: 2

Views: 6745

Answers (2)

Gan
Gan

Reputation: 1399

If you are using a single instance of the mediaplayer for your activity and plan on re-using it, do not release it. If you release it, you will have to acuire another instance before you can use it. You can however reset the mediaplayer before you prepare it.

Upvotes: 1

user529543
user529543

Reputation:

java.lang.IllegalStateException

It says your media player wasn't initialized properly, anyhow it is in a wrong state for him.

check the

om.example.android.htc.test.AudioPlayer$1.onItemClick(AudioPlayer.java:59)

line to see what is there. It doesn't like the setDataSource method.

Multiple statements in :

mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getDeclaredLength());

Please break into 1 statement / line

Upvotes: 0

Related Questions