如何管理Android MediaPlayer状态,错误和异常?

时间:2012-02-25 18:43:10

标签: java android audio media-player media

我正在尝试在处理扫描条形码后播放一个短的声音字节。我的代码目前可以正常工作多达20次扫描。但是,即使在应用程序被杀死后,MediaPlayer也会重复抛出以下错误:

MediaPlayer: Error (-38, 0)

MediaPlayer: Attempt to perform seekTo in wrong state: mPlayer=0xXXXXXX, mCurrentState=0

- X表示随机的6位数存储器地址 -

我最初是在播放UI线程的声音字节。因为我已经创建了一个处理程序以试图缓解这个问题。这就是我访问处理程序的方式:

    try {
            mHandler.post(mScanFeedback);
    } catch (IllegalStateException e) {
            System.out.println("Media player state error");
            e.printStackTrace();
    }

以下是处理程序的代码:

private Runnable mScanFeedback = new Runnable(){
    public void run() {
        if(getString(R.string.working).equals(mStatusHourly)) {
            final MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.bleep_working);
            mediaPlayer.setOnErrorListener(new OnErrorListener() {
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mediaPlayer.reset();
                    System.out.println("Media Player onError callback!");
                    return true;
                }
            });
            mediaPlayer.start();
            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                mediaPlayer.release();
            }
        } else if(getString(R.string.not_working).equals(mStatusHourly)) {
            final MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.bleep_not_working);
            mediaPlayer.setOnErrorListener(new OnErrorListener() {
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mediaPlayer.reset();
                    System.out.println("Media Player onError callback!");
                    return true;
                }
            });

            mediaPlayer.start();
            try {
                Thread.sleep(275);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                mediaPlayer.release();
            }
        } else {
            System.out.println("Audio feedback failed as status was indeterminate.");
        }

    }
};

一开始我没有调用release()并且添加它似乎没有让它工作得更好或更糟。发生问题时,永远不会调用onError回调。我尝试在每次播放后重置()媒体播放器,但是会引发错误。现在,我试图重新启动手机,以防止我的Logcat因不断重复的两条错误线的冲击而无法使用。

我正在使用zxing的条形码扫描仪,并且在该活动中发出了一声短促的哔声,以确认条形码已被捕获。我的一小部分想知道他们是不是那里的冲突。

我还是编程新手,这是我关于堆栈溢出的第一个问题。如果我应该提供任何其他信息,或者我是否应该尝试让它更加精益,请告诉我。

更新

我无法解决MediaPlayer的问题。但是,通过切换到SoundPool实现,我能够解决这个问题。下面的课程提供了所需的功能。

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

private  SoundPool mSoundPool;
private  HashMap mSoundPoolMap;
private  AudioManager  mAudioManager;
private  Context mContext;

public void initSounds(Context theContext) {
    mContext = theContext;
    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    mSoundPoolMap = new HashMap();
    mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}

public void addSound(int index, int SoundID) {
    mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));
}

public void playSound(int index) {
    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(index, streamVolume, streamVolume, 1, 0, 1f);
}

public void playLoopedSound(int index) {
    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(index, streamVolume, streamVolume, 1, -1, 1f);
}
}

然后我从我的活动中访问了:

mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.bleep_working);
mSoundManager.addSound(2, R.raw.bleep_not_working);
mSoundManager.playSound(1);
mSoundManager.playSound(2);

0 个答案:

没有答案