Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Created March 10, 2020 09:41
Show Gist options
  • Save joe-oli/e4f62e764da0fc2479e3b3cc5a1cb03f to your computer and use it in GitHub Desktop.
Save joe-oli/e4f62e764da0fc2479e3b3cc5a1cb03f to your computer and use it in GitHub Desktop.
randomly play mp3 file in java
/*
* Download from http://www.javazoom.net/javalayer/sources.html
* Unarchive JLayer into source directory
*
* Build with something like:
* javac -classpath JLayer1.0.1/classes/ RandomMP3Player.java
* Run with something like:
* java -classpath .:jl1.0.1.jar RandomMP3Player 10 z.mp3 1.mp3
*
*/
import javazoom.jl.player.*;
import java.io.*;
import java.util.Random;
public class RandomMP3Player {
public static void main(String[] args) {
if(args.length < 2){
System.out.println( "Usage: RandomMP3Player <seconds> <filename1> [<filenameN> ...]" );
System.exit(0);
}
int time = Integer.parseInt(args[0]);
Random rnd = new Random();
while(true){
try{
int sound = rnd.nextInt(args.length-1);
InputStream is = new FileInputStream(args[sound+1]);
Player player = new Player(is);
player.play();
Thread.sleep(rnd.nextInt(time)*1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment