简体   繁体   中英

Is there a fast way to get all audio files on a device using java?

Beginner Java programmer here. I'm trying to make a simple mp3 player on my mac (and eventually Android) and I would like to have it auto recognize all the music ( mp3 ) that's already on your device.

I have a simple for loop set up that starts at the home directory and loops through all other directories looking for mp3s . I have yet to see it complete the loop as I always have to force stop it because it goes on for so long. I could just exclude some folders because placing music in them just wouldn't make sense, but I feel like there is a better approach.

Are there any other methods that I could look into that are faster and/or more efficient than a for loop?

I'm also curious about how the finder in mac works. For Example, when I use the finder to look for all the mp3 files on my mac, the results only take a couple of seconds to show up and all I need is to enter in some search criteria. If someone could point me in the direction of how to accomplish something similar to this that would be great, I'll take any input I can get!

public static void findAudio(File currentDir) {
    File[] filesInDir = currentDir.listFiles();
    if (filesInDir != null){
        if (filesInDir.length == 0){
            return;
        }
        else {
            for (File file : filesInDir) {
                if (!file.isHidden()) {
                    if (file.isDirectory()) {
                        findAudio(file);
                    }
                    if (file.canRead()) {
                        if (file.getName().endsWith(".mp3")) {
                            System.out.println(file.getPath());
                        }
                    }
                }
            }
        }
    }
    else {
        System.out.println(currentDir.getPath() + " is null -- skipping");
    }
}

First things first, checking file extension is less time consuming than checking if the file is readable.

Additionally, you can use Files.walkFileTree method added in Java 8 for this.

There is a tutorial at oracle's site on how to use it: https://docs.oracle.com/javase/tutorial/essential/io/walk.html

The code would look something like this:

Files.walkFileTree(Paths.get(sourcePath), new SimpleFileVisitor<>() {
    @Override
    public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) {
        if (filePath.toString().endsWith(".mp3") && filePath.toFile().canRead()) {
            System.out.println(filePath.toString());
        }
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) {
        System.err.println("Failed reading \"" + file.toString() + "\" -- skipping");
        return FileVisitResult.CONTINUE;
    }
});

SimpleFileVisitor class is a simplified implementation of FileVisitor interface with default behavior to visit all files and re-throw I/O Errors.

In case there is an I/O error we only print file path to default error stream and continue with file processing.

In case there are no errors, the file extension is ".mp3" (checking extension first as noted above), and the file is readable we print file path to the default output stream (you can also do other stuff, like adding it to list of media files)

import java.io.*;

public static void extract(String p) {
    File f = new File(p);
    File l[] = f.listFiles();
    for(File x : l){
        if (x==null) return;
        if (x.isHidden()||!x.canRead()) continue;
        if (x.isDirectory()) extract(x.getPath());
        else if (x.getName().endsWith(".mp3"))
        System.out.println(x.getPath()+"\\"+x.getName());
    }
}

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