diff --git a/src/org/kreed/vanilla/Song.java b/src/org/kreed/vanilla/Song.java
index c6836d34..82efcea3 100644
--- a/src/org/kreed/vanilla/Song.java
+++ b/src/org/kreed/vanilla/Song.java
@@ -100,6 +100,20 @@ public class Song implements Parcelable {
*/
public int flags;
+ /**
+ * @return true if it's possible to retrieve any songs, otherwise false. For example, false
+ * could be returned if there are no songs in the library.
+ */
+ public static boolean isSongAvailable()
+ {
+ ContentResolver resolver = ContextApplication.getContext().getContentResolver();
+ Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
+ String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
+ Cursor cursor = resolver.query(media, FILLED_PROJECTION, selection, null, null);
+
+ return cursor != null && cursor.getCount() > 0;
+ }
+
/**
* Returns a song randomly selected from all the songs in the Android
* MediaStore.
@@ -149,7 +163,7 @@ public class Song implements Parcelable {
{
this.id = id;
}
-
+
/**
* Initialize the song with the specified id and flags. Call populate to
* fill fields in the song.
diff --git a/src/org/kreed/vanilla/SongTimeline.java b/src/org/kreed/vanilla/SongTimeline.java
index 50481cc5..ce0c915e 100644
--- a/src/org/kreed/vanilla/SongTimeline.java
+++ b/src/org/kreed/vanilla/SongTimeline.java
@@ -273,7 +273,8 @@ public final class SongTimeline {
/**
* Returns the song delta
places away from the current
* position. If there is no song at the given position, a random
- * song will be placed in that position.
+ * song will be placed in that position. Returns null if no songs are
+ * available.
*
* Note: This returns songs based on their position in the playback
* sequence, not necessarily the stored timeline. When repeat is enabled,
@@ -284,6 +285,9 @@ public final class SongTimeline {
*/
public Song getSong(int delta)
{
+ if (!Song.isSongAvailable())
+ return null;
+
ArrayList timeline = mSongs;
Song song;
@@ -332,8 +336,12 @@ public final class SongTimeline {
}
}
- if (song == null || !song.query(false)) {
+ if (song == null)
+ return null;
+
+ if (!song.query(false)) {
song.copy(Song.randomSong());
+
if (song == null || !song.query(false))
return null;
}