简体   繁体   中英

Easy way to store MP3 file-path to variable and read from another class?

I'm trying to create a MP3 player program and I need a way to store the file-path of the mp3 in a variable so I can load the variable in another class and play that file. Current code block for save/load is .txt now, but I need to replace it with the variable save.

try {
      // What to do with the file.
      textarea.read( new FileReader( file.getAbsolutePath() ), null );
    } catch (IOException ex) {
      System.out.println("Problem accessing file"+file.getAbsolutePath());
    }
} else {
    System.out.println("File access cancelled.");
}

I'm using these imports in the class reading the variable,

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import java.net.*;
import java.io.*;
import java.io.FileInputStream;
import java.util.*;
import javazoom.jl.player.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.util.ResourceLoader;

As well as the import for the class in question.

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
int returnVal = fileChooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
        try{

            FileInputStream mp3 = new FileInputStream(fileChooser.getSelectedFile());
            Player playMP3 = new Player(mp3);

            }catch(Exception e){System.out.println(e);

} }   
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM