Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

How to use PlayMusicLib

David Schulte edited this page Jan 26, 2015 · 7 revisions

You can use the PlayMusicLib module in your project. Notice that this module also requires my Framework module which contains general superuser and file functions.

Examples

To use the PlayMusicLib you need to set up a PlayMusicManager. Simply run the startUp() method and the PlayMusicManager will automatically search the required PlayMusic data path, check for root access and connect to the PlayMusic database. If something fails this method will throw an exception (PlayMusicNotFoundException, NoSuperUserException or CouldNotOpenDatabaseException). You need to call the startUp() method at least one time successfully to use the library.

PlayMusicManager playMusicManager = new PlayMusicManager(appContext);

try {
    playMusicManager.startUp();
} catch(PlayMusicNotFoundException e) {
    e.printStackTrace();
} catch(NoSuperUserException e) {
    e.printStackTrace();
} catch(CouldNotOpenDatabaseException e) {
    e.printStackTrace();
}

Load some data

You can use the data sources to read elements from the PlayMusic database. If you want to load one or more albums from PlayMusic you can use the AlbumDataSource like this.

AlbumDataSource albumDataSource = new AlbumDataSource(playMusicManager);

// Load all albums
List<Album> list = albumDataSource.getAll();

You can also modify the data source with setOfflineOnly(boolean) or setSearchKey(String). If you set setOfflineOnly(true) you will only get albums which contains music that is offline available in PlayMusic. You can also filter albums with setSearchKey("Dangerous"). Than you will get a list with all albums that contains "Dangerous" in the album name, in at least one title name or in at least one artist name.

AlbumDataSource albumDataSource = new AlbumDataSource(playMusicManager);

// Set some filter
albumDataSource.setOfflineOnly(true);
albumDataSource.setSearchKey("Dangerous");

// Load all albums which are offline available and contains "Dangerous" 
List<Album> list = albumDataSource.getAll();

You can also use MusicTrackDataSource, PlaylistDataSource or ArtistDataSource to load tracks, playlists or artists. Notice that setOfflineOnly(boolean) won't have any effect to the playlists currently. You will always get all playlists.

If you have a MusicTrackList (parent class or Album, Playlist and Artist) you can simply use getMusicTrackList() to get all tracks inside the list.

Album album = /* Load an album here */;

List<MusicTrack> list = album.getMusicTrackList();

Export a track to sdcard

You can simply export a track by calling exportMusicTrack(MusicTrack, String) from the PlayMusicManager instance.

MusicTrack musicTrack = /* Load a track here */;

// The path on the sdcard
String filename = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/" + musicTrack.getTitle() + ".mp3";

// Export the track
if (playMusicManager.exportMusicTrack(musicTrack, filename)) {
    // Success
} else {
    // Failed
}

Notice that this function will hold the active thread as long as the export is in progress. This can take up to 10 seconds so don't execute this on the main thread.

Using ID3

By default the exporter will automaticly add ID3v1 and ID3v2.3 tags with the album artwork to the MP3 files. You can disable ID3 completely or change the ID3v2 version.

playMusicManager.setID3Enable(true); // Write ID3 
playMusicManager.setID3EnableArtwork(true); // Write album artwork
playMusicManager.setID3EnableFallback(true); // Write ID3v1 fallback
playMusicManager.setID3v2Version(ID3v2Version.ID3v23); // Set the ID3v2 sub version
Clone this wiki locally