How to get all .mp3 files from raw folder to custom array listview and play that song?

3.7k views Asked by At

I am developing one app that can play .mp3 file.So,In this app i have created one custom listview adapter for populating the .mp3 files from raw folder to listview and created one model class and in my raw folder i have 20 .mp3 files.So, I want to add this all raw folder .mp3 files to listview in each row.after that when user click that particular listview item at that time play that song given position.

Note: I am not importing that .mp3 files from SDCARD.

Here this my adapter

public class RingtoneAdapter extends ArrayAdapter<RingtoneModel> {

private List<RingtoneModel> planetList;
private Context context;


public RingtoneAdapter(List<RingtoneModel> planetList, Context ctx) {
    super(ctx, R.layout.ringtone_row_layout, planetList);
    this.planetList = planetList;
    this.context = ctx;
}

public int getCount() {
    return planetList.size();
}

public RingtoneModel getItem(int position) {
    return planetList.get(position);
}

public long getItemId(int position) {
    return planetList.get(position).hashCode();
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    PlanetHolder holder = new PlanetHolder();


    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.ringtone_row_layout, null);

        TextView tv = (TextView) v.findViewById(R.id.name);

        holder.planetNameView = tv;

        v.setTag(holder);
    }
    else 
        holder = (PlanetHolder) v.getTag();

    RingtoneModel p = planetList.get(position);
    holder.planetNameView.setText(p.getName());


    return v;
}


private static class PlanetHolder {
    public TextView planetNameView;
}


}

This my model class

public class RingtoneModel {

private String name;
private Integer distance;
private String descr;
private int idImg;

public RingtoneModel(String name) {
    this.name = name;

}

public RingtoneModel(String name, String descr) {
    this.name = name;
    this.descr = descr;
}

public RingtoneModel(String name, Integer distance, String descr, int idImg) {
    this.name = name;
    this.descr = descr;
    this.idImg = idImg;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Integer getDistance() {
    return distance;
}
public void setDistance(Integer distance) {
    this.distance = distance;
}
public int getIdImg() {
    return idImg;
}
public void setIdImg(int idImg) {
    this.idImg = idImg;
}
}

And this is my MainActivity

public class MainActivity extends AppCompatActivity {


List<RingtoneModel> ringtoneList = new ArrayList<RingtoneModel>();
RingtoneAdapter ringAdapter;
ListView ringtoneListMain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    initRingtoneList();

    ringtoneListMain = (ListView)findViewById(R.id.rintoneList);

    ringAdapter = new RingtoneAdapter(ringtoneList,this);

    ringtoneListMain.setAdapter(ringAdapter);

    ringtoneListMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}

private void initRingtoneList() {

    ringtoneList.add(new RingtoneModel("xyz"));
    ringtoneList.add(new RingtoneModel("Venus"));
    ringtoneList.add(new RingtoneModel("Mars"));
    ringtoneList.add(new RingtoneModel("Jupiter"));
    ringtoneList.add(new RingtoneModel("Saturn"));
    ringtoneList.add(new RingtoneModel("Uranus"));
    ringtoneList.add(new RingtoneModel("Neptune"));
}
}

So,If anyone know how i can do all this then please help me...

1

There are 1 answers

0
rafsanahmad007 On

First get the item index in the ListView when users click a button in ListView.

    View parentRow = (View) v.getParent();
    ListView listView = (ListView) parentRow.getParent();
    final int position = listView.getPositionForView(parentRow);

After you got the exact position index, find the English word audio file and create an URI for that audio file.

Uri mp3 = Uri.parse("android.resource://"
          + context.getPackageName() + "/raw/"
          + datas.get(position).word);

After you have the audio file URI, you can instantiate the MediaPlayer class and play the audio file in Android.

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(context, mp3);
    mediaPlayer.prepare(); // might take long! (for buffering, etc)
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(onCompletionListener);

When the music stops, release the MediaPlayer instance immediately so that it will not consume lot of resources.

 private OnCompletionListener onCompletionListener = new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        mediaPlayer.release();
        mediaPlayer = null;
    }
};