Android Open Source - easympsd Music Reader






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.// ww w  . j  a  v a2  s  . c o m

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 android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

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


public class MusicReader {
    
    private static MusicReader instance = null;
    
    private static SeekBar shandler;
    private static TextView pthandler;
    
    private static Handler hl;
    private static Runnable rn;
    
    private static int seek_pos = 0;
    private static int duration;
    
    private MediaPlayer mplayer; 
    private int last_played = -1;
   
    //private Playlist playlist;
    
    private ListView  mlist;
    private ListAdapter mlist_adapter;
    
    private boolean mplayer_initialized = false;
    private boolean was_playing = false;
    
    private MusicReader() {};
    
    static public MusicReader getInstance() {
      if (instance == null) {
        instance = new MusicReader();
        instance.mplayer = new MediaPlayer();
        instance.mplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        instance.mplayer.setOnPreparedListener(new OnPreparedListener() {
        
        @Override
        public void onPrepared(MediaPlayer mp) {
          duration = mp.getDuration();
          
          if (shandler != null)
            shandler.setMax(duration);
            
          mp.start();
          
          runSeekTimeUpdater();
        }
      });
      
      }
    
      return instance;
    }
 
    public void setSeekBar(SeekBar s) {
      shandler = s;
      
      hl = new Handler();
      
    rn = new Runnable() {
      @Override
      public void run() {
        if (mplayer != null && mplayer.isPlaying()) {
          shandler.setProgress(mplayer.getCurrentPosition());
          
          if (pthandler != null)
            pthandler.setText(getPlayTime());
          
          hl.postDelayed(rn, 1000);
        }
      }
    };
    
      shandler.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      
      @Override
      public void onStopTrackingTouch(SeekBar sb) {
        mplayer.seekTo(seek_pos);
      }

      @Override
      public void onProgressChanged(SeekBar sb, int progress,
          boolean fromUser) {
        seek_pos = progress;
        
                if (pthandler != null)
                  pthandler.setText(getPlayTime());
      }

      @Override
      public void onStartTrackingTouch(SeekBar sb) {
        //if ()
      }
    });
    }
 
    public void setPlayTimeTextView(TextView ptv) {
      pthandler = ptv;
    }
    
    public void pause() {
      if (mplayer.isPlaying() && last_played != -1) {
        seek_pos = mplayer.getCurrentPosition();
        mplayer.pause();
        was_playing = true;
      }
    }
    
    public void setPlaylist(Playlist pl) {
      //playlist = pl;
    }
    
    public void setMusicList(ListView ml) {
      mlist_adapter = ml.getAdapter();
      mlist = ml;
      
    mplayer.setOnCompletionListener(new OnCompletionListener() {
      
      @Override
      public void onCompletion(MediaPlayer mp) {
        if (pthandler != null)
          pthandler.setText("00:00:00");
        
        if (shandler != null)
          shandler.setProgress(0);
        
        int maxindex = mlist_adapter.getCount() - 1;
        
        last_played = (last_played < maxindex) 
              ? last_played + 1
              : 0
              ;
        
        mlist.performItemClick(mlist.getChildAt(last_played),
                        last_played, 
                        mlist_adapter.getItemId(last_played));
        stop();
                play(last_played);
      }
    });
    }
    
    public String getPlayTimeAlternative() {
      int tsecs = seek_pos / 1000;
      
      int hours = tsecs / 3600;
      int res   = tsecs % 3600;
      int mins  = res / 60;
      int secs  = res % 60;
      
      String formatted_time = new String();
      
      String[] sarr = new String[] {new String(), new String(), new String()};
      int[]   iarr = new int[] {hours, mins, secs};
      
      for (int i=0; i < iarr.length; ++i) {
        int val = iarr[i];
        
        sarr[i] = (val >= 10) 
              ? Integer.valueOf(val).toString()
              : (val == 0) ? "00" : "0" + Integer.valueOf(val).toString()
              ;
      
        formatted_time += (i == 0)
                ? sarr[i]
                : ":" + sarr[i]
                ;
      }
      
      return formatted_time;
    }
    
    public String getPlayTime() {
      // Have to use this instead of the "alternative"
      // version due to speed reasons :-(
      
      int tsecs = seek_pos / 1000;
      
      int hours = tsecs / 3600;
      int res   = tsecs % 3600;
      int mins  = res / 60;
      int secs  = res % 60;
      
      return
        ((hours >= 10)
             ? Integer.valueOf(hours).toString()
             : (hours == 0) ? "00" : "0" + Integer.valueOf(hours).toString())
             + ":"
             +
        ((mins  >= 10)
             ? Integer.valueOf(mins).toString()
             : (mins == 0) ? "00" : "0" + Integer.valueOf(mins).toString())
             + ":"
             +
        ((secs  >= 10)
             ? Integer.valueOf(secs).toString()
             : (secs == 0) ? "00" : "0" + Integer.valueOf(secs).toString())
             ;
    }
    
    public void start() {
      mplayer.start();
    }
    
    public void resume() {
      try {
        if (was_playing) {
          mplayer.start();
          runSeekTimeUpdater();
        }
      }
      catch (IllegalStateException ise) {
        ise.printStackTrace();
      }
    }
    
    public MediaPlayer getPlayer() {
      return mplayer;
    }
    
    public void destroy() {
      if (mplayer_initialized) {
        this.stop();
        mplayer.release();
      }
      
      instance = null;
    }
    
    public boolean play(int pos) {
      if (mplayer.isPlaying()) {
        if (last_played == pos) {
          mplayer.pause();
          return false;
        }
        else
          this.stop();
      }
      
      File fl;
      
      if (!mplayer_initialized) {
        try {
          fl = ((Mp3File) mlist_adapter.getItem(pos)).getFile();
          
          mplayer.setDataSource(fl.getPath());
          mplayer.prepareAsync();
          
          mplayer_initialized = true;
        }
        catch (IOException ioe) {
          ioe.printStackTrace();
        }
      } 
      else {
        mplayer.start();
        runSeekTimeUpdater();
      }
      
      last_played = pos;
      return true;
    }
    
    public void stop() {
      if (mplayer_initialized) {
        mplayer.stop();
        
        if (shandler != null)
          shandler.setProgress(0);
        
        if (pthandler != null)
          pthandler.setText("00:00:00");
        
        mplayer.reset();
        mplayer_initialized = false;
      }
    }
    
    private static void runSeekTimeUpdater() {
      if (hl != null && rn != null)
        hl.post(rn);
    }
}




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