简体   繁体   中英

Why i am not able to play mp3 file in android?

I have Implemented this code to play the Sound file available in res/drawable/testing123.mp3 file. The code is :

 public void playSound()
    {
        boolean mStartPlaying = true;
        if (mStartPlaying==true) 
        {
//          String word = wordValue.getText().toString();
            String file = Environment.getExternalStorageDirectory().getAbsolutePath();
            String sound = file+"/testing123.mp3";
            //System.out.println("Before Allocation file is: "+file);
            //rePlay.setText("Stop");
            mPlayer = new MediaPlayer();
            try 
            {
                mPlayer.setDataSource(sound);
                mPlayer.prepare();
                mPlayer.start();
            } 
            catch (IOException e) 
            {
                Log.e(LOG_TAG, "prepare() failed");
            }
        } 
        else 
        {
           //   stopPlaying();
            //rePlay.setText("Replay");
            mPlayer.release();
            mPlayer = null;
        }
        mStartPlaying = !mStartPlaying;
    }

But i am not able to play it. The Sound file is in the res/drawable/testing123.mp3

So Please help me, why i am not able to play that file ? Thanks.

In my applications I put audio files in the /res/raw folder. For example I have an mp3 file used for notifications called "notify.mp3" in my raw folder and I can access it by

Uri uri = Uri.parse("android.resource://com.darrenmowat.boothr/" + R.raw.notify);

You could then use

mPlayer.setDataSource(context, uri);

to set the data source.

MediaPlayer player = new MediaPlayer.create(context, URI);
player.start();

This should be enought, just ensure that your URI is correct.

in my case it works.i add external storage permission.dont know whether it is needed.you first have to copy the music file to sdCard.and check ur mplayer is initiated or not.

public void playSound()
    {
        MediaPlayer mPlayer;
        mPlayer=new MediaPlayer();
        boolean mStartPlaying = true;
        if (mStartPlaying==true) 
        {


                String file = Environment.getExternalStorageDirectory().getAbsolutePath();


                try {

                    mPlayer.setDataSource(file+"/30.mp3"); mPlayer.prepare(); mPlayer.start();
                    ;
                    } 
                catch (Exception e) 
                    {e.printStackTrace(); } }
        else { 
     mStartPlaying = !mStartPlaying; }
}

your problem is the file permission.

By default, when writing to an FileOutputStream, you end up with rw-,---,--- type of permission (600). MediaPlayer will not be able to play the file unless you give it "read permission" to "others": at least rw-,---,r-- (604).

I recommend 644 (rw,r,r) and you can use the following command to do so:

Runtime.getRuntime().exec("chmod 644 " + PATH_TO_YOUR_FILE);

You should then be able to play the saved media file

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