diff --git a/app/src/main/java/com/termux/api/MediaPlayerAPI.java b/app/src/main/java/com/termux/api/MediaPlayerAPI.java index 04cac4e40..b1c181598 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: @@ -126,16 +125,16 @@ public void onPrepared(MediaPlayer mediaPlayer) { } protected static MediaCommandHandler getMediaCommandHandler(final String command) { - switch (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() { @@ -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,20 @@ 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(); 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(); + } else { + result.error = "Failed to play: " + mediaFile.getName(); } + } catch (IOException e) { + result.error = e.getMessage(); } return result; }