我已经在我的设备上预先安装的“音乐”应用程序中创建了3首歌曲的播放列表,并且在我自己的应用程序中,我已经成功查询了MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI(检查了在调试中命名,以确保它是正确的播放列表)并保存其ID,以便在我需要开始播放歌曲时。
后来当我来播放其中一首歌曲时,播放列表中的歌曲数量是正确的,但它与播放列表中的歌曲播放的歌曲不同。这是从播放列表中获取曲目的代码块。
注意:这是在PhoneGap插件中,因此“this.ctx”是活动。我的测试设备是运行Android 2.2的HTC Desire,如果有任何相关性的话。
Cursor cursor = null;
Uri uri = null;
Log.d(TAG, "Selecting random song from playlist");
uri = Playlists.Members.getContentUri("external", this.currentPlaylistId);
if(uri == null) {
Log.e(TAG, "Encountered null Playlist Uri");
}
cursor = this.ctx.managedQuery(uri, new String[]{Playlists.Members._ID}, null, null, null);
if(cursor != null && cursor.getCount() > 0) {
this.numSongs = cursor.getCount();
Log.d(TAG, "numSongs: "+this.numSongs); // Correctly outputs 3
int randomNum = (int)(Math.random() * this.numSongs);
if(cursor.moveToPosition(randomNum)) {
int idColumn = cursor.getColumnIndex(Media._ID); // This doesn't seem to be giving me a track from the playlist
this.currentSongId = cursor.getLong(idColumn);
try {
JSONObject song = this.getSongInfo();
play(); // This plays whatever song id is in "this.currentSongId"
result = new PluginResult(Status.OK, song);
} catch (Exception e) {
result = new PluginResult(Status.ERROR);
}
}
}
答案 0 :(得分:2)
Playlists.Members._ID
是播放列表中的ID,可用于对播放列表进行排序
Playlists.Members.AUDIO_ID
是音频文件的ID。
所以你的代码应该像
cursor = this.ctx.query(uri, new String[]{Playlists.Members.AUDIO_ID}, null, null, null);
if(cursor != null && cursor.getCount() > 0) {
this.numSongs = cursor.getCount();
Log.d(TAG, "numSongs: "+this.numSongs); // Correctly outputs 3
int randomNum = (int)(Math.random() * this.numSongs);
if(cursor.moveToPosition(randomNum)) {
int idColumn = cursor.getColumnIndex(Playlists.Members.AUDIO_ID); // This doesn't seem to be giving me a track from the playlist
// or just cursor.getLong(0) since it's the first and only column you request
this.currentSongId = cursor.getLong(idColumn);
try {
JSONObject song = this.getSongInfo();
play(); // This plays whatever song id is in "this.currentSongId"
result = new PluginResult(Status.OK, song);
} catch (Exception e) {
result = new PluginResult(Status.ERROR);
}
}
}