How to Play Music File’s from RAW Folder to a ListView in Android

In this blog post, i’ll  explain how to import & play MP3 files from raw folder to a listview.

Step 1:

Create a new project File -> Android Project. While creating a new project give activity name as MainActivity.

Step 2:

Right click now res folder and Create new folder called raw. Now copy paste few .MP3 files inside it.

Step 3:

Open your activity_main.xml and copy paste this code:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.audio.MainActivity" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>

</RelativeLayout>

Step 4:

Open MainActivity class and copy paste this code:


package com.example.audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

// variable declaration
private ListView mainList;
private MediaPlayer mp;
private final String[] listContent = { "chimes", "chord", "ding", "notify",
"recycle", "ringin", "ringout", "tada" };

private final int[] resID = { R.raw.chimes, R.raw.chord, R.raw.ding,
R.raw.notify, R.raw.recycle, R.raw.ringin, R.raw.ringout,
R.raw.tada };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing variables
mp = new MediaPlayer();
mainList = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
mainList.setAdapter(adapter);

mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
playSong(position);

}
});

}

public void playSong(int songIndex) {
// Play song
mp.reset();// stops any current playing song
mp = MediaPlayer.create(getApplicationContext(), resID[songIndex]);// create's
// new
// mediaplayer
// with
// song.

mp.start(); // starting mediaplayer

}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

That’s it, No Permission is need just run the app and select changing songs.

Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi
Comments
  1. Sahana
    • Sahana
    • Conrad Stanley
  2. manaswini.vemuri@gmail.com
  3. manu
  4. berihun

Leave a Reply

Your email address will not be published. Required fields are marked *