|
|
I've been working on an audio playback application that spawns a new thread to handle the actual playback of the file. The idea was that I could then revisit the thread and use a method to change the playback level of the player, or stop it altogether through custom methods. The actual playback of the file works just fine, but when another method of the control application then tries to fade the level of the player, it can't. The player in the fadeLevel() method is always null, and I'm afraid I just don't understand why. The class is as follows:
class PlayerThread extends Thread {
Object[] data; // data from the ready_cue
public PlayerThread(Object[] data) {
this.data = data;
}
private int number_index = 0;
private int name_index = 1;
private int autoFollow_index = 2;
private int type_index = 3;
private int preWait_index = 4;
private int duration_index = 5;
private int target_index = 6;
private int targetNumber_index = 7;
private int notes_index = 8;
private int level_index = 9;
public Player player;
public void run(){
try {
int baseTime = (Integer) data[preWait_index];
int preWaitTime = 1000 * baseTime;
Thread.sleep(preWaitTime);
File soundFile
= new File(String.valueOf(data[target_index]));
//Create a player
player = Manager.createRealizedPlayer(soundFile.toURL());
// Set the level for this cue to start at.
float gain = Float.valueOf(String.valueOf(data[level_index]));
GainControl gc = player.getGainControl();
gc.setDB(gain);
// Start the player.
System.out.println("playing player string is: " + player.toString());
player.start();
}
catch (InterruptedException e) { e.getMessage(); }
catch (Exception e) { e.getMessage(); }
}
public void fadeLevel() {
/* Change the level of the player over the specified duration.
*/
try {
int baseTime = (Integer) data[preWait_index];
int preWaitTime = 1000 * baseTime;
System.out.println(preWaitTime);
Thread.sleep(preWaitTime);
if (player != null) {
System.out.println("Fading volume");
GainControl gc = player.getGainControl();
float dur = Float.valueOf(String.valueOf(data[duration_index]));
float gain = Float.valueOf(String.valueOf(data[level_index]));
float time_interval = dur / gain;
float dur_buff = 0;
float gain_interval = gain / time_interval;
while (dur_buff < dur) {
Thread.sleep(((int) time_interval) * 1000);
gc.setDB(gain_interval);
dur_buff += time_interval;
}
}
} catch (Exception e) { e.getMessage(); }
}
public void printMessage() {
System.out.println("Test message");
}
}I've been away from Java for a while, so I'm very much willing to accept that I'm doing something dumb here, but my thinking was that by declaring Player player; outside the body of both methods, those methods would then have access to player, and fadeLevel would be able to do its thing with the running player.
I should mention that the player is running. I can still hear the sound playing: the level just never changes because fadeLevel() doesn't have access to the player that run() creates.
Little help?
Thank you,
Tony