Android Open Source - UltraExplorer Player Activity






From Project

Back to project page UltraExplorer.

License

The source code is released under:

GNU General Public License

If you think the Android project UltraExplorer 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

package com.mirrorlabs.musicplayer;
/*w ww. j  av a 2  s  .  c  om*/
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.mirrorlabs.filebrowser.FileUtils;
import com.mirrorlabs.filebrowser.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;



public class PlayerActivity extends Activity implements
    MediaPlayer.OnCompletionListener {

  private ImageButton bt_play = null;
  private ImageButton bt_last = null;
  private ImageButton bt_next = null;
  private ImageButton bt_forward = null;
  private ImageButton bt_backward = null;

  private SeekBar sb_progress = null;

  private int[] musicids;
  private String[] musictitles;
  private String[] musicartists;
  private int position;
  
  String songname;
  String artistname;

  private TextView title;
  private TextView playtime;
  private TextView duration;
  private TextView artist;

  private MediaPlayer player;

  // private DelayThread dThread;
  private Handler fb_handler;
  private Handler refresh_handler;

  private final int RANDOM_MODE = 1;
  private final int ALL_LOOP_MODE = 2;
  private final int SINGLE_LOOP_MODE = 3;

  private int currentMode = -1;
  
  private int currentBg = 0;

  private DBHelper dbHelper = null;
  private Cursor cursor = null;
  private Uri path;

  /*
   * private Handler mHandle = new Handler() {
   * 
   * @Override public void handleMessage(Message msg) { // TODO Auto-generated
   * method stub super.handleMessage(msg);
   * 
   * if (null != player) { int position = player.getCurrentPosition(); int
   * playMax = player.getDuration(); int sbMax = sb_progress.getMax();
   * 
   * sb_progress.setProgress(position * sbMax / playMax); } }
   * 
   * };
   */

  @Override
  public void onCompletion(MediaPlayer _player) {
    // TODO Auto-generated method stub
    //position = getNext();
    resetPlayer();
    play();
  }

  private int getNext() {
    int nextPosition = 0;
    int sizeofAll = musicids.length;
    switch (currentMode) {
    case RANDOM_MODE:
      Random ran = new Random();
      nextPosition = ran.nextInt(sizeofAll-1);
      break;

    case SINGLE_LOOP_MODE:
      nextPosition = position;
      break;

    case ALL_LOOP_MODE:
      if (position == sizeofAll - 1) {
        nextPosition = 0;
      } else {
        nextPosition = position + 1;
      }
      break;

    default:
      break;
    }
    return nextPosition;
  }

  private boolean releaseAll() {
    if (null != player) {
      player.reset();
      player.release();
      player = null;
    }
    if (null != fb_handler) {
      fb_handler.removeCallbacks(forward);
      fb_handler.removeCallbacks(backward);
      fb_handler = null;
    }
    if (null != dbHelper) {
      dbHelper.close();
      dbHelper = null;
    }
    if (null != refresh_handler) {
      refresh_handler.removeCallbacks(refresh);
      refresh_handler = null;
    }
    sb_progress.setProgress(0);
    playtime.setText("00:00");
    duration.setText("00:00");
    return false;
  }

  private void setMedia() {

    title.setText(songname);
    artist.setText(artistname);
    //player = new MediaPlayer();

    //Uri uri = Uri
      //  .withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
          //  "" + musicids[pos]);

    fb_handler = new Handler();
    refresh_handler = new Handler();
    // player = MediaPlayer.create(getApplicationContext(), R.raw.a);
    try {
      player = new MediaPlayer();
      player.setDataSource(getApplicationContext(), path);
      player.prepare();
      player.setOnCompletionListener(this);
    } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  private void resetPlayer() {
    releaseAll();
    setMedia();
  }

  private boolean play() {
    if (null != player) {
      try {
        // player.prepare();
        player.start();
        bt_play.setBackgroundResource(R.drawable.pause_selecor);
        // startProgressUpdate();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      if (null != refresh_handler) {
        refresh_handler.postDelayed(refresh, 500);
      }
      return true;
    }
    return false;
  }

  private void updateMusicDB(int position) {
    dbHelper = new DBHelper(this, "music.db", null, 3);
    Date currenttime = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateStr = format.format(currenttime);
    cursor = dbHelper.queryMusic(position);
    cursor.moveToFirst();
    if (cursor == null || cursor.getCount() == 0) {
      ContentValues values = new ContentValues();
      values.put("music_id", position);
      values.put("clicks", 1);
      values.put("latest", dateStr);
      values.put("list1", 0);
      values.put("list2", 0);
      values.put("list3", 0);
      values.put("list4", 0);
      values.put("list5", 0);
      dbHelper.insertMusic(values);
    } else {
      int clicks = cursor.getInt(2);
      clicks++;
      ContentValues values = new ContentValues();
      values.put("clicks", clicks);
      values.put("latest", dateStr);
      dbHelper.updateMusic(values, position);
    }
    if (dbHelper != null) {
      dbHelper.close();
      dbHelper = null;
    }
  }

  @SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi", "NewApi" })
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.music_player);

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    path =Uri.parse(bundle.getString("path"));
    
    File mFile = new File(bundle.getString("filePath"));
    

    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(mFile.getAbsolutePath());
    
      songname = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
      artistname =mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);

       
        musicids = bundle.getIntArray("musicids");
    musictitles = bundle.getStringArray("musictitles");
    position = bundle.getInt("position");
    musicartists = bundle.getStringArray("musicartists");
    currentMode = bundle.getInt("playmode");
    
    

    title = (TextView) findViewById(R.id.name);
    playtime = (TextView) findViewById(R.id.playtime);
    duration = (TextView) findViewById(R.id.duration);
    artist = (TextView) findViewById(R.id.artist);
 
    bt_play = (ImageButton) findViewById(R.id.play);
    bt_play.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View arg0) {
        // TODO Auto-generated method stub

        if (null != player) {
          if (player.isPlaying()) {
            player.pause();
            // releaseAll();
            bt_play.setBackgroundResource(R.drawable.play_selecor);
          } else {
            player.start();
            // setMedia(0);
            play();
            bt_play.setBackgroundResource(R.drawable.pause_selecor);
          }
        } /*
         * else { setMedia(0); play();
         * bt_play.setBackgroundResource(R.drawable.pause_selecor); }
         */
      }
    });

    sb_progress = (SeekBar) findViewById(R.id.seekbar);
    sb_progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

      @Override
      public void onStopTrackingTouch(SeekBar sb) {
        // TODO Auto-generated method stub
        if (null != player) {
          int dest = sb_progress.getProgress();
          int playerMax = player.getDuration();
          int sbMax = sb_progress.getMax();
          player.seekTo(dest * playerMax / sbMax);
        }
      }

      @Override
      public void onStartTrackingTouch(SeekBar sb) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onProgressChanged(SeekBar sb, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        // if (arg2) {
        // player.seekTo(arg1);
        // }
      }
    });

    bt_forward = (ImageButton) findViewById(R.id.forward);
    bt_forward.setOnTouchListener(new OnTouchListener() {

      @Override
      public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          if (null != player) {
            fb_handler.post(forward);
            player.pause();
          }
          break;

        case MotionEvent.ACTION_UP:
          if (null != player) {
            fb_handler.removeCallbacks(forward);
            player.start();
          }
          break;

        default:
          break;
        }
        return false;
      }
    });

    bt_backward = (ImageButton) findViewById(R.id.backward);
    bt_backward.setOnTouchListener(new OnTouchListener() {

      @Override
      public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          if (null != player) {
            fb_handler.post(backward);
            player.pause();
          }
          break;

        case MotionEvent.ACTION_UP:
          if (null != player) {
            fb_handler.removeCallbacks(backward);
            player.start();
          }
          break;

        default:
          break;
        }
        return false;
      }
    });

    bt_next = (ImageButton) findViewById(R.id.next);
    bt_next.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View view) {
        // TODO Auto-generated method stub
        int max = musicids.length;
        if (position == max - 1) {
          position = 0;
        } else {
          position++;
        }
        releaseAll();
        setMedia();
        play();
      }
    });

    bt_last = (ImageButton) findViewById(R.id.last);
    bt_last.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View view) {
        // TODO Auto-generated method stub
        int max = musicids.length;
        if (position == 0) {
          position = max - 1;
        } else {
          position--;
        }
        releaseAll();
        setMedia();
        play();
      }
    });
  }
  
  private String getRealPathFromURI(Uri contentURI) {
    String[] STAR = { "*" };     
        Uri allaudiosong = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String audioselection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    Cursor cursor;
    cursor = PlayerActivity.this.managedQuery(allaudiosong, STAR, audioselection, null, null);
      cursor.moveToFirst(); 
      int idx = cursor.getColumnIndex(MediaStore.Audio.Media.DATA); 
      return cursor.getString(idx); 
  }


  @Override
  protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    if (null == player) {
      resetPlayer();
      //updateMusicDB(musicids[position]);
      play();
    }
  }

  private Runnable forward = new Runnable() {

    @Override
    public void run() {
      // TODO Auto-generated method stub
      if (null != player) {
        int current = player.getCurrentPosition();
        int duration = player.getDuration();
        if (current < duration) {
          current += 5000;
          player.seekTo(current);
          fb_handler.postDelayed(forward, 500);
        }
      } else {
        fb_handler.removeCallbacks(forward);
      }

    }
  };

  private Runnable backward = new Runnable() {

    @Override
    public void run() {
      // TODO Auto-generated method stub
      if (null != player) {
        int current = player.getCurrentPosition();
        if (current > 0) {
          current -= 5000;
          player.seekTo(current);
          fb_handler.postDelayed(backward, 500);
        }
      } else {
        fb_handler.removeCallbacks(forward);
      }

    }
  };

  private Runnable refresh = new Runnable() {

    @Override
    public void run() {
      // TODO Auto-generated method stub
      /*
       * try { sleep(100); } catch (InterruptedException e) { // TODO
       * Auto-generated catch block e.printStackTrace(); }
       */

      // mHandle.sendEmptyMessage(0);
      if (null != player) {
        int position = player.getCurrentPosition();
        int playMax = player.getDuration();
        int sbMax = sb_progress.getMax();

        sb_progress.setProgress(position * sbMax / playMax);
        String cur = toTime(player.getCurrentPosition());
        String all = toTime(player.getDuration());
        playtime.setText(cur);
        duration.setText(all);
        refresh_handler.postDelayed(refresh, 500);
      }
    }
  };

  /*
   * public class DelayThread extends Thread { // int milliseconds; private
   * TextView pTime; private TextView aTime;
   * 
   * public DelayThread(TextView _pTime, TextView _aTime) { this.pTime =
   * _pTime; this.aTime = _aTime; }
   * 
   * public void run() { while (true) { try { sleep(100); } catch
   * (InterruptedException e) { // TODO Auto-generated catch block
   * e.printStackTrace(); }
   * 
   * // mHandle.sendEmptyMessage(0); if (null != player) { int position =
   * player.getCurrentPosition(); int playMax = player.getDuration(); int
   * sbMax = sb_progress.getMax();
   * 
   * sb_progress.setProgress(position * sbMax / playMax); String cur =
   * toTime(player.getCurrentPosition()); String all =
   * toTime(player.getDuration()); playtime.setText(cur);
   * duration.setText(all); } } } }
   */

  /*
   * public void startProgressUpdate() { // ????Thread ??????????SeekBar dThread =
   * new DelayThread(playtime, duration); dThread.start(); }
   */

  public String toTime(int time) {

    time /= 1000;
    int minute = time / 60;
    int second = time % 60;
    minute %= 60;
    return String.format("%02d:%02d", minute, second);
  }

  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    releaseAll();
  }

}




Java Source Code List

com.android.gestures.CreateGestureActivity.java
com.android.gestures.GestureBuilderActivity.java
com.android.gestures.GestureMonitorActivity.java
com.markupartist.android.widget.ActionBar.java
com.markupartist.android.widget.ScrollingTextView.java
com.mirrorlabs.customcheckboxwidget.DontPressWithParentCheckBox.java
com.mirrorlabs.customtoast.Toaster.java
com.mirrorlabs.filebrowser.BackupManager.java
com.mirrorlabs.filebrowser.BaseActivity.java
com.mirrorlabs.filebrowser.BitmapManager.java
com.mirrorlabs.filebrowser.BookmarksProvider.java
com.mirrorlabs.filebrowser.CMDProcessor.java
com.mirrorlabs.filebrowser.CompressManager.java
com.mirrorlabs.filebrowser.DesEncrypter.java
com.mirrorlabs.filebrowser.DrawableManager.java
com.mirrorlabs.filebrowser.DrawableThreadLoader.java
com.mirrorlabs.filebrowser.DuplicatesManager.java
com.mirrorlabs.filebrowser.ExtractManager.java
com.mirrorlabs.filebrowser.FileUtils.java
com.mirrorlabs.filebrowser.FilebrowserULTRAActivity.java
com.mirrorlabs.filebrowser.ImageThreadLoader.java
com.mirrorlabs.filebrowser.LinuxShell.java
com.mirrorlabs.filebrowser.MimeTypes.java
com.mirrorlabs.filebrowser.MyApplication.java
com.mirrorlabs.filebrowser.PDFViewer.java
com.mirrorlabs.filebrowser.PreferenceActivity.java
com.mirrorlabs.filebrowser.ProcessManager.java
com.mirrorlabs.filebrowser.RootUtils.java
com.mirrorlabs.filebrowser.SearchFilesDialog.java
com.mirrorlabs.filebrowser.SearchFilesWidget.java
com.mirrorlabs.filebrowser.UltraBaseAdapter.java
com.mirrorlabs.imageviewer.EclairMotionEvent.java
com.mirrorlabs.imageviewer.ExpandImage.java
com.mirrorlabs.imageviewer.TouchImageView.java
com.mirrorlabs.imageviewer.WrapMotionEvent.java
com.mirrorlabs.menupopup.MenuItem.java
com.mirrorlabs.menupopup.PopupMenu.java
com.mirrorlabs.musicplayer.DBHelper.java
com.mirrorlabs.musicplayer.PlayerActivity.java
com.mirrorlabs.quickaction3D.ActionItem.java
com.mirrorlabs.quickaction3D.PopupWindows.java
com.mirrorlabs.quickaction3D.QuickAction.java
com.mirrorlabs.quickaction.ActionItem.java
com.mirrorlabs.quickaction.PopupWindows.java
com.mirrorlabs.quickaction.QuickAction.java
com.mirrorlabs.ui.widgets.ClickableSlidingDrawer.java
com.mirrorlabs.ui.widgets.DropDownMenu.java
com.mirrorlabs.ui.widgets.IcsListPopupWindow.java
com.mirrorlabs.ui.widgets.JavaYoutubeDownloader.java
com.mirrorlabs.ui.widgets.Panel.java
com.mirrorlabs.ui.widgets.ScrollPager.java
com.mirrorlabs.ui.widgets.SlidingFrameLayout.java
com.mirrorlabs.widgets.ExampleAppWidgetProvider1.java
com.mirrorlabs.widgets.ExampleAppWidgetProvider.java
com.mirrorlabs.widgets.UpdateWidgetService1.java
com.mirrorlabs.widgets.UpdateWidgetService.java
org.zeroxlab.widget.AnimationLayout.java
org.zeroxlab.widget.MyHorizontalScrollView.java