Hello.  I am trying to use MediaPlayer to play background sounds for
my game.  I have currently been using as follows, but my application
has been crashing and I can only attribute it to my implementation of
MediaPlayer.  I would like to use only 1 MediaPlayer Object, but I'm
starting to think that it's crashing because I'm not releasing the
resources from the MediaPlayer.  But, once I call release() on the
mediaplayer object, it doesn't seem like I can play it again.  So I'm
stuck in this predicament in how to implement the MediaPlayer to play
background sounds for my game.  The current implementation sort works,
but crashes after some indeterminate amount of time, with a native
crash dump (even though I'm writing all java code).

How can I efficiently use the MediaPlayer to play background sounds.
It seems wrong to 'new' a MediaPlayer for each sound that I have.  Is
this really the way to go?

How can I do this?

Thanks in advance
stringa

package com.ddi.components;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import com.ddi.GameData;

public class ChannelPlayer {

        private final Context _context;
        private final String mChannelName;
        private final MediaPlayer _player;
        private boolean _doLoop = false;

        private static final String URI_BASE = GameData.AudioExtractionPath
                        + File.separator;
        private static final String URI_EXT = ".ogg";

        public ChannelPlayer(Context context, String channelName,
                        boolean doLoop) {
                mChannelName = channelName;
                _player = new MediaPlayer();
                _context = context;
                _doLoop = doLoop;
        }

    private boolean hasPlayed = false;

        public void play(String soundName) {
                try {

            // --- The following commented code seems to only work
once, after that the media player will never play audio files again,
after calling release().
            //try {

                //if(hasPlayed == true) {
                //    _player.release();
                //}

            //}
            //catch(Exception e) {
            //    e.printStackTrace();
           //}

            try {
                _player.reset();
            }
            catch(Exception e) {
                e.printStackTrace();
            }


            _player.setDataSource(_context,
                    Uri.fromFile(new File(URI_BASE + soundName +
URI_EXT)));
                } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                } catch (IllegalStateException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                try {
            _player.prepare();
                        _player.setVolume(1.0f, 1.0f);
                        _player.setLooping(_doLoop);
                        _player.setOnPreparedListener(_onPreparedListener);
            hasPlayed = true;

                } catch (IllegalStateException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        public void stop() {
                try {
                        _player.reset();
                } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                } catch (IllegalStateException e) {
                        e.printStackTrace();
                }
        }

        public void release() {
                _player.release();
        }

        MediaPlayer.OnPreparedListener _onPreparedListener = new
MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mp.start();

        }
    };

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to