Android Open Source - easympsd Mp3 File






From Project

Back to project page easympsd.

License

The source code is released under:

GNU General Public License

If you think the Android project easympsd listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
Copyright 2014-2015 Francesco Palumbo <franzodev@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version./*from  ww  w.j  a va 2  s .c om*/

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.franz.easympsd;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.media.MediaMetadataRetriever;
import android.util.Log;

@SuppressLint("NewApi")
public class Mp3File {
  private static final String TAG = Mp3File.class.getSimpleName();
  static private MediaMetadataRetriever metar;
  
  private File mfile;
  private String mname;
  private Integer mbitrate;
  
  static {
    metar = new MediaMetadataRetriever();
  }
  
  public Mp3File(File fl) {
    mfile = fl;
    mname = fl.getName();
    metar.setDataSource(fl.getAbsolutePath());
    
    mbitrate = Integer.parseInt(
               metar.extractMetadata(
                   MediaMetadataRetriever.METADATA_KEY_BITRATE))
           / 1000;
  }
  
  public String getName() {
    return mname;
  }
  
  public String getBr() {
    return mbitrate.toString() + "kbps";
  }
  
  public File getFile() {
    return mfile;
  }
  
  public String getDuration() {
    return metar.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
  }
  
  static public File[] getMusicList(String mdir) {
    File music_dir = new File(mdir);
    
        if (!music_dir.exists()) {
            if (!music_dir.mkdirs()) {
                Log.e(TAG, "Directory not created");
                return null;
            }
        }
        else {
            if (music_dir.isFile()) {
                Log.e(TAG, "File with same name detected");
                return null;
            }
        }

        if (music_dir.canRead()) {
          
          // Includes only mp3 files
            return music_dir.listFiles(new FilenameFilter() {
              @Override
              public boolean accept(File mdir, String name) {
                
                return name.toLowerCase(Locale.getDefault())
                       .endsWith(".mp3");
              }
            });
        }
        else {
            Log.e(TAG, "Cannot read from directory");
            return null;
        }
    }
  
  static public Mp3File[] retrieveMp3Files(String dir) {
    ArrayList<Mp3File> al = new ArrayList<Mp3File>();
    
    for (File mpf: getMusicList(dir)) {
      al.add(new Mp3File(mpf));
    }
    
    return (Mp3File[]) al.toArray(new Mp3File[al.size()]);
  }
}




Java Source Code List

com.franz.easympsd.MainActivity.java
com.franz.easympsd.Mp3File.java
com.franz.easympsd.MusicListAdapter.java
com.franz.easympsd.MusicReader.java
com.franz.easympsd.Playlist.java