Saturday, 2 June 2012

Android Mp3 Player


This Example Helps you to create a simple Mp3 player.Which reades a file from sd card and play it.
try it by using a listview displays list of mp3 songs and select one mp3.which plays on click event of a play button.

package com.Mp3Player;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class Mp3PlayerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
MediaPlayer mediaplayer = new MediaPlayer();
mediaplayer.setDataSource("/sdcard/Music/refisoftmp3song1.mp3");
mediaplayer.prepare();
mediaplayer.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_SHORT).show();
}
}
}



populate the mp3 file from sd card using this code.


File f = new File("/sdcard/Music");
 if (f.isDirectory()) {
String files[] = f.list();
 for (int i = 0; i < files.length; i++) {
 Log.d("", files[i]);
}



Try it All the best



1 comment: