diff --git a/app/src/main/java/phone/vishnu/dialogmusicplayer/Audio.java b/app/src/main/java/phone/vishnu/dialogmusicplayer/Audio.java new file mode 100644 index 0000000..49bce22 --- /dev/null +++ b/app/src/main/java/phone/vishnu/dialogmusicplayer/Audio.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2021 - 2022 Vishnu Sanal. T + * + * This file is part of DialogMusicPlayer. + * + * DialogMusicPlayer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package phone.vishnu.dialogmusicplayer; + +import android.net.Uri; + +public class Audio { + + private String name, artist; + private long duration; + private Uri uri; + + public Audio() {} + + public Audio(String name, String artist, long duration, Uri uri) { + this.name = name; + this.artist = artist; + this.duration = duration; + this.uri = uri; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public Uri getUri() { + return uri; + } + + public void setUri(Uri uri) { + this.uri = uri; + } + + @Override + public String toString() { + return "Audio{" + + "name='" + + name + + '\'' + + ", artist='" + + artist + + '\'' + + ", duration=" + + duration + + ", uri=" + + uri + + '}'; + } +} diff --git a/app/src/main/java/phone/vishnu/dialogmusicplayer/AudioUtils.java b/app/src/main/java/phone/vishnu/dialogmusicplayer/AudioUtils.java new file mode 100644 index 0000000..d647ef7 --- /dev/null +++ b/app/src/main/java/phone/vishnu/dialogmusicplayer/AudioUtils.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2021 - 2022 Vishnu Sanal. T + * + * This file is part of DialogMusicPlayer. + * + * DialogMusicPlayer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package phone.vishnu.dialogmusicplayer; + +import android.content.ContentUris; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build; +import android.provider.MediaStore; + +public class AudioUtils { + + public static Audio getMetaData(Context context, String duration, Uri uri) { + + try (Cursor cursor = + context.getApplicationContext() + .getContentResolver() + .query( + Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q + ? MediaStore.Audio.Media.getContentUri( + MediaStore.VOLUME_EXTERNAL) + : MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + new String[] { + MediaStore.Video.Media._ID, + MediaStore.Video.Media.DISPLAY_NAME, + MediaStore.Video.Media.ARTIST, + MediaStore.Video.Media.DURATION, + }, + MediaStore.Video.Media.DURATION + " = ?", + new String[] {duration}, + null)) { + + int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID); + + int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME); + + int durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION); + + int artistColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST); + + //noinspection LoopStatementThatDoesntLoop + while (cursor.moveToNext()) { + + long id = cursor.getLong(idColumn); + + Uri contentUri = + ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id); + + int d = cursor.getInt(durationColumn); + + String name = cursor.getString(nameColumn); + + if (name == null || name.equals("")) name = extractName(uri); + + String artist = cursor.getString(artistColumn); + + if (artist == null || artist.equals("")) artist = ""; + + return new Audio(name, artist, d, contentUri); + } + } + + return new Audio(extractName(uri), "", Long.parseLong(duration), uri); + } + + private static String extractName(Uri uri) { + + String[] split = uri.getLastPathSegment().split("/"); + + if (split.length == 0) split = new String[] {""}; + + return split[split.length - 1]; + } +} diff --git a/app/src/main/java/phone/vishnu/dialogmusicplayer/MainActivity.java b/app/src/main/java/phone/vishnu/dialogmusicplayer/MainActivity.java index 925cbe6..0be8344 100644 --- a/app/src/main/java/phone/vishnu/dialogmusicplayer/MainActivity.java +++ b/app/src/main/java/phone/vishnu/dialogmusicplayer/MainActivity.java @@ -24,7 +24,6 @@ import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; -import android.media.MediaMetadataRetriever; import android.media.MediaPlayer; import android.net.Uri; import android.os.Build; @@ -199,7 +198,7 @@ private void initTasks(Intent intent) { imageView.setImageResource(R.drawable.ic_pause); - populateMetaDataTextViews(uri); + populateMetaDataTextViews(uri, mediaPlayer.getDuration()); setTextViewScrollingBehaviour(); } } @@ -261,37 +260,25 @@ private String getFormattedTime(long millis) { return minutes + ":" + secs; } - private void populateMetaDataTextViews(Uri uri) { - - String title = null, artist = null; + private void populateMetaDataTextViews(Uri uri, long millis) { try { - MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); + Audio audio = AudioUtils.getMetaData(this, String.valueOf(millis), uri); - String s = uri.getPath().replace(uri.getPath().split("/")[1], ""); + Log.e("vishnu", "populateMetaDataTextViews: " + audio); - Log.e("vishnu", "populateMetaDataTextViews: " + s); + fileNameTV.setText(audio.getName()); + artistNameTV.setText(audio.getArtist()); - mediaMetadataRetriever.setDataSource(s); + } catch (Exception e) { + Log.e("vishnu", "populateMetaDataTextViews: " + e); - title = - mediaMetadataRetriever.extractMetadata( - MediaMetadataRetriever.METADATA_KEY_TITLE); - artist = - mediaMetadataRetriever.extractMetadata( - MediaMetadataRetriever.METADATA_KEY_ARTIST); + fileNameTV.setText(""); + fileNameTV.setText(""); - } catch (Exception e) { e.printStackTrace(); } - - String[] split = uri.getLastPathSegment().split("/"); - - if (split.length == 0) split = new String[] {""}; - - fileNameTV.setText(title == null ? split[split.length - 1] : title); - artistNameTV.setText(artist == null ? "" : artist); } public void enableScreenRotation() {