Skip to content

Commit

Permalink
use uri directly on media player ;__;
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Feb 21, 2022
1 parent 80baf98 commit 8f0052d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 54 deletions.
62 changes: 22 additions & 40 deletions app/src/main/java/phone/vishnu/dialogmusicplayer/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,36 @@
package phone.vishnu.dialogmusicplayer;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.os.AsyncTask;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

// https://stackoverflow.com/a/65447195/9652621
public class FileUtils {

public static String getFilePath(Context context, Uri uri) {
File destinationFilename =
new File(
context.getFilesDir().getPath()
+ File.separatorChar
+ queryName(context, uri));
try (InputStream ins = context.getContentResolver().openInputStream(uri)) {
createFileFromStream(ins, destinationFilename);
} catch (Exception ex) {
Log.e("Save File", ex.getMessage());
ex.printStackTrace();
public static void clearApplicationData(Context context) {
try {
AsyncTask.execute(() -> clear(context));
} catch (Exception e) {
Log.e("vishnu", "clearApplicationData: " + e);
}
return destinationFilename.getAbsolutePath();
}

public static void createFileFromStream(InputStream ins, File destination) {
try (OutputStream os = new FileOutputStream(destination)) {
byte[] buffer = new byte[4096];
int length;
while ((length = ins.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
} catch (Exception ex) {
Log.e("Save File", ex.getMessage());
ex.printStackTrace();
}
}
private static void clear(Context context) {

String path = context.getFilesDir().getPath();

File file = new File(path);

private static String queryName(Context context, Uri uri) {
Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);
assert returnCursor != null;
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String name = returnCursor.getString(nameIndex);
returnCursor.close();
return name;
if (!file.exists()) return;

String command = "rm -rf " + path;

try {
Runtime.getRuntime().exec(command);
Log.i("vishnu", "clearApplicationData() Deleted: " + path);
} catch (IOException e) {
Log.e("vishnu", "clearApplicationData: " + e);
}
}
}
29 changes: 15 additions & 14 deletions app/src/main/java/phone/vishnu/dialogmusicplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.content.res.Configuration;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -59,6 +60,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
this.setFinishOnTouchOutside(false);

FileUtils.clearApplicationData(getApplicationContext()); // fix for an old mistake ;_;

setScreenWidth();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
Expand Down Expand Up @@ -151,26 +154,22 @@ private void initTasks(Intent intent) {

initViews();

if (Intent.ACTION_VIEW.equals(intent.getAction()) && intent.getData() != null) {
Uri uri = intent.getData();

String path = FileUtils.getFilePath(this, intent.getData());
if (Intent.ACTION_VIEW.equals(intent.getAction()) && uri != null) {

Log.e("vishnu", "initTasks:" + path);
Log.e("vishnu", "initTasks:" + uri);

mediaPlayer = new MediaPlayer();

try {
mediaPlayer.setDataSource(path);
mediaPlayer.setDataSource(this, uri);
mediaPlayer.prepare();
} catch (IOException e) {
Log.e("vishnu", "initTasks -> Path: " + path, e);
Log.e("vishnu", "initTasks -> Uri: " + uri, e);
Toast.makeText(
this,
"Oops! Something went wrong\n\n"
+ e.toString()
+ "\n\n"
+ "Path: "
+ path,
"Oops! Something went wrong\n\n" + e + "\n\n" + "Uri: " + uri,
Toast.LENGTH_LONG)
.show();
}
Expand All @@ -187,7 +186,7 @@ private void initTasks(Intent intent) {

imageView.setImageResource(R.drawable.ic_pause);

populateMetaDataTextViews(path);
populateMetaDataTextViews(uri);
setTextViewScrollingBehaviour();
}
}
Expand Down Expand Up @@ -249,13 +248,15 @@ private String getFormattedTime(long millis) {
return minutes + ":" + secs;
}

private void populateMetaDataTextViews(String path) {
private void populateMetaDataTextViews(Uri uri) {

String title = null, artist = null;

try {

// TODO FIXME
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(path);
mediaMetadataRetriever.setDataSource(uri.getEncodedPath());

title =
mediaMetadataRetriever.extractMetadata(
Expand All @@ -268,7 +269,7 @@ private void populateMetaDataTextViews(String path) {
e.printStackTrace();
}

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

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

Expand Down

0 comments on commit 8f0052d

Please sign in to comment.