multithreading - Cannot delete a wav file after it has been opened, java -
i want sound file (temp0x) of wav format edited/deleted after has been opened. i'm using thread play sound, , close streams when done. main thread should delete it, fails so. if not play sound first there no problem deleting it.
this code in playsound thread plays sound:
public void run() { synchronized(this){ system.out.println("play sound: " + soundfile.getname()); audioformat format; dataline.info info; try { stream = audiosystem.getaudioinputstream(soundfile); format = stream.getformat(); info = new dataline.info(clip.class, format); clip = (clip) audiosystem.getline(info); clip.open(stream); clip.start(); }catch(exception ex){ ex.printstacktrace(); } while(clip.getmicrosecondlength() != clip.getmicrosecondposition()){ //waiting sound finished } try { stream.close(); } catch (ioexception e) { e.printstacktrace(); } clip.addlinelistener(new linelistener() { public void update(lineevent mylineevent) { if (mylineevent.gettype() == lineevent.type.stop) clip.close(); } }); soundfile = null; clip = null; stream = null; notify(); } }
this code in main thread:
playsound playsound = new playsound(filepath); thread play = new thread(playsound); play.start(); synchronized(play){ try{ system.out.println("waiting play complete..."); play.wait(); }catch(interruptedexception e){ e.printstacktrace(); } system.out.println("play done"); } if(new file("sounds/custom/temp0x.wav").delete()){ system.out.println("deleted"); } else system.out.println("cannot delete"); }
it prints cannot delete. i'v been staring @ hours , googling socks of cannot find solution. can me out?
edit::
if change boolean delete check suggested output (english: no acces since file used other proces):
java.nio.file.filesystemexception: src\opslaan.wav: het proces heeft geen toegang tot het bestand omdat het door een ander proces wordt gebruikt. @ sun.nio.fs.windowsexception.translatetoioexception(unknown source) @ sun.nio.fs.windowsexception.rethrowasioexception(unknown source) @ sun.nio.fs.windowsexception.rethrowasioexception(unknown source) @ sun.nio.fs.windowsfilesystemprovider.impldelete(unknown source) @ sun.nio.fs.abstractfilesystemprovider.delete(unknown source) @ java.nio.file.files.delete(unknown source) @ main.main(main.java:27)
edit:: problem seems platform dependent. need solution windows.
this time, little bit different
playsound.java
import java.lang.runnable; import javax.sound.sampled.audioformat; import javax.sound.sampled.*; import java.io.ioexception; import java.io.*; public class playsound implements runnable { bufferedinputstream soundfile; clip clip; audioinputstream stream; public playsound(string file) throws exception { // have create inputstream ourselves inputstream = new fileinputstream(file); soundfile = new bufferedinputstream(is); } public void run() { synchronized(this){ audioformat format; dataline.info info; try { // pass stream instead of file // looks getaudioinputstream messes around // file stream = audiosystem.getaudioinputstream(soundfile); format = stream.getformat(); info = new dataline.info(clip.class, format); clip = (clip) audiosystem.getline(info); clip.open(stream); clip.start(); while(clip.getmicrosecondlength() != clip.getmicrosecondposition()) {} // can close ourselves clip.close(); stream.close(); soundfile.close(); }catch(exception ex){ ex.printstacktrace(); } soundfile = null; clip = null; stream = null; notifyall(); } } }
main.java
import java.lang.thread; import java.io.ioexception; import java.io.*; import java.nio.*; import java.nio.file.*; public class main { public static void main(string [] arg) throws exception { string filepath = "sounds/custom/temp0x.wav"; playsound playsound = new playsound(filepath); thread play = new thread(playsound); play.start(); try { play.join(); } catch(exception ex) { ex.printstacktrace(); } system.out.println("play done"); path path = filesystems.getdefault().getpath("sounds/custom", "temp0x.wav"); try { files.delete(path); } catch (nosuchfileexception x) { system.err.format("%s: no such" + " file or directory%n", path); } catch (directorynotemptyexception x) { system.err.format("%s not empty%n", path); } catch (ioexception x) { // file permission problems caught here. system.err.println(x); } catch(exception ex) { ex.printstacktrace(); } } }
this time, after execution, file removed (at windows 7).
Comments
Post a Comment