@Override
 public void onPrepared(MediaPlayer mpx) {
   Logger.d(TAG, "on Prepared!");
   mpx.setOnCompletionListener(this);
   Logger.d(TAG, "ok, I'v set the on Completion Listener again...");
   mpx.start();
 }
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (action.equals(ACTION_START_MUSIC)) {
     Logger.d(TAG, "I GOT THE START MUSIC THING!");
     actionID = intent.getStringExtra("AmbientActionID");
     MusicAction ma = (MusicAction) ActionManager.getActionByID(actionID);
     play(ma);
   }
   if (action.equals(ACTION_STOP_MUSIC)) {
     Logger.d(TAG, "STOooooooooP");
     String newactionID = intent.getStringExtra("AmbientActionID");
     if (newactionID.equals(actionID)) {
       stop();
       actionID = "";
     }
   }
   if (action.equals(ACTION_SWITCH_TO_RADIO)) {
     Logger.d(TAG, "switchtoradio....");
     actionID = intent.getStringExtra("AmbientActionID");
     MusicAction ma = (MusicAction) ActionManager.getActionByID(actionID);
     String station = Radiostations.stations.get(ma.getRadiourl());
     turnOnRadio(station);
   }
   if (action.equals(ACTION_TURN_MEDIASERVICE_PERMNENTLY_OFF)) {
     Logger.d(TAG, "turneverythingoff");
     stopeverything();
   }
 }
  public void turnOnRadio(String station) {
    if (mp != null) {
      mp.stop();
      mp.release();
    }
    Logger.d("clock", "turnOnRadio");
    try {
      Logger.d(TAG, "try");
      mp = new MediaPlayer();
      mp.setScreenOnWhilePlaying(true);
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
      Logger.d(TAG, "Station---------------------" + station);
      try {
        mp.setDataSource(station);
      } catch (Exception e) {
        Logger.d(TAG, "default radio");
        try {
          mp.setDataSource(Radiostations.stations.get("DLF"));
        } catch (Exception ex) {
          Logger.d(TAG, "f**k this");
        }
      }
      mp.setVolume(0.99f, 0.99f);
      mp.setOnCompletionListener(this);
      mp.setOnPreparedListener(this);
      mp.prepareAsync();

    } catch (Exception ee) {
      Logger.e("Error", "No Stream");
    }
  }
  public ActionConfigBundle(String semiColonSeparatedData) {
    content = new Bundle();
    Logger.i(TAG, semiColonSeparatedData);
    StringTokenizer tk = new StringTokenizer(semiColonSeparatedData, ";");
    while (tk.hasMoreTokens()) {
      try {
        String key = tk.nextToken();
        String value = tk.nextToken();
        content.putString(key, value);

      } catch (Exception e) {
        Logger.d(TAG, "Exception in ActionConfigBundle Constructor");
      }
    }
  }
  private void playmp3() {
    boolean mExternalStorageAvailable = false;
    String state = Environment.getExternalStorageState();
    Logger.d(TAG, "Go Play 3");
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      // We can read and write the media
      mExternalStorageAvailable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
      // We can only read the media
      mExternalStorageAvailable = true;
    } else {
      // Something else is wrong. It may be one of many other states, but all we need
      //  to know is we can neither read nor write
      mExternalStorageAvailable = false;
    }
    if (mExternalStorageAvailable) {
      // Log.i(TAG, Environment.getExternalStorageState());
      File filesystem = Environment.getExternalStorageDirectory();
      if (usedropboxchecked) {
        Logger.d(
            TAG,
            "Music Player knows that we should use a foder that is synced with a cloud storage");
        localfolderstring =
            filesystem.getPath()
                + "/"
                + AlarmClockConstants.BASE_FOLDER
                + "/"
                + AmbientAlarmManager.getAlarmByRegisteredAction(actionID).getAlarmID()
                + "/"
                + actionID;
      }
      File file = new File(localfolderstring);
      // Log.d(TAG, "wakeupsongsX");
      File[] filelist3 = new File[0];
      // Log.d(TAG, "--1");
      try {
        filelist3 = file.listFiles();
      } catch (Exception e) {
        Logger.d(TAG, "Could not get Filelist");
      }
      // Log.d(TAG, "--2");
      // count mp3s
      int numberOfMp3 = 0;
      String musicpath = "";
      if (filelist3 != null) {
        for (int i = 0; i < filelist3.length; i++) {
          // Log.d(TAG, "--2b");
          if (filelist3[i].getName().endsWith("mp3")) {
            // Log.d(TAG, "Number of MP3s");
            numberOfMp3++;
          }
        }
        // Log.d(TAG, "FILELIST LENGTH " + numberOfMp3);
        if (numberOfMp3 > 0) {
          int randomsongnumber = (int) (Math.random() * (filelist3.length));
          musicpath = filelist3[randomsongnumber].getAbsolutePath();
          File f = new File(musicpath);
          String artist = "";
          String song = "";
          try {
            MP3File mp3 = new MP3File(f);
            ID3v1 id3 = mp3.getID3v1Tag();
            artist = id3.getArtist();
            // Log.d(TAG, "----------->ARTIST:" + artist);
            song = id3.getSongTitle();
            // Log.d(TAG, "----------->SONG:" + song);
            Scrobbler.scrobble(artist, song);
          } catch (IOException e1) {
            e1.printStackTrace();
          } catch (TagException e1) {
            e1.printStackTrace();
          } catch (Exception ex) {
            // Log.e(TAG, "There has been an exception while extracting ID3 Tag Information from the
            // MP3");
          }
        }
      }
      try {
        mp = new MediaPlayer();
        if (numberOfMp3 == 0) {
          Logger.d(TAG, "DEFAULT FILE");
          mp = MediaPlayer.create(this, mediaarray[0]);
          // mp.setLooping(true);
          mp.setVolume(0.99f, 0.99f);
          mp.setOnCompletionListener(this);
          mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
          mp.setOnPreparedListener(this);
          mp.prepareAsync();
        } else {
          mp.setDataSource(musicpath);
          mp.setLooping(false);
          mp.setVolume(0.99f, 0.99f);
          Logger.d(TAG, "...");
          mp.setOnCompletionListener(this);
          mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
          Logger.d(TAG, "....");
          mp.setOnPreparedListener(this);
          Logger.d(TAG, ".....");
          mp.prepareAsync();
        }

      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalStateException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      Logger.d(TAG, "not read or writeable...");
    }
  }
 @Override
 public boolean onError(MediaPlayer mp, int what, int extra) {
   Logger.d(TAG, "MEDIAPLAYER ON ERROR");
   playmp3();
   return true;
 }
 @Override
 public void onCompletion(MediaPlayer mpx) {
   Logger.d(TAG, "on Completetion");
   playmp3();
 }
 public void stop() {
   Logger.d(TAG, "stop Media Player Service");
   mp.stop();
   mp.release();
   mp = null;
 }