mediaplayer creation after some time in android
<p>i am new to android and need to play sound and display image after sound over.</p>

<p>below mentioned code tell you that after completion of sound display the next image and play the sound associate with it </p>

<p>code:</p>

<pre><code>mymediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                //mymediaplayer=null;
                //mymediaplayer.reset();
                mseekbar.setProgress(0);
                count++;
                Toast.makeText(getApplicationContext(), "SONG PLAYING OVER", 0).show();
                changeImage(count);
                playsound(count);

            }
        });
</code></pre>

<p>so for some image its ok but after some image its throws exception saying as <strong>Unable to create media player</strong> and then force closing the app.</p>

<p>below code for the playsound method.</p>

<pre><code>private void playsound(int soundindex) {
        // TODO Auto-generated method stub
        if(mymediaplayer!=null)
            mymediaplayer.release();
        Toast.makeText(getApplicationContext(), "SOUND"+soundindex, 0).show();
        if(souncollect.getlengthofsoundfile_array()&gt;=count)
        {
            try
            {
            mymediaplayer=MediaPlayer.create(MainActivity.this, souncollect.capture_sound.get(soundindex));


            mymediaplayer.seekTo(0);
            mymediaplayer.start();
            new Thread(this).start();
            }
            catch(Exception ex)
            {
                Toast.makeText(getApplicationContext(), "ERROR WHILE PLAYING SOUND", Toast.LENGTH_LONG).show();
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(), "NO SOUND TO PLAY", Toast.LENGTH_SHORT).show();
        }
</code></pre>

<p>all the sound and image thing are define on other class inside some arraylist.in the arraylist i had 18 sound and 18 image.</p>

<p>below mentioning the log file for the exception</p>

<pre><code>02-27 13:06:00.908: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 87K, 52% free 2615K/5379K, external 2112K/2137K, paused 53ms
02-27 13:06:10.928: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 23K, 52% free 2621K/5379K, external 2289K/2613K, paused 42ms
02-27 13:06:15.488: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 14K, 52% free 2631K/5379K, external 7212K/8760K, paused 264ms
02-27 13:06:21.039: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 10K, 52% free 2634K/5379K, external 7015K/8761K, paused 42ms
****02-27 13:06:27.098: E/MediaPlayer(1503): Unable to to create media player
02-27 13:06:27.098: D/MediaPlayer(1503): create failed:
02-27 13:06:27.098: D/MediaPlayer(1503): java.io.IOException: setDataSourceFD failed.: status=0x80000000****
02-27 13:06:27.098: D/MediaPlayer(1503):    at android.media.MediaPlayer.setDataSource(Native Method)
02-27 13:06:27.098: D/MediaPlayer(1503):    at android.media.MediaPlayer.create(MediaPlayer.java:666)
02-27 13:06:27.098: D/MediaPlayer(1503):    at com.goutam.imageandplaysound.MainActivity.playsound(MainActivity.java:85)
02-27 13:06:27.098: D/MediaPlayer(1503):    at com.goutam.imageandplaysound.MainActivity.access$3(MainActivity.java:76)
02-27 13:06:27.098: D/MediaPlayer(1503):    at com.goutam.imageandplaysound.MainActivity$1.onCompletion(MainActivity.java:117)
02-27 13:06:27.098: D/MediaPlayer(1503):    at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1316)
02-27 13:06:27.098: D/MediaPlayer(1503):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 13:06:27.098: D/MediaPlayer(1503):    at android.os.Looper.loop(Looper.java:123)
02-27 13:06:27.098: D/MediaPlayer(1503):    at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 13:06:27.098: D/MediaPlayer(1503):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 13:06:27.098: D/MediaPlayer(1503):    at java.lang.reflect.Method.invoke(Method.java:507)
02-27 13:06:27.098: D/MediaPlayer(1503):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 13:06:27.098: D/MediaPlayer(1503):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 13:06:27.098: D/MediaPlayer(1503):    at dalvik.system.NativeStart.main(Native Method)
02-27 13:06:27.218: D/AndroidRuntime(1503): Shutting down VM
02-27 13:06:27.218: W/dalvikvm(1503): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-27 13:06:27.228: E/AndroidRuntime(1503): FATAL EXCEPTION: main
02-27 13:06:27.228: E/AndroidRuntime(1503): java.lang.NullPointerException
02-27 13:06:27.228: E/AndroidRuntime(1503):     at com.goutam.imageandplaysound.MainActivity.playsound(MainActivity.java:106)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at com.goutam.imageandplaysound.MainActivity.access$3(MainActivity.java:76)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at com.goutam.imageandplaysound.MainActivity$1.onCompletion(MainActivity.java:117)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1316)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at android.os.Looper.loop(Looper.java:123)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at java.lang.reflect.Method.invoke(Method.java:507)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 13:06:27.228: E/AndroidRuntime(1503):     at dalvik.system.NativeStart.main(Native Method)
02-27 13:11:27.292: I/Process(1503): Sending signal. PID: 1503 SIG: 9
</code></pre>

<p>as you can see for some image and sound its going ok but after some time it failed to create mediaplayer.so confused and not getting any idea how to implement the mediaplayer for playing sound one by one.its failed after image number 7.</p>

<p>also mentioned the onpause state in the code</p>

<pre><code>@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if(!mymediaplayer.isPlaying())
        {
            mymediaplayer=null;
            mymediaplayer.release();
        }
    }
</code></pre>

<p>so please suggest what will be the better way.thanks for any reply</p>
