简体   繁体   English

Android Media Player无法播放多个音频文件

[英]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. 我创建了一个列表视图,其中包含ImageView标记定义的图像列表。 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. 但是,当我第二次单击同一图像再次播放音频时,应用程序崩溃,并带有日志文件中所示的日志IllegalStateException。 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. 如果我从列表视图图像转到另一个活动并在该活动中播放音频,然后返回列表视图并单击另一个图像以播放音频,则我有这段代码可以工作,但是我不想这样做,因为创建了一个每次选择图像时,mediaPlayer对象可能对内存不利,我想我想做的应该是可能的。

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. 我有一些使用媒体控制器来控制媒体播放器的基本方法,如果我在触摸屏幕时进行了其他活动,但曾经出现过,但ontouch事件也不再响应时,就会出现这种方法。 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. 我正在使用的手机的SDK为4.0.4。 I am developing in Eclipse. 我正在Eclipse中进行开发。

thanks in advance. 提前致谢。

Below is my activity file which plays the audio when it recieves the onclick event. 下面是我的活动文件,当它收到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;
  }
}

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 这是我的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>

I have included two log files. 我已经包括了两个日志文件。 The first one is with the mediaplayer.reset() method 第一个是使用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

And this one is with the mediaPlayer.reset method commented out. 而这是用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

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. 但是,您可以在准备媒体播放器之前对其进行重置。

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. 它不喜欢setDataSource方法。

Multiple statements in : 中的多个语句:

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

Please break into 1 statement / line 请分成1条语句/行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM