Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to play audio file from server in android

Tags:

android

I want to download audio file from url and play that audio file in my device.how to implement this concept in my application.please help me

Thanks Friends

like image 310
John Avatar asked Sep 16 '11 06:09

John


People also ask

How do I play audio files on Android?

To play audio or video files in Android, the Android multimedia framework includes the support of MediaPlayer APIs. So, by using MediaPlayer APIs, you can play audio/video files from your Android filesystem or play files from your Application's resource file or even you can stream audio/video files just like Spotify.

How do I play music from URL on Android?

String fileUrl = "http://192.168.1.131/myproject/songs/xyz"; String url = "http://myserver/songs/xyz"; //(myserver -> A remote server) mVideoView. setVideoURI(Uri. parse(fileUrl)); mVideoView. requestFocus();

How do I select audio in Android programmatically?

So, to pick an audio file I'm using following code: Intent intent = new Intent(); intent. setType("audio/*"); intent. setAction(Intent.


2 Answers

to play audio file from server try this

  try {
    MediaPlayer player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setDataSource("http://xty/MRESC/images/test/xy.mp3"
            );
    player.prepare();
                player.start();

} catch (Exception e) {
    // TODO: handle exception
}

and if you want to download a .mp3 file form server then try this..

    private class DownloadFile extends AsyncTask<String, Integer, String>{
    @Override
    protected String doInBackground(String... url) {
    int count;

    try {

    URL url = new URL("url of your .mp3 file");
    URLConnection conexion = url.openConnection();
    conexion.connect();
    // this will be useful so that you can show a tipical 0-100% progress bar
    int lenghtOfFile = conexion.getContentLength();

    // downlod the file
    InputStream input = new BufferedInputStream(url.openStream());
    OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3");

    byte data[] = new byte[1024];

    long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        // publishing the progress....
        publishProgress((int)(total*100/lenghtOfFile));
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    input.close();
} catch (Exception e) {}
return null;
}

also in your manifest file use this permission..

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
like image 62
user370305 Avatar answered Oct 20 '22 14:10

user370305


Try below code:-

private void PlayFile() {
    try {
        mp.reset();
        Uri uri = Uri.parse("http://soundcloud.com/storynory/the-valentine-witch-mp3/download.mp3");
        mp.setDataSource(this, uri); // "this" refers to context
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

If you are required "Download MP3" code then i will post that code also.

like image 43
Dipak Keshariya Avatar answered Oct 20 '22 13:10

Dipak Keshariya