Android Tutorial: How to create a media player reading from raw resources
// <![CDATA[ $.SyntaxHighlighter.init( ); // ]]>
Hi Guys, so today I want to show really briefly how to create a media player reading a song (or songs) from raw resources. Ok! So let's start:
1. First of all, I have followed the tutorial shown in AndroidHive http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
So your first task is to set all the project as it's shown in there. I really recommend AndroidHive from Ravi Tamada, is a great point to start Android developing.
2. Once you have it, you can customize it for your own purposes. For instance, in my case I didn't need to read from the memory card my songs but from the raw resources so, mainly the change you need to add is:
//This is the file: SongsManager.java, certain changes will be done in this class package com.androidhive.musicplayer; import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.HashMap; public class SongsManager { // Create the array of Hashmaps here (songsList) private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); // Constructor public SongsManager(){ } //Here you will read each song and return a playlist :-) public ArrayList<HashMap<String, String>> getPlayList(){ //lets suppose we have 4 songs in here. Yes I do love Keane <3 HashMap<String, String> song1 = new HashMap<String, String>(); HashMap<String, String> song2 = new HashMap<String, String>(); HashMap<String, String> song3 = new HashMap<String, String>(); HashMap<String, String> song4 = new HashMap<String, String>(); //first song of the playlist song1.put("songTitle", "Frog Prince"); song1.put("songPath", "android.resource://" + MainActivity.PACKAGE_NAME + "/"+R.raw.frogprince); songsList.add(song1); //second song song2.put("songTitle", "Bedshaped"); song2.put("songPath", "android.resource://" + MainActivity.PACKAGE_NAME + "/"+R.raw.bedshaped); songsList.add(song2); //third song song3.put("songTitle", "Is it any wonder"); song3.put("songPath", "android.resource://" + MainActivity.PACKAGE_NAME + "/"+R.raw.isitanywonder); songsList.add(song3); //finally the fourth song song4.put("songTitle", "Walnut tree"); song4.put("songPath", "android.resource://" + MainActivity.PACKAGE_NAME + "/"+R.raw.walnuttree); songsList.add(song4); return songsList; } //The next part of the code just leave it as it was before :-) /*** Class to filter files which are having .mp3 extension* */ class FileExtensionFilter implements FilenameFilter { boolean accept(File dir, String name) { return (name.endsWith(".mp3") || name.endsWith(".MP3"));} } }//end of songManager class
Let me explain clearly the code of this class... First of all your songManager class is the most important here because it deals with the creation of the Song list, without it you won't be able to read any file. You have to create a raw folder inside your project. So for example if your project has the name "keaneplayer" your raw folder should be "keaneplayer/res/raw" inside of raw folder you should leave your mp3 files that will be read later on.
So in my case I had the following files inside of raw folder:
Yes you can tell I love Keane, because is such an amazing band with awesome instrumental melodies and Tom has such a flawless voice.
Sooooo, going back to the code <3... everytime you call "song.put("songPath", "android.resource://" + MainActivity.PACKAGE_NAME + "/"+R.raw.frogprince);" you refer to the file that was left inside the raw folder.
Let me know if you have any questions after this post :-) As you know, my twitter is @devprincess_ so let me your comments there or in here. I'm glad to answer any question! See you guys!