how to play audio file in android
Asked Answered
C

6

64

I have a mp3 file in my android mobile, lets it's a xyz.mp3 somewhere in my sdcard. How to play it through my application?

Casarez answered 3/9, 2011 at 7:18 Comment(1)
You may also put xyz.mp3 into res/raw/ and then reference it via R.raw.xyz. However, then you have two choices: MediaPlayer and SoundPool. For efficient memory management, you should use a library to work with those classes: github.com/delight-im/Android-AudioCashman
C
125

Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio:

 public void audioPlayer(String path, String fileName){
    //set up MediaPlayer    
    MediaPlayer mp = new MediaPlayer();

    try {
        mp.setDataSource(path + File.separator + fileName);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Catiline answered 3/9, 2011 at 7:23 Comment(6)
for GUI You have to go for yourself and take help of images and all through layout.Annettannetta
"what about the GUI" LOLIntegral
how to get path and file name??Captivate
You should never use "/". You should always use File.separator instead.Sportswear
does audio gets mute if incoming call comes?Crypto
The kind of things I enjoy. It worked for the first test, and as I used to have a full path which included a name I did my code this way public void audioPlayer(String path){ //set up MediaPlayer MediaPlayer mp = new MediaPlayer(); try { mp.setDataSource(path ); mp.prepare(); mp.start(); } catch (Exception e) { e.printStackTrace(); } }Antarctic
M
18

If the audio is in the local raw resource:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

To play from a URI available locally in the system:

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
Miran answered 22/2, 2019 at 22:0 Comment(0)
T
12

@Niranjan, If you are using a raw file from res/raw folder, ie., reading a file stored inside the project, we can use:

mediaplayer.setDataSource(context, Uri.parse("android.resource://urpackagename/res/raw/urmp3name");

If you have to use from SD card:

 MediaPlayer mediaPlayer = new MediaPlayer();
 File path = android.os.Environment.getExternalStorageDirectory();
 mediaPlayer.setDataSource(path + "urmp3filename");

See this related question: MediaPlayer issue between raw folder and sdcard on android

Taber answered 16/2, 2015 at 4:6 Comment(4)
If you let me know why -1, I will learn whats wrong with the above answer?Taber
Probably the -1 is because you posted an answer to the original question (i.e. how to play an audio file); if you wanted to reply to Niranjan's comment, you should have added a comment yourself, under Lalit's answerHarrisharrisburg
Hmmm......They should have told me that in the comment, I would have changed it......anyway, thanks for letting me know........Taber
I'd rather see: try { mp.setDataSource(context, Uri.parse("android.resource://" + context.getPackageName() +"/res/raw/urmp3name")); } catch (IOException e) { e.printStackTrace(); }Betterment
D
4
    public class MainActivity extends Activity implements OnClickListener {
    Button play;
    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        play=(Button)findViewById(R.id.button1);
        play.setOnClickListener(this);

    }
    @Override
    public void onClick(View arg0)
    {
        mp=MediaPlayer.create(getApplicationContext(),R.raw.song);// the song is a filename which i have pasted inside a folder **raw** created under the **res** folder.//
        mp.start();


    }

    @Override
    protected void onDestroy() {
        mp.release();
        super.onDestroy();
    }

}
Denizen answered 20/7, 2017 at 10:54 Comment(0)
A
1

The replay from https://stackoverflow.com/users/726863/lalit-poptani is great one, it worked the first time, but as I used to have the full path of the file, I did it this way

public void audioPlayer(String path){
        //set up MediaPlayer
        MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource(path );
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Credit to http://www.helloandroid.com/tutorials/how-play-video-and-audio-android

Antarctic answered 13/11, 2021 at 11:36 Comment(0)
M
0

If the audio is in the local raw resource:

 // no need to call prepare(); create() does that for you
        try {
            if (mp!=null)
            if (mp.isPlaying()) {
                mp.stop();
                mp.release();
            }
            mp = MediaPlayer.create(this, R.raw.sound_file_1);
            mp.start();

        } catch(Exception e) {
            e.printStackTrace();
        }

witch sound_file_1 is m4a file.

Muns answered 20/6, 2024 at 10:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.