-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
850b897
commit e9fbf0d
Showing
3 changed files
with
188 additions
and
23 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
app/src/main/java/phone/vishnu/dialogmusicplayer/Audio.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
92
app/src/main/java/phone/vishnu/dialogmusicplayer/AudioUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters