1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package fr.iutvalence.m3tplayer;
import java.util.Random;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import javazoom.jl.player.advanced.AdvancedPlayer;
import enumerations.PlayerControl;
public class M3TPlayer implements Runnable{
/**
* The volume of the player.
* Value is between 0 and 100.
*/
private int volume;
/**
* Random playing mod
*/
private boolean randomPlaying;
/**
* The current media
*/
private Media currentMedia;
/**
* The library
*/
private Library library;
/**
* The audio player
*/
private AdvancedPlayer player;
/**
* The state of playing
*/
private boolean isPlaying;
/**
* The position of the music
*/
private int position;
/**
* The random generator
*/
private Random randomGenerator;
/**
* Initializes the player with default values.
* Sets the volume to 100 percent, and the random mode to false, and the identifiant to 0.
*/
public M3TPlayer() {
this.library = new Library();
this.volume = 100;
this.randomPlaying = false;
this.isPlaying = false;
this.position = 0;
if(this.library.isEmpty())
this.currentMedia = null;
else
this.currentMedia = this.library.getMedia(1);
this.player = null;
this.randomGenerator = new Random();
}
/**
* Switchs the random playing to on/off
*/
public void setRandomPlaying(){
// TODO Fix
int maxRandId = this.library.getImportedMusicNumber();
if (this.randomPlaying)
this.randomPlaying = false;
else {
this.randomPlaying = true;
this.currentMedia = this.library.getMedia(this.randomGenerator.nextInt(maxRandId));
}
}
/**
* Allow to stop the music
*/
public void stop(){
this.player.stop();
this.isPlaying = false;
}
public void pause(){
// TODO Check method behavior
Player playing;
try {
playing = new Player(this.currentMedia.getStream());
this.position = playing.getPosition();
this.player.stop();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
/**
* Allow to change the volume of the M3TPlayer
*/
public void changeVolume(){
//TODO method
DataLine.Info info = null;
Clip clip;
try {
clip = (Clip) AudioSystem.getLine(info);
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
double gain = .5D; // number between 0 and 1 (loudest)
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
} catch (LineUnavailableException e) {
// TODO Bloc catch généré automatiquement
e.printStackTrace();
}
}
/**
* Changes the current media to another one.
* @param control <tt>PREVIOUS</tt> to play the previous media,
* <tt>NEXT</tt> to play the next media
*/
public void changeMedia(PlayerControl control){
int size = this.library.getImportedMusicNumber();
if (!this.randomPlaying){
int mediaId = 0;
mediaId = this.library.getMediaId(this.currentMedia);
switch (control) {
case PREVIOUS:
mediaId -= 1;
if (mediaId < 0)
mediaId = size-1;
break;
case NEXT:
mediaId += 1;
if (mediaId >= size)
mediaId = 0;
break;
default:
break;
}
this.currentMedia = this.library.getMedia(mediaId);
}
else{
this.currentMedia = this.library.getMedia(this.randomGenerator.nextInt(size));
}
}
/**
* Plays the current media
*/
public void playMedia(){
try {
this.player = new AdvancedPlayer(this.currentMedia.getStream());
this.isPlaying = true;
this.player.play(this.position, Integer.MAX_VALUE);
while (this.isPlaying){
this.changeMedia(PlayerControl.NEXT);
this.playMedia();
}
} catch (JavaLayerException e) {
e.printStackTrace();
} catch(NullPointerException e){
System.out.println("No media to play ! (the source does not exists)");
}
}
@Override
public void run() {
this.playMedia();
}
} |
Partager