Reputation: 501
I'm developing a Chrome OS app and I want to play a song when a song dragged and dropped onto a view in my app. I created a listener and I'm able to get the dragged file with its path. But the problem is; I can't get the information about the dropped song.
Here's what I've tried so far;
class BackingTrackDragListener implements View.OnDragListener {
private final static Uri ARTWORK_URI = Uri.parse("content://media/external/audio/albumart");
BackingTrackViewModel viewModel;
public BackingTrackDragListener(BackingTrackViewModel viewModel) {
super();
this.viewModel = viewModel;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DROP:
if (event.getClipDescription().hasMimeType("application/x-arc-uri-list")) {
MainActivity mainActivity = MyApplication.getMainActivity();
if (mainActivity == null) {
break;
}
mainActivity.requestDragAndDropPermissions(event);
ClipData.Item item = event.getClipData().getItemAt(0);
ContentResolver contentResolver = mainActivity.getContentResolver();
try {
String audioPath = new File(new URI(item.getUri().toString()).getPath()).getCanonicalPath();
Cursor cursor = contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM_ID
},
MediaStore.Audio.Media.DATA + " = ?",
new String[]{audioPath},
"");
String[] str = cursor.getColumnNames(); // prints the column names
final String displayName = cursor.getString(0);
final long duration = cursor.getLong(1);
final long albumId = cursor.getLong(2);
final Uri imageUri = ContentUris.withAppendedId(ARTWORK_URI, albumId);
if (audioPath.endsWith("mp3") && duration > 15000) {
viewModel.setSelectedAudio(new BackingTrackAudio(displayName, imageUri, audioPath));
} else {
break;
}
cursor.close();
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
break;
}
default:
break;
}
return false;
}
}
The actual problem here is I can't read the column data.
I think the problem is occurring when I create the cursor. When I wanted to get a column data from the cursor, my app throws the below error:
Error: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
Error line: final String displayName = cursor.getString(0);
I also tried this using a while loop like cursor.moveToNext() or cursor.moveToFirst()
but no luck.
What I want is to get the track name, album cover, and duration from the mp3 file's path. Is there another way to achieve this? I also tried this solution: Android: How to get audio detail from audio file but this one also didn't work :/ Any suggestion would help, thank you.
UPDATE
I also tried:
Cursor cursor = contentResolver.query(
item.getUri(),
new String[]{
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM_ID
},
MediaStore.Audio.Media.DATA + " = ?",
new String[]{audioPath},
"");
And the error was very similar to the previous one:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
Upvotes: 1
Views: 1536
Reputation: 2141
Here is the code to get details about the song from the mp3 file.
String path = Environment.getExternalStorageDirectory().getPath() + "/Download/music1.mp3";
File file = new File(path);
String fileName = file.getName();
double bytes = file.length();
String fileSize = String.format("%.2f", bytes / 1024) + " kb";
Log.d("tag", fileName + " " + fileSize);
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
Uri uri = Uri.fromFile(file);
mediaMetadataRetriever.setDataSource(MainActivity.this, URI);
String songName = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
String artist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String album = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
String genre = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
String track = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
Log.d(TAG, "SongName: " + songName + " Artist: " + artist + " Album: " + album);
Log.d(TAG, "Genre: " + genre + " Track: " + track);
Upvotes: 1