Tuesday, December 14, 2010

Flash MP3 Player

This post demonstrates how the Android MediaStore can be used to play MP3 files directly from the device. As player front-end the Flash MP3 Player is used.

For those of you not interested in the code, there is a ready to install ZIP file at the end of the post.

Flash MP3 Player


All the files are mainly taken directly from the flashmp3player.org website.

Three changes were made:
  1. In the index.html the reference to flashmp3player.php has been changed to  flashmp3player.xhtml.
  2. The file flashmp3player.xhtml has been implemented.
  3. The file get_file.xhtml has been copied from the PAW app directory and the PAW access control has been removed. This file provides access to files that are not located within the html root directory of the server. This is a potential security risk, so make sure to protect the flashmp3player folder with a password.

The implementation of flashmp3player.xhtml uses the MediaStore to extract the information about artist, title and file and constructs a XML file for the Flash MP3 Player.

An example XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<playlist>
<song id="120"  title="Beat The Bastards" artist="Accept" src="get_file.xhtml?file=%2Fmnt%2Fsdcard%2FMusic%2FAccept%20-%20Blood%20Of%20The%20Nations%2F01%20-%20Beat%20The%20Bastards.mp3" />
<song id="121"  title="Teutonic Terror" artist="Accept" src="get_file.xhtml?file=%2Fmnt%2Fsdcard%2FMusic%2FAccept%20-%20Blood%20Of%20The%20Nations%2F02%20-%20Teutonic%20Terror.mp3" />
<song id="122"  title="The Abyss" artist="Accept" src="get_file.xhtml?file=%2Fmnt%2Fsdcard%2FMusic%2FAccept%20-%20Blood%20Of%20The%20Nations%2F03%20-%20The%20Abyss.mp3" />
<song id="123"  title="Blood Of The Nations" artist="Accept" src="get_file.xhtml?file=%2Fmnt%2Fsdcard%2FMusic%2FAccept%20-%20Blood%20Of%20The%20Nations%2F04%20-%20Blood%20Of%20The%20Nations.mp3" />
</playlist>

Below is the code of the flashmp3player.xhtml file:
<bsh>
import android.net.Uri;

uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

String[] tags = new String[] {
android.provider.MediaStore.Audio.Media._ID,
android.provider.MediaStore.Audio.Media.TITLE,
android.provider.MediaStore.Audio.Media.ARTIST,
android.provider.MediaStore.Audio.Media.ALBUM,
android.provider.MediaStore.Audio.Media.DATA
};

service = server.props.get("serviceContext");

cursor = service.getContentResolver().query(uri, tags, null, null, android.provider.MediaStore.Audio.Media.ARTIST + "," + android.provider.MediaStore.Audio.Media.ALBUM);

print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
print("<playlist>");
cursor.moveToFirst();

do{

  id = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media._ID)).replaceAll("\"", "");
  artist = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.ARTIST)).replaceAll("\"", "");
  title = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.TITLE)).replaceAll("\"", "");
  file = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA));
  file = "get_file.xhtml?file=" + Uri.encode(file);

  if(file.endsWith(".mp3")) { 
 print("<song id=\"" + id + "\"  title=\"" + title + "\" artist=\"" + artist + "\" src=\"" + file + "\" />");
  }
}

while(cursor.moveToNext());
cursor.close();
print("</playlist>");
</bsh>

Downloads:
paw_flashmp3player.zip

Installation:
Unzip into the /sdcard folder of your Android device.
Access http:<ip>:8080/ on your device. The MP3 Player entry should be available in the list of applications.

Attention: There are no access restrictions on the installation directory!
So use this only for a short period of time or password protect the directory by defining a password (PAW Menu -> Server -> Directory Protection).

Uninstall:
Delete the folder /sdcard/paw/html/flashmp3player and the file /sdcard/paw/webconf/apps/x02_flashmp3player.conf.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.