Salut à tous,

Je dois développer un lecteur mp3 en Java et je suis partis avec la lib JLayer de Javazoom pour réaliser cela. Pour le moment, la lecture marche et les musiques s'enchainent au fur et à mesure.
J'ai en parallèle commencé l'IHM afin d'avoir une interaction plus simple qu'en console. J'ai donc ajouté le strict minimum, càd 4boutons (play, next, prev, random).
Lorsque je détecte un clic sur le bouton play par exemple, je lance la méthode play() de mon player. Le problème est que ça interrompt le thread principale et par conséquent, je ne peux plus interagir avec l'IHM (ni même fermer la fenêtre).

J'ai essayer d'implémenter Runnable sur ma classe Player, et j'appelle la méthode qui va bien pour lancer la musique, mais le problème est que je ne sais pas comment intéragir avec mon Objet à partir du moment que le thread est lancé. J'entends par à que si j’appelle la méthode start() sur le thread, comment je peux "ré-appeler" des méthode sur mon objet, alors qu'il démarre grâce à la surcharge de run().

Je me rends compte que ce ne doit pas être très clair, mais j'ai essayé de faire de mon mieux.

Je pose mon code avec les test au cas où:

M3TPlayer.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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();
	}
 
}
MainWindow.java

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
package fr.iutvalence.gui;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
import enumerations.PlayerControl;
import fr.iutvalence.m3tplayer.M3TPlayer;
 
public class MainWindow extends JFrame implements ActionListener, Runnable{
 
	private static final String APP_TITLE = "M3TPlayer";
 
	private ControlButtonsPanel controllButtonsPanel;
 
	private M3TPlayer m3tPlayer;
 
	public MainWindow(){
		this.m3tPlayer = new M3TPlayer();
		Thread m3tThread = new Thread(this.m3tPlayer);
 
		this.setTitle(APP_TITLE);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		this.controllButtonsPanel = new ControlButtonsPanel(this);
 
		this.getContentPane().add(this.controllButtonsPanel);
 
		this.pack();
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		JComponent source = (JComponent) e.getSource();
		Thread m3tThread = new Thread(this.m3tPlayer);
		if(source.equals(this.controllButtonsPanel.getPlayButton())){
			m3tThread.start();
		}
 
		if(source.equals(this.controllButtonsPanel.getNextButton())){
 
		}
 
		if(source.equals(this.controllButtonsPanel.getPreviousButton())){
			// TODO Previous
		}
 
		if(source.equals(this.controllButtonsPanel.getRandomButton())){
			// TODO Rand
		}
	}
 
	@Override
	public void run() {
		this.setVisible(true);
	}
 
}
Merci d'avance,