// Set Activity
 public void setActivity(InterfacePlayer activity) {
   this.activity = activity;
   if (activity != null) {
     activity.onPluginsLoaded(plugins);
     activity.onFileLoaded(null, duration, "", "", 0, 0);
     activity.onProgressChanged(progress);
   }
 }
  // 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();
    }
  }
  @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 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();
 }
        @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 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);
   }
 }
  public void onPlayError(String e) {
    // Update Properties
    this.duration = 0.0;
    this.progress = 0.0;

    // Notify activity
    if (activity != null) {
      activity.onFileLoaded(tracks.get(position), this.duration, "", "", 0, 0);
      activity.onProgressChanged(progress);
      activity.onUpdatePlayPause();
    }

    stopUpdateProgress();

    // skip 1st n errors on play
    if (errorCount < 3) {
      errorCount++;
      playNext();
    } else {
      FlurryAgent.onError("onPlayError", e, "");
      stop();
    }
  }