Fix: can't play media files from file browser
When media file is selected from various file browers, the uri sent to app might be different scheme like file or content. Fix is to query the media file based on different scheme.
This commit is contained in:
parent
d5bf0b58d2
commit
d411b33983
2 changed files with 31 additions and 11 deletions
|
|
@ -2,11 +2,14 @@ package com.kabouzeid.gramophone.helper;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.net.Uri;
|
||||
import android.os.IBinder;
|
||||
import android.provider.DocumentsContract;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
|
@ -17,6 +20,7 @@ import com.kabouzeid.gramophone.loader.SongLoader;
|
|||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.service.MusicService;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
import java.util.WeakHashMap;
|
||||
|
|
@ -374,17 +378,34 @@ public class MusicPlayerRemote {
|
|||
return -1;
|
||||
}
|
||||
|
||||
public static void playFile(String path) {
|
||||
if (musicService != null) {
|
||||
ArrayList<Song> songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
musicService,
|
||||
MediaStore.Audio.AudioColumns.DATA + "=?",
|
||||
new String[]{path}
|
||||
));
|
||||
if (!songs.isEmpty()) {
|
||||
public static void playFromUri(Uri uri) {
|
||||
if (musicService != null && uri.getScheme() != null) {
|
||||
ArrayList<Song> songs = null;
|
||||
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
|
||||
String songId = null;
|
||||
if (uri.getAuthority().equals("com.android.providers.media.documents")) {
|
||||
songId = DocumentsContract.getDocumentId(uri).split(":")[1];
|
||||
} else if (uri.getAuthority().equals("media")) {
|
||||
songId = uri.getLastPathSegment();
|
||||
}
|
||||
if (songId != null) {
|
||||
songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
musicService,
|
||||
MediaStore.Audio.AudioColumns._ID + "=?",
|
||||
new String[]{songId}
|
||||
));
|
||||
}
|
||||
} else if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
|
||||
songs = SongLoader.getSongs(SongLoader.makeSongCursor(
|
||||
musicService,
|
||||
MediaStore.Audio.AudioColumns.DATA + "=?",
|
||||
new String[]{new File(uri.getPath()).getAbsolutePath()}
|
||||
));
|
||||
}
|
||||
if (songs != null && !songs.isEmpty()) {
|
||||
openQueue(songs, 0, true);
|
||||
} else {
|
||||
//TODO the file is not listed in the media store
|
||||
//TODO uri is not listed in the media store
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,6 @@ import com.kabouzeid.gramophone.util.PreferenceUtil;
|
|||
import com.kabouzeid.gramophone.util.Util;
|
||||
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import butterknife.BindView;
|
||||
|
|
@ -299,7 +298,7 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
}
|
||||
|
||||
if (uri != null && uri.toString().length() > 0) {
|
||||
MusicPlayerRemote.playFile(new File(uri.getPath()).getAbsolutePath());
|
||||
MusicPlayerRemote.playFromUri(uri);
|
||||
handled = true;
|
||||
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
|
||||
final int id = (int) parseIdFromIntent(intent, "playlistId", "playlist");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue