2016-01-15 75 views
0

我想要一個wav格式的聲音文件(temp0x)在打開後編輯/刪除。我正在使用線程播放聲音,並在完成時關閉所有流。主線程然後應該刪除它,但它沒有這樣做。如果我不先播放聲音,刪除它沒有問題。打開後無法刪除wav文件,java

這是playSound線程時播放的聲音代碼:

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 for sound to be 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(); 
    } 
} 

這是我的主線程代碼:

PlaySound playSound = new PlaySound(filePath); 
    Thread play = new Thread(playSound); 
    play.start(); 

    synchronized(play){ 
     try{ 
      System.out.println("Waiting for play to 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"); 


} 

它將打印無法刪除。我一直盯着它幾個小時,搜索我的襪子,但我找不到解決方案。有人可以幫我嗎?

編輯::

如果我改變布爾刪除檢查的建議,這是我的輸出(英文:沒有存取權限,因爲已經使用其它理線文件):

java.nio.file.FileSystemException: src\Opslaan.wav: Het proces heeft geen toegang tot het bestand omdat het door een ander proces wordt gebruikt. 

at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) 
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) 
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) 
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source) 
at sun.nio.fs.AbstractFileSystemProvider.delete(Unknown Source) 
at java.nio.file.Files.delete(Unknown Source) 
at Main.main(Main.java:27) 

編輯:: 問題似乎取決於平臺。我需要Windows的解決方案。

+0

您可能想嘗試從您的調用中分離出您的文件創建('new File(「Sounds/Custom/temp0x.wav).delete())',然後確保該文件存在,並且路徑是正確的,然後調用'delete'。 – Andy

+0

首先,將'new File(「Sounds/Custom/temp0x.wav」)。delete()'改爲'Files.delete(Paths.get(「Sounds/Custom/temp0x。 wav「))'。這樣,你就會得到一個信息性的異常,它會給你更多關於爲什麼文件不能被刪除的細節。第二,Object.wait()必須被調用請參閱[文檔](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--)以瞭解詳細信息。 – VGR

+0

在OS X中,我不知道根本沒有遇到任何問題,按預期工作。 – mko

回答

0

這一次,有點不同

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 { 
     // we have to create InputStream by ourselves 
     InputStream is = new FileInputStream(file); 
     soundFile = new BufferedInputStream(is); 
    } 

    public void run() { 
     synchronized(this){ 

     AudioFormat format; 
     DataLine.Info info; 

     try { 
      // we pass stream instead of file 
      // it looks like getAudioInputStream messes around with 
      // 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()) {} 
      // we can close everything by 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 are caught here. 
      System.err.println(x); 
     } catch(Exception ex) { 
      ex.printStacktrace(); 
     } 
    } 
} 

這一次,執行後,文件將被刪除(在Windows 7的)。

+0

它的作品!然而,它太慢了,我的程序不再可用了:(:(不是這些代碼部分,但與GUI等整個事情)我看看內存負載,但這似乎並沒有增加太多,在播放聲音時只能達到30%左右,但如果這個問題是由於Windows問題造成的,那麼我只能試着圍繞它進行構建,非常感謝您的幫助! – Anne

1

Main.java

import java.lang.Thread; 
import java.io.IOException; 
import java.io.*; 

public class Main { 

    public static void main(String [] arg) { 

     String filePath = "Sounds/Custom/temp0x.wav"; 
     PlaySound playSound = new PlaySound(filePath); 
     Thread play = new Thread(playSound); 
     play.start(); 

     synchronized(play){ 
     try{ 
      System.out.println("Waiting for play to complete..."); 
      play.wait(); 
     }catch(InterruptedException e){ 
      e.printStackTrace(); 
     } 

     System.out.println("play done"); 
     } 

     if(new File(filePath).delete()){ 
     System.out.println("deleted"); 
     } else { 
     System.out.println("cannot delete"); 
     } 
    } 
} 

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 { 

    File soundFile; 
    Clip clip; 
    AudioInputStream stream; 

    public PlaySound(String file) { 
     soundFile = new File(file); 
    } 

    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 for sound to be 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(); 
     } 
    } 
} 

執行

> javac PlaySound.java 
> javac Main.java 
> java Main 
Waiting for play to complete... 
play sound: sound.wav 
play done 
deleted 

目錄佈局

. 
├── Main.class 
├── Main.java 
├── PlaySound$1.class 
├── PlaySound.class 
├── PlaySound.java 
└── Sounds 
    └── Custom 
     └── temp0x.wav 

Windows7

它真的看起來像系統依賴問題:)有趣。

enter image description here

+0

我只是不明白......這可能是平臺的依賴嗎?我正在使用Windows 7.這是我在我的電腦上運行時得到的結果:等待播放完成... 播放聲音:Opslaan.wav 播放完成 無法刪除 – Anne

+0

這不適用於我。它與我發送的代碼相同。看原來的問題:編輯部分。 – Anne