From 1cf711001391f4ba59f591451d3191ae04fef182 Mon Sep 17 00:00:00 2001 From: Tom Yan Date: Wed, 4 Apr 2018 17:55:21 +0800 Subject: [PATCH 1/4] MediaPlayerAPI: remove extension pattern check There are way more possibilities and extension doesn't guarantee anything about the file. --- .../java/com/termux/api/MediaPlayerAPI.java | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/termux/api/MediaPlayerAPI.java b/app/src/main/java/com/termux/api/MediaPlayerAPI.java index 04cac4e40..2b942e955 100644 --- a/app/src/main/java/com/termux/api/MediaPlayerAPI.java +++ b/app/src/main/java/com/termux/api/MediaPlayerAPI.java @@ -14,7 +14,6 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; -import java.util.regex.Pattern; /** * API that enables playback of standard audio formats such as: @@ -171,17 +170,6 @@ public void writeResult(PrintWriter out) throws Exception { }); } - /** - * Checks to see if the specified file exists and is a supported media type - * @param file - * @return - */ - protected static boolean isValidMediaFile(File file) { - final String MEDIA_PATTERN = ".3gp|.flac|.mkv|.mp3|.ogg|.wav$"; - Pattern pattern = Pattern.compile(MEDIA_PATTERN); - return pattern.matcher(file.getName()).find(); - } - /** * ----- * Media Command Handlers @@ -210,20 +198,16 @@ public MediaCommandResult handle(MediaPlayer player, Context context, Intent int File mediaFile = new File(intent.getStringExtra("file")); - if (!isValidMediaFile(mediaFile)) { - result.error = "Invalid file: " + mediaFile.getName(); - } else { - if (player.isPlaying()) { - player.stop(); - player.reset(); - } - try { - player.setDataSource(context, Uri.fromFile(mediaFile)); - player.prepareAsync(); - result.message = "Now Playing: " + mediaFile.getName(); - } catch (IOException e) { - result.error = e.getMessage(); - } + if (player.isPlaying()) { + player.stop(); + player.reset(); + } + try { + player.setDataSource(context, Uri.fromFile(mediaFile)); + player.prepareAsync(); + result.message = "Now Playing: " + mediaFile.getName(); + } catch (IOException e) { + result.error = e.getMessage(); } return result; } From f1062d4f169e0acb566ec7d3a68d9c9adae209ef Mon Sep 17 00:00:00 2001 From: Tom Yan Date: Wed, 4 Apr 2018 18:52:14 +0800 Subject: [PATCH 2/4] MediaPlayerAPI: prevent NullPointerException Fixes #154. --- app/src/main/java/com/termux/api/MediaPlayerAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/termux/api/MediaPlayerAPI.java b/app/src/main/java/com/termux/api/MediaPlayerAPI.java index 2b942e955..271c70ee1 100644 --- a/app/src/main/java/com/termux/api/MediaPlayerAPI.java +++ b/app/src/main/java/com/termux/api/MediaPlayerAPI.java @@ -125,7 +125,7 @@ public void onPrepared(MediaPlayer mediaPlayer) { } protected static MediaCommandHandler getMediaCommandHandler(final String command) { - switch (command.toUpperCase()) { + switch (command == null ? "" : command.toUpperCase()) { case "INFO": return infoHandler; case "PLAY": From 5cb22977f8f662b626d885c74905f81b046f738c Mon Sep 17 00:00:00 2001 From: Tom Yan Date: Wed, 4 Apr 2018 18:54:21 +0800 Subject: [PATCH 3/4] MediaPlayerAPI: eliminate pointless toUpperCase() Fixes #154. --- app/src/main/java/com/termux/api/MediaPlayerAPI.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/termux/api/MediaPlayerAPI.java b/app/src/main/java/com/termux/api/MediaPlayerAPI.java index 271c70ee1..cb94d57c6 100644 --- a/app/src/main/java/com/termux/api/MediaPlayerAPI.java +++ b/app/src/main/java/com/termux/api/MediaPlayerAPI.java @@ -125,16 +125,16 @@ public void onPrepared(MediaPlayer mediaPlayer) { } protected static MediaCommandHandler getMediaCommandHandler(final String command) { - switch (command == null ? "" : command.toUpperCase()) { - case "INFO": + switch (command == null ? "" : command) { + case "info": return infoHandler; - case "PLAY": + case "play": return playHandler; - case "PAUSE": + case "pause": return pauseHandler; - case "RESUME": + case "resume": return resumeHandler; - case "STOP": + case "stop": return stopHandler; default: return new MediaCommandHandler() { From 5776b15877b5e849aafbdfb741cd13e6573bd791 Mon Sep 17 00:00:00 2001 From: Tom Yan Date: Wed, 4 Apr 2018 19:33:31 +0800 Subject: [PATCH 4/4] MediaPlayerAPI: check whether playback has been successfully started --- app/src/main/java/com/termux/api/MediaPlayerAPI.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/termux/api/MediaPlayerAPI.java b/app/src/main/java/com/termux/api/MediaPlayerAPI.java index cb94d57c6..b1c181598 100644 --- a/app/src/main/java/com/termux/api/MediaPlayerAPI.java +++ b/app/src/main/java/com/termux/api/MediaPlayerAPI.java @@ -205,7 +205,11 @@ public MediaCommandResult handle(MediaPlayer player, Context context, Intent int try { player.setDataSource(context, Uri.fromFile(mediaFile)); player.prepareAsync(); - result.message = "Now Playing: " + mediaFile.getName(); + if (player.isPlaying()) { + result.message = "Now Playing: " + mediaFile.getName(); + } else { + result.error = "Failed to play: " + mediaFile.getName(); + } } catch (IOException e) { result.error = e.getMessage(); }