2016-01-15 69 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