2012-09-10 36 views
3

我已經在我的設備上預裝的「Music」應用程序中創建了3首歌曲的播放列表,並且在我自己的應用程序中已成功查詢MediaStore.Audio.Playlists。 EXTERNAL_CONTENT_URI(在調試中檢查名稱以確保它是正確的播放列表)並保存其ID,以便當我需要從其播放歌曲時。Android MediaStore播放列表返回錯誤軌跡

後來,當我播放其中一首歌曲時,播放列表中的歌曲數量正確,但播放列表中的歌曲會播放不同的曲目。以下是從播放列表中獲取曲目的代碼塊。

注意:這是在PhoneGap插件中,所以「this.ctx」是Activity。我的測試設備是運行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); 
     } 
    } 
} 

回答

2

Playlists.Members._ID是可以用來排序播放列表

Playlists.Members.AUDIO_ID播放列表內的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); 
     } 
    } 
} 
+1

謝謝,現在的作品治療。 – Liam984

+1

@ Liam984這些ID很混亂:) – zapl