媒体播放器(-19,0)用于播放来自raw的大量音频文件

时间:2014-08-06 10:49:34

标签: android android-mediaplayer

我正在处理原始文件夹中的所有声音。使用playSound()并在完成后释放声音以及错误释放声音。在使用playSound()播放几声后,我仍然会收到错误(-19,0)。

public static MediaPlayer mediaPlayer;

public static boolean playSound(final Context context, int resId) {

        if(releaseSound()){

        mediaPlayer = MediaPlayer.create(context, resId);

        mediaPlayer.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d("test", "Media player error :" + what + " extra:" + extra);

         if(what == 19)
            FireToast
                        .showToast(context,
                                "MediaPlayer error (Out of buffer). Please restart your application.");

                releaseSound();

                mp.release();
                mp = null;
                return false;
            }
        });
        if (resId == 0) {
            FireToast.showToast(context, "File not found");
            return false;
        }

        // mediaPlayer = MediaPlayer.create(context, resId);

        try {
            if (mediaPlayer != null)
                mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        releaseSound();
                        FireToast.showToast(context,"MediaPlayer song completed.");
                    }
                });
            mediaPlayer.start();
            setPause(false);

            return true;

        } catch (IllegalStateException e) {
            e.printStackTrace();
            return false;
        }
        }
        return false;
    }

public static boolean releaseSound() {
        try {
            if (mediaPlayer != null) {
                if (mediaPlayer.isPlaying())
                    mediaPlayer.stop();

                mediaPlayer.reset();
                mediaPlayer.release();

            }
            mediaPlayer = null;
            isPause = false;

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

0 个答案:

没有答案