RSS

The music Code

05 Nov

SongData.java


package com.gilo.gplayer.songsUtils;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Albums;
import android.util.Log;

import com.gilo.gplayer.classes.Album;
import com.gilo.gplayer.classes.Artist;
import com.gilo.gplayer.classes.Song;
 /*
 * This class is instantiated when I need songs at any point on my app.
 * */
public class SongData {

Context context;

//This are the Uri's to the particular contents we want:
 final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //the songs
 final Uri album_uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; //the albums
 final Uri artist_uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI; //the artits

 //This are the columns we want
 final String[] cursor_cols = { MediaStore.Audio.Media._ID,
 MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
 MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA };

final String[] album_cols = { MediaStore.Audio.Albums._ID,
 MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST,
 MediaStore.Audio.Albums.NUMBER_OF_SONGS, MediaStore.Audio.Albums.ALBUM_ART};

 final String[] artist_cols = { MediaStore.Audio.Artists._ID,
 MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS,
 MediaStore.Audio.Artists.NUMBER_OF_TRACKS};

 //Constructor
 public SongData(Context context) {
 this.context = context;
 }

/*
 * This method will return an arraylist containing objects of Song class. Each object has
 * the title of the song, artist, album, duration, album art, and the path on the sd-card
 */
 public ArrayList<Song> GetSongs() {
 final Cursor cursor = context.getContentResolver().query(uri,
 cursor_cols, null, null, MediaStore.Audio.Media.TITLE + " ASC");
 ArrayList<Song> song_list = new ArrayList<Song>();

while (cursor.moveToNext()) {

Song song = new Song();

// get the song info
 final String artist = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
 final String album = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
 final String title = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
 String duration = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
 String path = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

 //correctly set the duration from milliseconds to a samthing like 2:55
 duration = setCorrectDuration(duration);

// this gets the album art
 Cursor cur = context.getContentResolver().query(
 Albums.EXTERNAL_CONTENT_URI,
 new String[] { Albums.ALBUM_ART }, Albums.ALBUM + "=?",
 new String[] { album }, null);
 cur.moveToFirst();
 String album_art = cur.getString(cur
 .getColumnIndex(Albums.ALBUM_ART));

song.setTitle(title);
 song.setArtist(artist);
 song.setAlbum(album);
 song.setDuration(duration);
 song.setAlbumArt(album_art);
 song.setPath(path);

song_list.add(song);

 Log.d("music added", title );
 Log.d("Path", path );
 }

return song_list;
 }
 /*
 * Same as above but returns the albums
 */
public ArrayList<Album> GetAlbums() {
 final Cursor cursor = context.getContentResolver().query(album_uri,
 album_cols, null, null, MediaStore.Audio.Media.ALBUM + " ASC");
 ArrayList<Album> album_list = new ArrayList<Album>();

while (cursor.moveToNext()) {

Album album = new Album();

// get the album info
 final String title = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM));
 final String artist = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Albums.ARTIST));
 final String no_Of_Songs = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS));
 String album_art = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART));

 album.setTitle(title);
 album.setArtist(artist);
 album.setNoSongs(no_Of_Songs);
 album.setAlbumArt(album_art);

album_list.add(album);

 }

return album_list;
 }
 /*
 * Returns the artists
 */
public ArrayList<Artist> GetArtists() {
 final Cursor cursor = context.getContentResolver().query(artist_uri,
 artist_cols, null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
 ArrayList<Artist> artist_list = new ArrayList<Artist>();

while (cursor.moveToNext()) {
 Artist artist = new Artist();

// get the album info
 final String name = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST));
 final String no_Of_Songs = cursor.getString(cursor
 .getColumnIndexOrThrow(MediaStore.Audio.Artists.NUMBER_OF_TRACKS));

 artist.setName(name);

 artist.setSongNo(no_Of_Songs);

 Cursor cur = context.getContentResolver().query(
 Albums.EXTERNAL_CONTENT_URI,
 new String[] { Albums.ALBUM_ART }, Albums.ARTIST + "=?",
 new String[] { name }, null);
 cur.moveToFirst();

 String album_art = cur.getString(cur
 .getColumnIndex(Albums.ALBUM_ART));

 artist.setAlbumArt(album_art);
 artist.setAlbumNo(String.valueOf(cur.getCount()));

 artist_list.add(artist);

}

return artist_list;
}

public String setCorrectDuration(String duration) {
 // TODO Auto-generated method stub
 int time = Integer.valueOf(duration);

 int seconds = time/1000;
 int minutes = seconds/60;
 seconds = seconds % 60;

 if(seconds<10)
 return String.valueOf(minutes) + ":0" + String.valueOf(seconds);

 return String.valueOf(minutes) + ":" + String.valueOf(seconds);
 }
}

Then the Song.java jst as an example:

</pre>
package com.gilo.gplayer.classes;

public class Song {

String title;
String artist;
String album;
String duration;
String album_art;
String path;
// String size;

// the getter methods
public String getTitle() {
return this.title;
}

public String getArtist() {
return this.artist;
}

public String getAlbum() {
return this.album;
}

public String getDuration() {
return this.duration;
}

public String getAlbumArt() {
return this.album_art;
}

public String getPath() {
return this.path;
}

// the setter methods
public void setTitle(String title) {
this.title = title;
}

public void setArtist(String artist) {
this.artist = artist;
}

public void setAlbum(String album) {
this.album = album;
}

public void setDuration(String duration) {
this.duration = duration;
}

public void setAlbumArt(String album_art) {
this.album_art = album_art;
}

public void setPath(String path) {
this.path = path;
}
}
<pre>
Advertisements
 
Leave a comment

Posted by on November 5, 2012 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: