implement simple m3u writer
This commit is contained in:
1 parent
eba6acabc2
commit
4715bd3c31
3 files changed
+138
-3
No files matched your search
@@ -506,7 +506,7 @@ public final class PlaybackService extends Service
|
||||
mRemoteControlClient = new RemoteControl().getClient(this);
|
||||
mRemoteControlClient.initializeRemote();
|
||||
|
||||
mPlaylistObserver = new PlaylistObserver();
|
||||
mPlaylistObserver = new PlaylistObserver(this);
|
||||
|
||||
mLooper = thread.getLooper();
|
||||
mHandler = new Handler(mLooper, this);
|
||||
|
||||
@@ -28,6 +28,7 @@ import ch.blinkenlights.android.medialibrary.MediaLibrary;
|
||||
import android.content.Context;
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -72,6 +73,30 @@ public class Playlist {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of given playlist id.
|
||||
*
|
||||
* @param context the context to use
|
||||
* @param id the playlist id to look up
|
||||
* @return name of the playlist, may be null
|
||||
*/
|
||||
public static @Nullable String getPlaylist(Context context, long id)
|
||||
{
|
||||
String name = null;
|
||||
final String[] projection = { MediaLibrary.PlaylistColumns.NAME };
|
||||
final String selection = MediaLibrary.PlaylistColumns._ID+"=?";
|
||||
final String[] selectionArgs = { new Long(id).toString() };
|
||||
Cursor cursor = MediaLibrary.queryLibrary(context, MediaLibrary.TABLE_PLAYLISTS, projection, selection, selectionArgs, null);
|
||||
|
||||
if (cursor != null) {
|
||||
if (cursor.moveToNext())
|
||||
name = cursor.getString(0);
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new playlist with the given name. If a playlist with the given
|
||||
* name already exists, it will be overwritten.
|
||||
|
||||
@@ -20,6 +20,8 @@ package ch.blinkenlights.android.vanilla;
|
||||
import ch.blinkenlights.android.medialibrary.MediaLibrary;
|
||||
import ch.blinkenlights.android.medialibrary.LibraryObserver;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
@@ -27,10 +29,21 @@ import android.os.Message;
|
||||
import android.os.Process;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
public class PlaylistObserver implements Handler.Callback {
|
||||
/**
|
||||
* Timeout to coalesce duplicate messages.
|
||||
*/
|
||||
private final static int COALESCE_EVENTS_DELAY_MS = 280;
|
||||
/**
|
||||
* Context to use.
|
||||
*/
|
||||
private Context mContext;
|
||||
/**
|
||||
* Handler thread used to perform playlist sync.
|
||||
*/
|
||||
@@ -41,7 +54,8 @@ public class PlaylistObserver implements Handler.Callback {
|
||||
private File mPlaylists = new File(Environment.getExternalStorageDirectory(), "Playlists");
|
||||
|
||||
|
||||
public PlaylistObserver() {
|
||||
public PlaylistObserver(Context context) {
|
||||
mContext = context;
|
||||
// Create thread which will be used for background work.
|
||||
HandlerThread handlerThread = new HandlerThread("PlaylistWriter", Process.THREAD_PRIORITY_LOWEST);
|
||||
handlerThread.start();
|
||||
@@ -58,11 +72,103 @@ public class PlaylistObserver implements Handler.Callback {
|
||||
MediaLibrary.unregisterLibraryObserver(mObserver);
|
||||
}
|
||||
|
||||
private final static int MSG_SYNC_M3U = 1;
|
||||
@Override
|
||||
public boolean handleMessage(Message message) {
|
||||
switch (message.what) {
|
||||
case MSG_SYNC_M3U:
|
||||
Long id = (Long)message.obj;
|
||||
if (id == -1) {
|
||||
syncAllM3u();
|
||||
} else {
|
||||
syncM3uPlaylist(id);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid message type received");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports a single playlist ad M3U(8).
|
||||
*
|
||||
* @param id the playlist id to export
|
||||
*/
|
||||
private void syncM3uPlaylist(long id) {
|
||||
String name = Playlist.getPlaylist(mContext, id);
|
||||
|
||||
if (name == null) {
|
||||
cleanupOrphanedM3u();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mPlaylists.isDirectory())
|
||||
mPlaylists.mkdir();
|
||||
Log.v("VanillaMusic", "Dumping "+getFileFromName(name));
|
||||
|
||||
PrintWriter pw = null;
|
||||
QueryTask query = MediaUtils.buildPlaylistQuery(id, Song.FILLED_PLAYLIST_PROJECTION);
|
||||
Cursor cursor = query.runQuery(mContext);
|
||||
try {
|
||||
if (cursor != null) {
|
||||
pw = new PrintWriter(getFileFromName(name));
|
||||
pw.println("#EXTM3U");
|
||||
while (cursor.moveToNext()) {
|
||||
final String path = cursor.getString(1);
|
||||
final String title = cursor.getString(2);
|
||||
final String artist = cursor.getString(3);
|
||||
final long duration = cursor.getLong(7);
|
||||
|
||||
pw.printf("#EXTINF:%d,%s - %s%n", (duration/1000), artist, title);
|
||||
pw.println(path);
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.v("VanillaMusic", "IOException while writing:", e);
|
||||
} finally {
|
||||
if (pw != null) pw.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps all playlist to stable storage.
|
||||
*/
|
||||
private void syncAllM3u() {
|
||||
Log.v("VanillaMusic", "M3U: Dumping all playlists");
|
||||
cleanupOrphanedM3u();
|
||||
|
||||
Cursor cursor = Playlist.queryPlaylists(mContext);
|
||||
if (cursor != null) {
|
||||
while(cursor.moveToNext()) {
|
||||
final long id = cursor.getLong(0);
|
||||
syncM3uPlaylist(id);
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks our playlists directory for files which reference
|
||||
* non-existing playlists and removes them.
|
||||
*/
|
||||
private void cleanupOrphanedM3u() {
|
||||
Log.v("VanillaMusic", "Implement me!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a file object for given playlist name
|
||||
*
|
||||
* @param name name of playlist to use.
|
||||
* @return file object for given name
|
||||
*/
|
||||
private File getFileFromName(String name) {
|
||||
//Fixme: check for m3u8 and remove invalid chars.
|
||||
File f = new File(mPlaylists, name +".m3u8");
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Library observer callback which notifies us about media library
|
||||
* events.
|
||||
@@ -70,7 +176,11 @@ public class PlaylistObserver implements Handler.Callback {
|
||||
private final LibraryObserver mObserver = new LibraryObserver() {
|
||||
@Override
|
||||
public void onChange(LibraryObserver.Type type, long id, boolean ongoing) {
|
||||
Log.v("VanillaMusic", "onChange type = "+type+", id = "+id+" ongoing = "+ongoing);
|
||||
if (type != LibraryObserver.Type.PLAYLIST || ongoing)
|
||||
return;
|
||||
|
||||
mHandler.removeMessages(MSG_SYNC_M3U, id);
|
||||
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SYNC_M3U, id), COALESCE_EVENTS_DELAY_MS);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user