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.
Related Posts
durga chiranjeevi
Latest posts by durga chiranjeevi (see all)
- Sharing Application Data Between Two Android Apps – Part III - April 8, 2015
- Sharing Application Data Between Two Android Apps – Part II - April 8, 2015
- Sharing Application Data Between Two Android Apps – Part I - April 8, 2015
Hello Durga , this is easy and nice but I’m getting error when i create raw folder and paste some music files (.mp3) . Any idea how to resolve it??
Okay i resolved no probs..!!!
Hello Durga
Gosh you’re smart!! Thanks
hello this code is not running actually just it will display in list view and songs are not playing actually on-click is not working in this code please send correct code
please give correct codee but ur exolanation is very niceee
Thank so much