gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:34:56 26 4
gpt4 key购买 nike

我想要一个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");


}

打印出来无法删除。我已经盯着它看了几个小时,并用谷歌搜索了我的 socks ,但我找不到解决方案。有人可以帮我吗?

编辑::

如果我按照建议更改 boolean 删除检查,这就是我的输出(英语:由于文件已被其他进程使用而无法访问):

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 的解决方案。

最佳答案

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

关于java - wav 文件打开后无法删除,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34819367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com