这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions app/src/main/java/com/termux/api/MediaPlayerAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down