@Override
  public void onDestroy() {
    if (tm != null) {
      tm.listen(telephone, PhoneStateListener.LISTEN_NONE);
      tm = null;
    }
    if (myBroadcastReceiver != null) {
      unregisterReceiver(myBroadcastReceiver);
    }
    if (mAudioManager != null) {
      mAudioManager.unregisterMediaButtonEventReceiver(
          new ComponentName(getPackageName(), RcvMediaControl.class.getName()));
    }
    if (Build.VERSION.SDK_INT >= 14 && remoteControlClient != null) {
      unregisterRemoteControl();
    }
    // "free" the output device and all plugins
    BASS.BASS_Free();
    BASS.BASS_PluginFree(0);

    // Stop foreground
    stopForeground(true);
    stopUpdateProgress();

    super.onDestroy();
  }
  @Override
  public void onCreate() {
    super.onCreate();

    // initialize default output device
    if (!BASS.BASS_Init(-1, 44100, 0)) {
      return;
    }

    // look for plugins
    plugins = "";
    String path = getApplicationInfo().nativeLibraryDir;
    String[] list = new File(path).list();
    for (String s : list) {
      int plug = BASS.BASS_PluginLoad(path + "/" + s, 0);
      if (plug != 0) { // plugin loaded...
        plugins += s + "\n"; // add it to the list
      }
    }
    if (plugins.equals("")) plugins = "no plugins - visit the BASS webpage to get some\n";
    if (activity != null) {
      activity.onPluginsLoaded(plugins);
    }
    BASS.BASS_SetConfig(BASS.BASS_CONFIG_BUFFER, 1000);
    Log.w("BASS.BASS_CONFIG_BUFFER", "" + BASS.BASS_GetConfig(BASS.BASS_CONFIG_BUFFER));
    // screen
    screenHeight =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
            .getInt("screenHeight", 1000);
    screenWidth =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
            .getInt("screenWidth", 800);

    // Pending Intend
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // tracklist
    updateTrackList();

    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(telephone, PhoneStateListener.LISTEN_CALL_STATE);
    myBroadcastReceiver = new MyBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
    intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
    registerReceiver(myBroadcastReceiver, intentFilter);

    mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    ComponentName rcvMedia = new ComponentName(getPackageName(), RcvMediaControl.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(rcvMedia);

    // Use the remote control APIs (if available) to set the playback state
    if (Build.VERSION.SDK_INT >= 14 && remoteControlClient == null) {
      registerRemoteControl(rcvMedia);
    }
  }
 public boolean isPlaying() {
   if (!(BASS.BASS_ACTIVE_PLAYING == BASS.BASS_ChannelIsActive(chan))) {
     stopForeground(true);
     stopUpdateProgress();
   }
   if (BASS.BASS_ACTIVE_PLAYING == BASS.BASS_ChannelIsActive(chan)) {
     startUpdateProgress();
   }
   return BASS.BASS_ACTIVE_PLAYING == BASS.BASS_ChannelIsActive(chan);
 }
        @Override
        public void run() {
          if (BASS.BASS_ChannelIsActive(chan) == BASS.BASS_ACTIVE_PLAYING) {
            if (activity != null && activityStarted) {

              progress =
                  BASS.BASS_ChannelBytes2Seconds(
                      chan, BASS.BASS_ChannelGetPosition(chan, BASS.BASS_POS_BYTE));
              activity.onProgressChanged(progress);
            }
          }
          timerHandler.postDelayed(this, 200); // looks like laggy timer on more then 200 values
        }
 public void stop() {
   BASS.BASS_ChannelStop(chan);
   stopUpdateProgress();
   if (activity != null) {
     activity.onUpdatePlayPause();
   }
   if (remoteControlClient != null) {
     updateRemoteControlState(RemoteControlClient.PLAYSTATE_STOPPED);
   }
 }
 public void playFromPause() {
   BASS.BASS_ChannelPlay(chan, false);
   startUpdateProgress();
   // Notify activity
   if (activity != null) {
     activity.onUpdatePlayPause();
   }
   if (remoteControlClient != null) {
     updateRemoteControlState(RemoteControlClient.PLAYSTATE_PLAYING);
   }
   fireNotification();
 }
 public void pause() {
   BASS.BASS_ChannelPause(chan);
   stopForeground(true);
   stopUpdateProgress();
   // Notify activity
   if (activity != null) {
     activity.onUpdatePlayPause();
   }
   // Tell any remote controls that our playback state is 'paused'.
   if (remoteControlClient != null) {
     updateRemoteControlState(RemoteControlClient.PLAYSTATE_PAUSED);
   }
 }
  // Play file
  public void play(int pos) {
    if (tracks == null) return;
    startUpdateProgress();

    this.position = pos;

    // Play File
    String path = "";
    if (tracks != null && tracks.size() > position) {
      currentTrack = tracks.get(position);
      path = currentTrack.getPath();
    }
    if (path.equals("")) {
      onPlayError("empty");
      return;
    }

    BASS.BASS_StreamFree(chan);
    if ((chan = BASS.BASS_StreamCreateFile(path, 0, 0, 0)) == 0) {
      onPlayError(path);

      // Stop Foreground
      stopForeground(true);

      return;
    }
    // Play File
    int result = BASS.BASS_ChannelSetSync(chan, BASS.BASS_SYNC_END, 0, EndSync, 0);
    // ByteBuffer byteBuffer = (ByteBuffer)BASS.BASS_ChannelGetTags(chan, BASS.BASS_TAG_ID3V2);

    BASS.BASS_ChannelPlay(chan, false);

    // Update Properties
    this.duration =
        BASS.BASS_ChannelBytes2Seconds(chan, BASS.BASS_ChannelGetLength(chan, BASS.BASS_POS_BYTE));
    this.progress = 0.0;

    // Notify Activity
    if (activity != null) {
      activity.onFileLoaded(
          currentTrack,
          this.duration,
          currentTrack.getArtist(),
          currentTrack.getTitle(),
          position,
          currentTrack.getAlbumId());
      activity.onProgressChanged(progress);
      activity.onUpdatePlayPause();
    }

    // Start foreground
    fireNotification();

    // Remote control
    if (Build.VERSION.SDK_INT >= 14 && remoteControlClient != null) {
      updateRemoteControl();
    }
  }
 public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   if (row == null) {
     // ROW INFLATION
     LayoutInflater inflater =
         (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     row = inflater.inflate(R.layout.mp3_list_item_layout, parent, false);
   }
   // Get item
   MP3Entity message = getItem(position);
   TextView messageArtistView = null;
   if (row != null) {
     messageArtistView = (TextView) row.findViewById(R.id.MP3artist);
   }
   SeekBar progressSeekBar = (SeekBar) row.findViewById(R.id.MP3SeekBar1);
   SeekBar volumeSeekBar = (SeekBar) row.findViewById(R.id.volumeSeekBar);
   messageArtistView.setText(
       message.getArtist() + " / " + message.getTime() + " / " + message.getTitle());
   ImageButton deleteBtn = (ImageButton) row.findViewById(R.id.deleteMP3Button);
   HashMap<String, String> messageMap = new HashMap<String, String>();
   messageMap.put("artist", message.getArtist());
   messageMap.put("directory", message.getDirectory());
   messageMap.put("time", message.getTime());
   messageMap.put("title", message.getTitle());
   deleteBtn.setTag(messageMap);
   deleteBtn.setOnClickListener(deleteCLickListener);
   ImageButton playBtn = (ImageButton) row.findViewById(R.id.MP3buttonPlay);
   playBtn.setTag(message);
   playBtn.setOnClickListener(playCLickListener);
   progressSeekBar.setTag(message.getDirectory());
   volumeSeekBar.setTag(message.getDirectory() + "volume");
   playBtn.setImageResource(R.drawable.play_states);
   if (message == PlayerState.getInstance().getCurrentMP3Entity()) {
     progressSeekBar.setVisibility(View.VISIBLE);
     volumeSeekBar.setVisibility(View.VISIBLE);
     CurrentControls.setCurrentMP3SeekBar(progressSeekBar);
     CurrentControls.setCurrentVolumeSeekBar(volumeSeekBar);
     if (!(BASS.BASS_ChannelIsActive(BASSUtil.getChan()) == BASS.BASS_ACTIVE_PAUSED))
       playBtn.setImageResource(R.drawable.pause_states);
   } else {
     progressSeekBar.setVisibility(View.INVISIBLE);
     volumeSeekBar.setVisibility(View.INVISIBLE);
     volumeSeekBar.setVisibility(View.INVISIBLE);
   }
   return row;
 }
 public boolean isPaused() {
   return BASS.BASS_ACTIVE_PAUSED == BASS.BASS_ChannelIsActive(chan);
 }
 // Seek to position
 public void seekTo(int progress) {
   BASS.BASS_ChannelSetPosition(
       chan, BASS.BASS_ChannelSeconds2Bytes(chan, progress), BASS.BASS_POS_BYTE);
 }