A: 

ok try this an excelent example

Custom Audio Streaming with MediaPlayer

Jorgesys
arakn0
A: 

Did you find a solution? I have different short audio files as raw resources in my project, but don't find a way to re-use an old mediaplayer by just setting the new resource id! What I now do is to create a new mediaplayer for each sound which seems to be inefficient, even if I release all resources after completion in my OnCompletionListener, the application lags..

Sonja
A: 

You can use code

mp.pause(); mp.seekTo(0);

to stop music player,and you can start if you want to continue playing.

Rectangle
+1  A: 

Here is what I did to load multiple resources with a single MediaPlayer:

/**
 * Play a sample with the Android MediaPLayer.
 *
 * @param resid Resource ID if the sample to play.
 */
private void playSample(int resid)
{
    AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);

    try
    {   
        mediaPlayer.reset();
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
        mediaPlayer.prepare();
        mediaPlayer.start();
        afd.close();
    }
    catch (IllegalArgumentException e)
    {
        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
    }
    catch (IllegalStateException e)
    {
        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
    }
    catch (IOException e)
    {
        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
    }

mediaPlay is a member variable that get created and released at other points in the class. This may not be the best way (I am new to Android myself), but it seems to work. Just note that the code will probably fall trough to the bottom of the method before the mediaPlayer is done playing. If you need to play a series of resources, you will still need to handle this case.

adstro