Skip to content

Commit

Permalink
fix metadata issue
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Feb 26, 2022
1 parent 850b897 commit e9fbf0d
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 23 deletions.
86 changes: 86 additions & 0 deletions app/src/main/java/phone/vishnu/dialogmusicplayer/Audio.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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
+ '}';
}
}
92 changes: 92 additions & 0 deletions app/src/main/java/phone/vishnu/dialogmusicplayer/AudioUtils.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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("<unknown>")) name = extractName(uri);

String artist = cursor.getString(artistColumn);

if (artist == null || artist.equals("<unknown>")) artist = "<Unknown Artist>";

return new Audio(name, artist, d, contentUri);
}
}

return new Audio(extractName(uri), "<Unknown Artist>", Long.parseLong(duration), uri);
}

private static String extractName(Uri uri) {

String[] split = uri.getLastPathSegment().split("/");

if (split.length == 0) split = new String[] {"<Unknown Title>"};

return split[split.length - 1];
}
}
33 changes: 10 additions & 23 deletions app/src/main/java/phone/vishnu/dialogmusicplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -199,7 +198,7 @@ private void initTasks(Intent intent) {

imageView.setImageResource(R.drawable.ic_pause);

populateMetaDataTextViews(uri);
populateMetaDataTextViews(uri, mediaPlayer.getDuration());
setTextViewScrollingBehaviour();
}
}
Expand Down Expand Up @@ -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("<Unknown Title>");
fileNameTV.setText("<Unknown Artist>");

} catch (Exception e) {
e.printStackTrace();
}

String[] split = uri.getLastPathSegment().split("/");

if (split.length == 0) split = new String[] {"<Unknown Title>"};

fileNameTV.setText(title == null ? split[split.length - 1] : title);
artistNameTV.setText(artist == null ? "<Unknown Artist>" : artist);
}

public void enableScreenRotation() {
Expand Down

0 comments on commit e9fbf0d

Please sign in to comment.