简体   繁体   English

文件扩展名-用于文件。 (.mp3和.wav)

[英]File Extension - Use it for files. (.Mp3 and .Wav)

I manage to make a program that could understand mp3 in Java (by jaco.mp3 jar) and it works. 我设法制作了一个可以理解Java中mp3的程序(通过jaco.mp3 jar),它可以工作。 However this means that the .Wav files doesn't work of course. 但是,这意味着.Wav文件当然不起作用。 So I fixed so that I could use the Wav but the problem will then be that I can't use the mp3 then. 因此,我进行了修复,以便可以使用Wav,但是问题是我当时无法使用mp3。 so I found out that I could use the File Extension and my idea was to make like this: 所以我发现我可以使用文件扩展名,而我的想法是这样做的:

If the last 3 or 4 letters ends with .mp3 then do the mp3 method, if its .Wav then to Wave method. 如果最后3或4个字母以.mp3结尾,则执行mp3方法;如果其.Wav,则执行Wave方法。 but I don't really know how to manage it together, I was thinking more like a Switch-statement. 但是我真的不知道如何一起管理它,我想的更像是Switch语句。 But I really never worked with File extension before and could need som help with that! 但是我之前从未真正使用过文件扩展名,因此可能需要som帮助!

However this is my mp3 method that is like this right now: 但是,这是我现在的mp3方法:

public void Choose() {
        String userDir = System.getProperty("user.home");
        JFileChooser fileChooser = new JFileChooser(userDir +"/Desktop");

        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println(selectedFile.getName());

            mp3_player =  new MP3Player(selectedFile);
            lblPlayURL.setText(selectedFile.getName());
        }
    }
}

as you can see I have a JFileChooser where I pick the song I want of course and I still want to do it, only that the different is now it should work both as .Wav and .mp3. 如您所见,我有一个JFileChooser,我在其中选择了我想要的歌曲,但我仍然想要这样做,只是现在不同了,它应该同时以.Wav和.mp3的形式工作。 But I don't really know how to go further with it. 但是我真的不知道该怎么做。 so any help is needed from you guys! 因此,你们需要任何帮助! :) :)

Before people is trying to give any suggestions from a thread that says Playing .Wav and .Mp3. 在人们尝试从播放“ .Wav和.Mp3”的线程给出任何建议之前。 I would just say first that I have read it and there is only answers about each of them, Only mp3 or the .Wav. 我首先要说的是,我已经读过它,并且每个答案都只有mp3或.Wav。 not both. 不是都。 so thats why I created this thread because I need help! 这就是为什么我创建此线程的原因,因为我需要帮助!

You can perhaps check if the file path ends with MP3 or WAV and have an if statement to run different programs depending on the condition. 您也许可以检查文件路径是否以MP3或WAV结尾,并根据条件使用if语句来运行不同的程序。

This can be done like so: 可以这样完成:

String ext = selectedFile.getPath();

if(ext.endsWith(".wav"))
{
    // A WAV file was chosen
}
else if(ext.endsWith(".mp3"))
{
    // An MP3 file was chosen
}
else
{
    // Something else was chosen
}

EDIT: This method together with Bogdan's answer is probably your best bet. 编辑:这种方法与Bogdan的答案一起可能是您最好的选择。

Add FileFilter rules to your JFileChooser to match .mp3 and .wav files: FileFilter规则添加到JFileChooser以匹配.mp3.wav文件:

fileChooser.addChoosableFileFilter(new FileTypeFilter(".mp3", "MP3 Files"));
fileChooser.addChoosableFileFilter(new FileTypeFilter(".wav", "WAV files"));

How to add file filter for JFileChooser dialog . 如何为JFileChooser对话框添加文件过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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