I am working on my Hhighschool capstone. It is a program where the user can download songs from the internet and store them in an organized manner and play them. My program must contain a feature where the user may delete a specified .mp3 file by simply pressing a button. I have tried the .dispose() method on the MediaPlayer, and then trying to delete the file which doesn’t seem to be working. It creates an error saying that the .mp3 file is still being used. How would I stop Javafx from accessing the file? I have searched online for answers but none of them have answers which fit my needs. If anyone could provide me with some code to fix my problem that would be greatly appreciated! Here’s a Mini Reproducible Example below!
JavaFxMp3WavPlayer
package mediaplayerjavafx; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.Paths; import javafx.application.Application; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.stage.Stage; public class JavaFxMp3WavPlayer extends Application { public static void main(String[] args) throws MalformedURLException, IOException { launch(args); } @Override public void start(Stage stage) throws Exception { stage.setTitle("My"); Button button = new Button("Delete Song File!"); Scene scene = new Scene(button, 200, 100); stage.setScene(scene); stage.show(); File file = new File("C:\Users\John Doe\OneDrive\Desktop\YourLieInAprilTest\Mp3Test.mp3"); String path = file.toURI().toASCIIString(); Media media = new Media(path); MediaPlayer mediaPlayer = new MediaPlayer(media); button.setOnAction(new EventHandler() { @Override public void handle(Event arg0) { deleteMusicFile(mediaPlayer); } }); mediaPlayer.play(); } public void deleteMusicFile(MediaPlayer mediaPlayer) { mediaPlayer.stop(); mediaPlayer.dispose(); try { Files.delete(Paths.get("C:\Users\John Doe\OneDrive\Desktop\YourLieInAprilTest\Mp3Test.mp3")); } catch (IOException ex) { ex.printStackTrace(); } } }
The class above essentially creates a MediaPlayer object and runs the specified .mp3 file. Then when you press the button, it will stop the MediaPlayer with the .stop() method, and then it will dispose of the MediaPlayer with the .dispose() method. Then it will try to delete the file which causes an error.
module-info
module MotisHarmony { requires javafx.swt; requires javafx.base; requires javafx.controls; requires javafx.fxml; requires javafx.graphics; requires javafx.media; requires javafx.swing; requires javafx.web; exports mediaplayerjavafx; opens mediaplayerjavafx to javafx.graphics; }
Error Produced
java.nio.file.FileSystemException: C:UsersJohn DoeOneDriveDesktopYourLieInAprilTestMp3Test.mp3: The process cannot access the file because it is being used by another process. at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92) at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103) at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108) at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:270) at java.base/sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:105) at java.base/java.nio.file.Files.delete(Files.java:1141) at MotisHarmony/mediaplayerjavafx.JavaFxMp3WavPlayer.deleteMusicFile(JavaFxMp3WavPlayer.java:53) at MotisHarmony/mediaplayerjavafx.JavaFxMp3WavPlayer$1.handle(JavaFxMp3WavPlayer.java:43) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.base/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792) at javafx.controls/javafx.scene.control.Button.fire(Button.java:203) at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208) at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.base/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897) at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878) at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449) at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557) at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) at java.base/java.lang.Thread.run(Thread.java:834)
Additional info
Link to the Mp3 file I used to test. https://drive.google.com/file/d/1CvAafbMviQ7nvKyojnem9GK73LJsD6MJ/view?usp=sharing
I am using JDK 11 and Javafx 17.0.2
System Type: 64-bit operating system, x64-based processor
Processor: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz 2.81 GHz
Windows Edition: Windows 10 Home
Advertisement
Answer
Thank you to @Slaw for providing a solution to my problem. To solve my issue, I created a deletionQueue ArrayList which would hold the paths to the files I would like to delete. Once .dispose() is used on the MediaPlayer, after a certain amount of time has passed, those files would be automatically deleted.