import java.io.*; import java.io.IOException; import java.lang.Runtime;
import java.util.Random;
import java.nio.file.Files; import java.nio.file.Paths;
public class AudioPlayer {
public static final String DIRECTORY_CONTAINING_ALL_SONGS = "/home/x/songs/"; public static void e(int i) { System.out.print(i); } public static void newline() { System.out.println(); } public static void e(String i) { System.out.print(i); } public static void main(String[] args) { e( "Playing songs from the directory `"+ DIRECTORY_CONTAINING_ALL_SONGS+ "` next.\n\n" ); do_play_the_songs(); } /* * do_play_the_songs() */ public static void do_play_the_songs() { File directory_containing_all_songs = new File(DIRECTORY_CONTAINING_ALL_SONGS); File[] array_files = directory_containing_all_songs.listFiles(); Random r = new Random(); int random_number = r.nextInt(array_files.length); File this_file = array_files[random_number]; if (this_file.isFile()) { /* Get the path of the given file f */ String path = this_file.getPath(); e( "Next playing this song:\n\n "+path+"\n" ); run_external_command_for_this_file(path); } } public static void printResults(Process process) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = ""; while ((line = reader.readLine()) != null) { System.out.println(line); } } /* * run_external_command_for_this_file() */ public static void run_external_command_for_this_file(String path) { String full_path = "mpv "+path; /* Show to the user what will be done. */ newline(); e(full_path); newline();
Process process = Runtime.getRuntime()
.exec(full_path); //.exec("sh -c ls", null, new File("Pathname")); for non-Windows users
printResults(process);
/* assert exitCode == 0; */
/*
try { Runtime rt = Runtime.getRuntime(); Process ps = rt.exec(full_path); } catch(IOException error) { e("An error happened.\n"); error.printStackTrace(); }
*/
}
}