2012-09-10 6 views
3

내 장치에 사전 설치된 "음악"앱에서 3 곡의 재생 목록을 만들었으며 내 앱에서 MediaStore.Audio.Playlists를 성공적으로 쿼리했습니다. EXTERNAL_CONTENT_URI (올바른 재생 목록인지 확인하기 위해 디버그의 이름을 확인한)에서 노래를 재생해야 할 때 해당 ID를 저장했습니다.Android MediaStore 재생 목록이 잘못된 트랙을 반환합니다.

나중에 해당 노래 중 하나를 재생할 때 재생 목록의 노래 수는 정확하지만 재생 목록에 넣은 노래와 다른 트랙을 재생합니다. 다음은 재생 목록에서 트랙을 가져 오는 코드 블록입니다.

참고 : 이것은 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); 
     } 
    } 
} 

답변

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