简体   繁体   中英

android play sound from sd-card

I need to play sound from sd card. I have method that must do this but it's doesn't work.

When I use this method:

public class AudioPlayService extends Service

{
    MediaPlayer mMediaPlayer;
..............
    public void soundplay(String adr)
        {
            mMediaPlayer = new MediaPlayer();
            try
            {
                if (mMediaPlayer.isPlaying())
                {
                    mMediaPlayer.reset();
                }
                mMediaPlayer.setDataSource(adr);
                mMediaPlayer.prepare();
            } catch (IOException e)
            {
            }
            mMediaPlayer.start();
            mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
            {
                @Override
            public void onCompletion(MediaPlayer mp)
            {
                mp.release();
                mp = null;
            }
        });
    }

Where String adr - it's absolute path to file on sd.

Then I call it:

AudioPlayService s = new AudioPlayService();
s.soundplay(iA.getSdDir() + "Files/Numbers/0.mp3");

and I get an err's:

12-09 13:04:13.829: E/MediaPlayer(16997): error (1, -2147483648)
12-09 13:04:13.829: E/MediaPlayer(16997): start called in state 0
12-09 13:04:13.829: E/MediaPlayer(16997): error (-38, 0)
12-09 13:04:13.839: E/MediaPlayer(16997): Error (-38,0)

player.start() put within try,catch block. Because if player not prepare your song ,still you are calling start method.start method is only called after playback is ready, means prepare is success.Another thing is that you have to release the media player when your player is no longer.Otherwise there is so many player objects are running in back ground.

For some reason, your setDataSource() or Prepare() is failing. So after the exception, start() is giving the error, since the player is not in Prepared state. Add print statements in the catch block to know what is the exception. And move start() into the try catch block.

mMediaPlayer = new MediaPlayer();
try
{
  if (mMediaPlayer.isPlaying())
  {
     mMediaPlayer.reset();
  }
  mMediaPlayer.setDataSource(adr);
  mMediaPlayer.prepare();
  mMediaPlayer.start();
} catch (IOException e)
{
  e.printStackTrace();
}

I myself tried your code:

Mine worked fine when i put just "sdcard/1.mp3"

You ensure that the path is right or not

s.soundplay(iA.getSdDir() + "Files/Numbers/0.mp3");

Note: If you are trying to make your method soundplay so as to stop previous audio and play the new audio file, then better would be not to perfom this inside that method

mMediaPlayer = new MediaPlayer();

Place this in constructor. Otherwise each time you invoke s.soundplay(path) you will hear previous audio plus the new audio. ie,Previous one will not be stopped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM