Java File Mime Type isMP3(File file)

Here you can find the source of isMP3(File file)

Description

Checks if is mp3.

License

Apache License

Parameter

Parameter Description
file the file to be checked

Return

true, if the file is a valid mp3

Declaration

public static boolean isMP3(File file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /** The Constant MP3_MEDIA_TYPE. */
    private static final String MP3_MEDIA_TYPE = "audio/mpeg";

    /**/*ww  w.j  a  v  a  2  s .c o m*/
     * Checks if is mp3.
     *
     * @param file the file to be checked
     * @return true, if the file is a valid mp3
     */
    public static boolean isMP3(File file) {
        try {
            Path filepath = Paths.get(file.getAbsolutePath());
            String contentType = Files.probeContentType(filepath);
            if (contentType.equals(MP3_MEDIA_TYPE))
                return true;
        } catch (IOException e) {
            System.err.println("Error while detecting media type");
        }
        return false;
    }
}

Related

  1. getImageType(File file)
  2. getMime(File file)
  3. getMimeType(File file)
  4. getMimeType(String fileName)
  5. getType(File file)
  6. isValidImage(File file)