I have nearly 28 sound files in which most of them have the duration
less than a second (i.e in milliseconds), to reduce the delay in
creating the player everytime, i created a player for each sound file
i.e 28 MediaPlayers and each sound file plays on its own player and
all the sounds are managed by a separate single thread (FYI only that
sound thread can call these methods no other thread can start the
sounds explicitly), so I can confirm that all the methos calling is
done by a single thread.

and regarding the other code, i have posted all what am using and the
only other thing which is not here is the constructor which converts
the given input stream to a file as the MediaPlayer cannot play sounds
directly with a stream.

(I am packing all the sounds in a single pack and open it at runtime
and give the input stream to Player as it is done in J2ME, so again am
converting the given input stream to a file.)
Here is the below code of Constructor.

protected PlayerImpl(String fileName1, InputStream inputstream)
{
        fileName = fileName1;
        fileInputStream = inputstream;

        //Creates a FileOutput pointer to dump the InputStream., with
permissions: 662 (rw- rw- -w-)
        FileOutputStream fOut = null;
        try{
                fOut = Utils.getContext().openFileOutput
(fileName,Context.MODE_WORLD_WRITEABLE);
        }
        catch(Exception filenotfound){
                if (Utils.debugEnabled)
                        android.util.Log.d("Player ERROR","FileOutputStream 
FAILED!");
        }
        //we dump the 'stream here...
        int n;
        try {
                while ((n=inputstream.read(FOS_BUFFER))>=0)
                {
                        fOut.write(FOS_BUFFER, 0, n);
                }
                //we close fOut but DO NOT CLOSE fIn !!
                fOut.close();
        } catch (IOException e1) {
                if (Utils.debugEnabled)
                        android.util.Log.d("Player ERROR","temp file creation 
FAILED");
        }

}

I dont think this Constructor has anything to do with the errors I
get, to point-out mostly i found these errors are raised when the
device is kept idle for sometime while sounds are playing, the device
backlight goes off and device enters the idle state....
at this point all these exceptions are raised and with less
reproducibility my game crashes.

does the phone stop the sounds while it goes to standby mode?
I have my own code to stop the sounds when an interrupt occurs, so
does this conflict with the device method calls?

On May 7, 7:59 pm, Marco Nelissen <marc...@android.com> wrote:
> Based on the code you posted, the "stop called in state 2" is because you
> call stop() in your OnCompletionListener, which isn't necessary (it's
> already stopped at that point).
> I don't see how the other problems could happen with the code you posted,
> unless you have multiple threads accidentally using the same MediaPlayer, or
> there is some other code involved that you didn't post.
>
> On Thu, May 7, 2009 at 1:36 AM, Sudha <sudhaker...@gmail.com> wrote:
>
> > Hi I am using MediaPlayer to play my sounds below is my another post
>
> >http://groups.google.com/group/android-developers/browse_thread/threa...
>
> > I tried all the possible ways stated in the above post but could not
> > play sounds accordingly so changed the sound code as below,
>
> > Now the sounds are working fine and no sound is getting skipped but am
> > getting some errors which i cannot figure why they are coming...?
>
> > Below are some errors:
>
> > 1-      E/MediaPlayer( 2173): stop called in state 1
>
> > 2-      E/MediaPlayer( 2173): stop called in state 2
>
> > the above errors i get frequently and i get the below one's randomly,
> > the game freezes and requires a force close when i encounter the below
> > errors
>
> > 3-      E/MediaPlayer( 2173): setDataSource called in state 2
> >         W/System.err( 2173): java.lang.IllegalStateException
> >         W/System.err( 2173):    at
> > android.media.MediaPlayer.setDataSource(Native Method)
>
> > 4-      E/MediaPlayer( 2173): setDataSource called in state 128
> >         W/System.err( 2173): java.lang.IllegalStateException
> >         W/System.err( 2173):    at
> > android.media.MediaPlayer.setDataSource(Native Method)
> >         W/System.err( 2173):    at
> > android.media.MediaPlayer.setDataSource(MediaPlayer.java:247)
>
> > 5-      E/MediaPlayer( 2173): prepareAsync called in state 128
> >         E/MediaPlayer( 2173): setDataSource called in state 128
> >         W/System.err( 2173): java.lang.IllegalStateException
> >         W/System.err( 2173):    at
> > android.media.MediaPlayer.setDataSource(Native Method)
>
> > 6-      E/MediaPlayerService(   31): offset error
> >         E/MediaPlayer( 2173): Unable to to create media player
>
> > I have observed that all the sounds played atleast once in the game
> > without any error.
>
> > I want to know what are the states 1,2 & 128 and why are the errors
> > raised.
>
> > As per the
> >http://developer.android.com/reference/android/media/MediaPlayer.html...
> > I suppose there is no problem in my code but y is that error shown...?
>
> > Plz check the below code:
>
> > public void stop() throws MediaException {
> >        try
> >        {
> >                mp.stop();
> >                mp.reset();
> >                FileInputStream fIn =
> > Utils.getContext().openFileInput(fileName);
> >                if (fIn != null)
> >                {
> >                        mp.setDataSource(fIn.getFD());
> >                        fIn.close();
> >                }
> >        }
> >        catch(Exception e){e.printStackTrace();}
> >        isPlayingSound = false;
> > }
>
> > public boolean isPlayingSound; //class member
> > MediaPlayer mp = null;
> > String last_req = "";
> > public void playSound(final String res) {
> >        if (isPlayingSound){
> >                return;
> >        }
> >        try {
> >                if (!last_req.equals(res))
> >                {
> >                        last_req = res;
> >                        mp = new MediaPlayer();
>
> >                        FileInputStream fIn =
> > Utils.getContext().openFileInput(res);
> >                        if (fIn != null)
> >                        {
> >                                mp.setDataSource(fIn.getFD());
> >                                fIn.close();
> >                        }
>
> >                        mp.setOnCompletionListener(new
> >                        MediaPlayer.OnCompletionListener() {
> >                                public void onCompletion(MediaPlayer mp)
> >                                {
> >                                        try
> >                                        {
> >                                                mp.stop();
> >                                                mp.reset();
> >                                                FileInputStream fIn =
> > Utils.getContext().openFileInput(res);
> >                                                if (fIn != null)
> >                                                {
>
> >  mp.setDataSource(fIn.getFD());
> >                                                        fIn.close();
> >                                                }
> >                                                isPlayingSound = false;
> >                                        }
> >                                        catch(Exception
> > e){e.printStackTrace();}
>
> >                                }
> >                        });
>
> >                        mp.setOnErrorListener(new
> >                        MediaPlayer.OnErrorListener() {
> >                                public boolean onError(MediaPlayer mp, int
> > what, int extra)
> >                                {
> >                                        mp.release();
> >                                        mp = null;
> >                                        deleteSoundFile(res);
> >                                        isPlayingSound = false;
> >                                        last_req="";
> >                                        System.gc();
> >                                        new
> > PlayerImpl(fileName,fileInputStream);
> >                                        return false;
> >                                }
> >                        });
> >                }
>
> >                // mp.prepareAsync();
> >                if (isLooping())
> >                {
> >                        mp.setLooping(true);
> >                }
> >                mp.prepare();
> >                mp.start();
> >                isPlayingSound = true;
>
> >        }
> >        catch (Exception e) {
> >                mp.release();
> >                deleteSoundFile(res);
> >                isPlayingSound = false;
> >                mp = null;
> >                System.gc();
> >                new PlayerImpl(fileName,fileInputStream);
> >                last_req="";
> >                playSound(fileName);
> >        }
> > }
>
>
--~--~---------~--~----~------------~-------~--~----~
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