打开后无法删除wav文件,java

时间:2016-01-15 20:29:39

标签: java multithreading audio memory-leaks delete-file

我希望在打开之后编辑/删除wav格式的声音文件(temp0x)。我使用线程播放声音,并在完成后关闭所有流。然后主线程应该删除它,但它没有这样做。如果我不首先播放声音,删除它没有问题。

这是播放声音的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");


}

它打印无法删除。我已经盯着它看了好几个小时,然后搜索我的袜子,但我找不到解决办法。有人可以帮帮我吗?

EDIT ::

如果我按照建议更改布尔删除检查,这是我的输出(英文:由于其他进程已经使用过文件,因此没有访问权限):

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)

EDIT :: 问题似乎取决于平台。我需要一个适用于Windows的解决方案。

2 个答案:

答案 0 :(得分:1)

Main.java

scope: {
    eventHandler: '&ngClick'
},

PlaySound.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");
        }
    }
}

执行

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

Windows7的

它真的看起来像系统依赖的问题:)有趣。

enter image description here

答案 1 :(得分: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上)。