Java Android MediaStore播放列表返回错误曲目
我已经从预装在我的设备上的“音乐”应用程序中创建了一个包含3首歌曲的播放列表,在我自己的应用程序中,我成功地查询了MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI(在调试中检查名称以确保它是正确的播放列表),并保存了它的id,以便我需要从中开始播放歌曲 稍后,当我播放其中一首歌曲时,播放列表中的歌曲计数是正确的,但它播放的曲目与我放入播放列表中的曲目不同。下面是从播放列表中取出曲目的代码块 注意:这是在PhoneGap插件中,因此“This.ctx”是活动。我的测试设备是运行安卓2.2的HTC Desire,如果相关的话Java Android MediaStore播放列表返回错误曲目,java,android,playlist,mediastore,Java,Android,Playlist,Mediastore,我已经从预装在我的设备上的“音乐”应用程序中创建了一个包含3首歌曲的播放列表,在我自己的应用程序中,我成功地查询了MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI(在调试中检查名称以确保它是正确的播放列表),并保存了它的id,以便我需要从中开始播放歌曲 稍后,当我播放其中一首歌曲时,播放列表中的歌曲计数是正确的,但它播放的曲目与我放入播放列表中的曲目不同。下面是从播放列表中取出曲目的代码块 注意:这是在PhoneGap插件中,因此“This.ctx”
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);
}
}
}
Playlists.Members.\u ID
是播放列表中可用于对播放列表进行排序的ID
Playlists.Members.AUDIO\u 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);
}
}
}
@Liam984这些ID非常混乱:)