So I load some data from Database and use SwingWorker for it.
public class LoadFromDatabase extends SwingWorker<ArrayList<Ucet>, GuiUpdate>{
private ArrayList<Ucet> ucty;
private JLabel lblStav;
private File dbPath;
private JProgressBar progress;
private int pocetUctov;
private JButton btnLoad;
private JButton btnStart;
public LoadFromDatabase(ArrayList<Ucet> ucty,JLabel lblStav,File dbpath,JProgressBar progress, JButton btnLoad,JButton btnStart){
this.ucty=ucty;
this.lblStav=lblStav;
this.dbPath=dbpath;
this.progress=progress;
this.btnLoad=btnLoad;
this.btnStart=btnStart;
}
#Override
protected ArrayList<Ucet> doInBackground() throws Exception {
String sqlLoadUcty="SELECT email,password FROM members";
ArrayList<Ucet> ucty2=new ArrayList<>();
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+dbPath.getPath());
Statement stmt = conn.createStatement();
stmt.setQueryTimeout(30);
ResultSet rs = stmt.executeQuery(sqlLoadUcty);
GuiUpdate gd=new GuiUpdate(GuiUpdate.GuiType.setStartLoading);
gd.setValue(0);
publish(gd);
pocetUctov=rs.getFetchSize();
gd=new GuiUpdate(GuiUpdate.GuiType.setMaxValue);
gd.setValue(pocetUctov);
publish(gd);
int counter=0;
while (rs.next()){
Ucet uct=new Ucet(
rs.getString("email"),
rs.getString("password")
);
gd=new GuiUpdate(GuiUpdate.GuiType.setValue);
gd.setValue(counter);
publish(gd);
ucty2.add(uct);
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException ex) {
System.out.println("Problem= "+ex);
} catch (SQLException ex) {
System.out.println("Problem= "+ex);
}
return ucty2;
}
#Override
public void process(List<GuiUpdate> update){
for (GuiUpdate guiUpdate : update) {
if (guiUpdate.getToDo()==GuiUpdate.GuiType.setStartLoading) {
lblStav.setText("Loading ...");
progress.setVisible(true);
} else if (guiUpdate.getToDo()==GuiUpdate.GuiType.setMaxValue) {
progress.setMaximum(guiUpdate.getValue());
pocetUctov=guiUpdate.getValue();
progress.setMinimum(0);
} else if (guiUpdate.getToDo()==GuiUpdate.GuiType.setValue) {
progress.setValue(guiUpdate.getValue());
}
}
}
#Override
public void done(){
progress.setVisible(false);
btnLoad.setEnabled(true);
try {
ucty=get();
} catch (InterruptedException ex) {
System.out.println("Problem= "+ex);
} catch (ExecutionException ex) {
System.out.println("Problem= "+ex);
}
if (ucty!=null && ucty.size()>0) {
btnStart.setEnabled(true);
lblStav.setText("Loaded "+ucty.size()+" accounts.");
}
}
}
This is whole SwingWorker. I basicly load data from DB and update progress bar.
Once loaded then done() method is called where reference to created ArrayList in doInbackground is set to arraylist which comes from mainGUI and some buttons are allowed and disallowed.
This is how I call SwingWorker from Gui:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileDb=new JFileChooser();
int returnVal=fileDb.showOpenDialog(this);
if (returnVal==JFileChooser.APPROVE_OPTION) {
databasePath=fileDb.getSelectedFile();
jButton4.setEnabled(false);
execurtor.execute(new LoadFromDatabase(naciatneUcty, jLabel9, databasePath, jProgressBar1, jButton4,jButton1));
}
}
This works great ,even if debug last line of code in SwingWorker done() I can clearly see that ArrayList ucty contians data from databse.
Once back to main gui , ArrayList naciatneUcty is still null.
But it should not be since I am sending it to SwingWorker where its reference should be updated...
Where is the problem ,why reference is not updated at all?
Java references are passed by value, so when you execute ucty = get() the reference inside the swingworker is changed, but that isn't going to update the reference for naciatneUcty. You'd be better off instantiating naciatneUcty before running the swingworker, have doInBackground() return void, scrap utcy2 and then just grab utcy in the done() method.
Related
I have a pretty simplistic JavaFX application. In it, I have a Java object for handling database activities, mainly executing queries. To prevent my UI from completely freezing while the query executes, I've implemented a background thread using the javafx.concurrent.Service. This works great on my connect method, which doesn't return anything. However, in my query method it immediately jumps to the return line, and of course returns null. Then it goes back and runs the query, but it's already returned an empty arraylist.
What am I doing wrong?
Here's my method:
public ArrayList<Foo> runQuery() throws SQLException {
ArrayList<Foo> result = new ArrayList<Foo>();
backgroundThread = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
protected Void call() throws Exception {
stmt = conn.createStatement();
String query = "Select stuff...
rs = stmt.executeQuery(query);
return null;
}
};
}
};
backgroundThread.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent argo) {
try {
while (rs.next()) {
result.add(new Foo(rs.getString(1)));
}
} catch (SQLException e) {
e.printStackTrace();
}
controller.addLogEntry("done.\n");
}
});
backgroundThread.restart();
return result;
}
please help me guys..
my observablelist was updated successfully by background Thread.
but my GUI Update is not accurate.
addPiechart1 to HBOX
addPieChart2 to HBOX
observablelist1= getobservablelist from piechart1
observablelist2= getobservablelist from piechart2
pass observablelist1 to Thread
pass observablelist2 to Thread
ex
on the process i call
animatePie(observablelist1,value1);
animatePie(observablelist2,value2);
public void animatePie(ObservableList<PieChart.Data> obs,int[] value){
ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
#Override public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
});
pieAddToQueue add = new pieAddToQueue(obs,value);
//add.run();
executor.execute(add);
}
private class pieAddToQueue implements Runnable {
ObservableList<PieChart.Data> observableData;
int[] value;
int loop;
public pieAddToQueue(ObservableList<PieChart.Data> obs,int[] value){
observableData=obs;
this.value=value;
loop=0;
}
#Override
public void run() {
for(int i=0;i<value.length;i++){
observableData.get(i).setPieValue(value[i]);
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(MISInfoBoardBottom.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
You cannot update the UI on a background thread, so you must wrap the calls that change the UI with a Platform.runLater(...):
private class pieAddToQueue implements Runnable {
ObservableList<PieChart.Data> observableData;
int[] value;
int loop;
public pieAddToQueue(ObservableList<PieChart.Data> obs,int[] value){
observableData=obs;
this.value=value;
loop=0;
}
#Override
public void run() {
for(int i=0;i<value.length;i++){
PieChart.Data pieChartData = observableData.get(i);
int v = value[i] ;
Platform.runLater(() ->
pieChartData.setPieValue(v));
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(MISInfoBoardBottom.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
I am new to JavaFx/Concurrency so I read the tutorial over at Concurrency in JavaFX but I am still a little confused about the implementation of background threads in a JavaFX Gui.
I'm trying to write a small GUI that interfaces with some serial devices (using JSSC-2.8) and that updates the GUI based on the responses from those devices. But, there's a lag between when the message is written and when the device responds, and using Thread.sleep() for an arbitrary amount of time wasn't a reliable way for me program it. So instead I want to use wait() and notify() methods from the concurrency package (with all the appropriate synchronizations), but I am not sure how to implement it. What I initially did is create another Thread, inside the Task, that would write the messages and wait for the responses, and using some bindings, would update the GUI. I've included my code at the end. Here is a short form of the pseudocode I am trying to implement:
start Task:
connect to serial devices
synchronized loop:
send messages
wait() for event to fire
notify()
But what's been happening is, as soon as I call the wait(), the entire application idles and then when notify() is called (after the response fires and event), it doesn't continue where it left off in the recipe() loop, or the startTdk() loop for that matter, it's just idle. Have I implements the threads wrong? When I am calling the wait(), is it a possibility that I cause the EventDispatch or JavaFX Application Thread to pause?
I hope the question is clear, if there are any clarifications needed I can update the post.
public class OmicronRecipe extends Service<String> implements Runnable{
private final String SEPERATOR=";";
private final Tdk tdk;
private final Pvci pvci;
private final SimpleStringProperty data = new SimpleStringProperty("");
private final Float MAX_V = 26.0f,UHV=1e-8f;
private boolean isTdkOn=false, isPvciOn=false;
private String power;
private Float temp,press,maxT, setT;
private int diffMaxT,diffP,diffPow, diffT, index=0;
public OmicronRecipe(){
tdk = new Tdk("COM4");
pvci = new Pvci("COM5");
}
private synchronized void recipe(){
while (true){
try {
sendMessages();
data.set(power+SEPERATOR+temp+SEPERATOR+press);
calcDiffs();
if (diffPow < 0){
if(diffMaxT < 0){
if(diffT < 0){
if (diffP < 0){
if(!rampPow()){
//Max Power reached
}
}else{
//Wait for pressure drop
}
}
}else{
//Wait until quit
}
}else{
//Max power reached
}
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(OmicronRecipe.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private synchronized boolean rampPow(){
boolean isRamped=false;
Float setPow = tdk.getSetPow(index), curPow;
setT = tdk.getSetT(index);
curPow = Float.parseFloat(power);
if(curPow.compareTo(setPow) < 0){
do{
curPow += 0.1f;
tdk.sendMessage("PV "+curPow+"\r");
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(OmicronRecipe.class.getName()).log(Level.SEVERE, null, ex);
}
curPow = Float.parseFloat(power);
}while(curPow.compareTo(setPow) < 0);
index++;
isRamped=true;
}
return isRamped;
}
public synchronized boolean connect(){
if(!isTdkOn && !isPvciOn){
isTdkOn = tdk.connect();
isPvciOn = pvci.connect();
}
return isTdkOn && isPvciOn;
}
public synchronized boolean disconnect(){
if(tdk!=null && pvci !=null){
isTdkOn = tdk.disconnect();
isPvciOn = pvci.disconnect();
}
return !isTdkOn && !isPvciOn;
}
public synchronized StringProperty getData(){
return data;
}
public void setMaxT(Float maxT){
this.maxT = maxT;
}
private synchronized void calcDiffs(){
Float pow = Float.parseFloat(power);
diffPow = pow.compareTo(MAX_V);
diffMaxT = temp.compareTo(maxT);
diffT = temp.compareTo(100f);
diffP = press.compareTo(UHV);
}
private synchronized void setListeners(){
tdk.getLine().addListener((ov,t, t1)-> {
synchronized (this){
System.out.println("New Power: "+t1);
power = t1;
this.notify();
}
});
pvci.getLine().addListener((ov,t,t1) ->{
synchronized (this){
String[] msg = t1.split(SEPERATOR);
if(msg.length == 2){
switch(msg[0]){
case "temperature":
System.out.println("Temperaute");
temp = Float.parseFloat(msg[1]);
break;
case "pressure":
System.out.println("Pressure");
press = Float.parseFloat(msg[1]);
break;
default:
System.out.println("Nothing; Something went wrong");
break;
}
}
this.notify();
}
});
}
private synchronized void sendMessages(){
try {
tdk.sendMessage("PV?\r");
this.wait();
pvci.sendMessage("temperature");
this.wait();
pvci.sendMessage("pressure");
this.wait();
} catch (InterruptedException ex) {
Logger.getLogger(OmicronRecipe.class.getName()).log(Level.SEVERE, null, ex);
}
}
private synchronized boolean startTdk(){
boolean isOut=false;
if(isTdkOn){
try {
tdk.sendMessage("ADR 06\r");
this.wait();
System.out.println("Power: "+power);
if(power.equals("OK")){
tdk.sendMessage("OUT?\r");
this.wait();
if(power.equals("OFF")){
tdk.sendMessage("OUT ON\r");
this.wait();
isOut = power.equals("ON");
}
else{
isOut = power.equals("ON");
}
}
} catch (InterruptedException ex) {
Logger.getLogger(OmicronRecipe.class.getName()).log(Level.SEVERE, null, ex);
}
}
return isOut;
}
#Override
protected Task<String> createTask() {
return new Task<String>() {
#Override
protected String call() throws IOException{
new Thread(new OmicronRecipe()).start();
return "";
}
};
}
#Override
public void run() {
if (connect()){
setListeners();
if(startTdk()){
recipe();
}
}
}
}
I won't include the Pvci class, because it just a copy of the Tdk class but with specific message sequences to talk with that machine.
public class Tdk {
private SerialPort tdkPort;
private final String portName;
private StringBuilder sb = new StringBuilder("");;
private final StringProperty line = new SimpleStringProperty("");
private final HashMap<Float,Float> calibMap;
private ArrayList<Float> list ;
private boolean isEnd=false;
public Tdk(String portName){
this.portName = portName;
System.out.println("TDK at "+portName);
calibMap = new HashMap();
setMap();
}
public synchronized boolean connect(){
tdkPort = new SerialPort(portName);
try {
System.out.println("Connecting");
tdkPort.openPort();
tdkPort.setParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
tdkPort.setEventsMask(SerialPort.MASK_RXCHAR);
tdkPort.addEventListener(event -> {
if(event.isRXCHAR()){
if(event.getPortName().equals(portName)){
try {
if(!isEnd){
int[] str = tdkPort.readIntArray();
if(str!=null)
hexToString(str);
}
if(isEnd){
System.out.println("Here: "+sb.toString());
isEnd=false;
String d = sb.toString();
sb = new StringBuilder("");
line.setValue(d);
}
} catch (SerialPortException e) {
Logger.getLogger(Tdk.class.getName()).log(Level.SEVERE, null, e);
}
}
}
});
} catch (SerialPortException e) {
Logger.getLogger(Tdk.class.getName()).log(Level.SEVERE, null, e);
}
return tdkPort !=null && tdkPort.isOpened();
}
public synchronized boolean disconnect(){
if(tdkPort!=null) {
try {
tdkPort.removeEventListener();
if (tdkPort.isOpened())
tdkPort.closePort();
} catch (SerialPortException e) {
Logger.getLogger(Tdk.class.getName()).log(Level.SEVERE, null, e);
}
System.out.println("Disconnecting");
}
return tdkPort.isOpened();
}
public synchronized void sendMessage(String message){
try {
tdkPort.writeBytes(message.getBytes());
} catch (SerialPortException e) {
Logger.getLogger(Tdk.class.getName()).log(Level.SEVERE, null, e);
}
}
private void setMap(){
calibMap.put(1.0f, 25.0f);
calibMap.put(7.0f, 125.0f);
calibMap.put(9.8f, 220.0f);
list = new ArrayList(calibMap.keySet());
}
public Float getSetPow(int index){
return list.get(index);
}
public Float getSetT(int index){
return calibMap.get(list.get(index));
}
public synchronized StringProperty getLine(){
return line;
}
private synchronized void hexToString(int[] hexVal){
for(int i : hexVal){
if(i != 13){
sb.append((char)i);
}else{
isEnd=true;
}
}
System.out.println("Turning: "+Arrays.toString(hexVal)+" to String: "+sb.toString()+" End: "+isEnd);
}
Freeze
Your UI freezes most probably because you are waiting on the FX Apllication Thread, to solve this there are different approaches:
JavaFX Application Thread
You can delegate some work to the FX Application Thread, therefore see Platform.runLater
Not everything can be run on this thread, but for example, in your DeviceController, you can wait until the message appears and then call Platform.runLater() and update the field (you should therefor oc hand the field over to the controller).
DataBinding
What you are describing can also be realised with DataBinding.
With this you could define a SimpleStringProperty, which is bound to your UI Label (.bind() Method). If the controller must fire its message you can set the StringProperty and the UI will update itself.
The scenario you described could be used like this:
start Task:
connect to serial devices
synchronized loop:
send messages
wait() for event to fire
**updateDate the DataBounded fields**
We are taught that, Concurrency notify/wait
Concurrency on level wait()/notify() is very low level. You should try to work with higher level synchronisation methods or helpers (where people have already solved your problems :))
I cannot resolve a problem and need your help. When I click on menu I call customer account and then afterwards I close it. Every time I call customer account the memory increases. It should diminish when I close the account, but it does not happen.
Class Menu
mnItemCL_Cust.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
try {
panCenterPrev = (Pane) root.getCenter();
panCenterAct = Customer.listCustomer();
root.setCenter(null);
root.setCenter(panCenterAct);
Customer.btCanc.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
try {
Customer.Fim();
panCenterAct.getChildren().clear();
panCenterAct = null;
root.setCenter(null);
root.setCenter(panCenterPrev);
} catch (Throwable ex) {
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Class Customer
public class Customer
{
public static Pane listCustomer() throws SQLException, ClassNotFoundException
{
...
final ObservableList<MyCustomer> data = FXCollections.observableArrayList();
...
}
public static class MyCustomer {
private final SimpleIntegerProperty idcl;
private MyCustomer(Integer pIdcl ) {
this.idcl = new SimpleIntegerProperty(pIdcl);
}
public Integer getIdcl() {
return idcl.get();
}
public void setIdcl(Integer pIdcl) {
idcl.set(pIdcl);
}
}
public static void Fim() throws Throwable {
...
rs = null;
tbViewCL.getItems().clear();
tbViewCL = null;
colIDCL.getColumns().clear();
colIDCL = null;
}
...
protected void finalize() throws Throwable {
try{
...
rs.close();
...// Never happens... why??
} catch(Throwable t) {
throw t;
} finally {
JOptionPane.showMessageDialog(null,"End?");
super.finalize();
}
}
Regards
Java usually reclaims the memory you used when it see it fits, so even if you finalize the object, the memory may still be there. However, if rs.Close() never executes, probably is because something before it is throwing and exception, i recommend you to check the code before just to be sure that nothing is doing so, also, if you catch an exception is a good practice to log it so you can know what is happening.
Hi I am new to android and I am learning by example. I am trying to make an activity that has a list view of all songs in my raw folder with media player controls at the bottom. I have everything working so far but I can't seem to get the SeekBar to stop force closing.
Here is the code:
public class music extends ListActivity implements Runnable {
private ArrayList<sound> mSounds = null;
private soundadapter mAdapter = null;
private ImageButton playbtn;
private SeekBar seekbar;
private int total;
private MediaPlayer mp = null;
private TextView selelctedFile = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
selelctedFile = (TextView) findViewById(R.id.selectedfile);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setProgress(0);
// create a simple list
mSounds = new ArrayList<sound>();
sound s = new sound();
s.setDescription("Rudolph The Red Nosed Reindeer");
s.setSoundResourceId(R.raw.rudolphtherednosereindeer);
mSounds.add(s);
s = new sound();
s.setDescription("Battery");
s.setSoundResourceId(R.raw.battery);
mSounds.add(s);
mAdapter = new soundadapter(this, R.layout.listitem, mSounds);
setListAdapter(mAdapter);
playbtn = (ImageButton) findViewById(R.id.play);
playbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (mp.isPlaying()) {
mp.pause();
playbtn.setImageResource(android.R.drawable.ic_media_play);
} else {
mp.start();
playbtn.setImageResource(android.R.drawable.ic_media_pause);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
});
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id) {
sound s = (sound) mSounds.get(position);
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, s.getSoundResourceId());
selelctedFile.setText(s.getDescription());
playbtn.setImageResource(android.R.drawable.ic_media_pause);
mp.start();
total = mp.getDuration();
seekbar.setMax(total);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser) {
mp.seekTo(progress);
seekBar.setProgress(progress);
}
}
});
Thread currentThread = new Thread(this);
currentThread.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
try {
while (mp != null) {
int currentPosition = mp.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
Thread.sleep(100);
}
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
private Handler threadHandler = new Handler() {
public void handleMessage(Message msg) {
// super.handleMessage(msg);
// txt.setText(Integer.toString(msg.what));
seekbar.setProgress(msg.what);
}
};
#Override
protected void onPause() {
// TODO Auto-generated method stub
mp.stop();
mp.release();
mp = null;
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
and here is the error i keep getting when i click several times on different songs:
04-14 02:53:00.452: W/dalvikvm(27452): threadid=19: thread exiting with uncaught exception (group=0x40018560)
04-14 02:53:00.466: E/AndroidRuntime(27452): FATAL EXCEPTION: Thread-22
04-14 02:53:00.466: E/AndroidRuntime(27452): java.lang.IllegalStateException
04-14 02:53:00.466: E/AndroidRuntime(27452): at android.media.MediaPlayer.getCurrentPosition(Native Method)
04-14 02:53:00.466: E/AndroidRuntime(27452): at net.cybercore.collapsingfromwithin.music.run(music.java:145)
04-14 02:53:00.466: E/AndroidRuntime(27452): at java.lang.Thread.run(Thread.java:1019)
Line error 145 is :
int currentPosition = mp.getCurrentPosition();
I cannot for the life of me figure out why it works for 3 or 4 times playing and then it kills the app.
Any help is appreciated. I have already looked at several other sites for examples including http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ and http://www.androiddevblog.net/android/playing-audio-in-android
**
UPDATE
**
I think I fixed it. thanks for your help I found Thread using for seekbar on android mediaplayer so i changed it to
#Override
public void run() {
// TODO Auto-generated method stub
try {
while (mp != null) {
int currentPosition = mp.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("interrupt exeption" + e);
}
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("My exeption" + e);
}
}
I still get the errors but they are not killing my app. I don't think this is right way to do it but its working.
You should prepare your media player when instanciating it.
A MediaPlayer object must first enter the Prepared state before playback can be started.
There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to prepare() (synchronous) which transfers the object to the Prepared state once the method call returns, or a call to prepareAsync() (asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand via setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener).
Read it here
so you should call mp.prepare() after instanciating the player.
also you should make sure the media player in playing to run the run method. I'd start by adding
mp.isPlaying() to the while line.
while (mp != null && mp.isPlaying()) {
...
}
IllegalStateException means that you are on an illegal state to call that method, like for instance, if the player is stopped.
I'm not sure, but I think this will stop the run method when you pause the music. So you should try to avoid this. I create a boolean to identify that the player is playing or paused and use it on the while.