示例#1
0
  // "parent" is the AbstractID3-derived instance making use of this frame.
  public void write(final RandomAccessFile file, AbstractID3 parent) throws IOException {
    final String str;
    final Iterator<?> iterator;
    final byte[] buffer = new byte[6];
    final MP3File mp3 = new MP3File();
    mp3.seekMP3Frame(file);
    final long mp3start = file.getFilePointer();
    file.seek(0);
    ID3v2_3Frame frame;
    str = "ID3";
    for (int i = 0; i < str.length(); i++) {
      buffer[i] = (byte) str.charAt(i);
    }
    buffer[3] = 3;
    buffer[4] = 0;
    if (this.unsynchronization) {
      buffer[5] |= TagConstant.MASK_V23_UNSYNCHRONIZATION;
    }
    if (this.extended) {
      buffer[5] |= TagConstant.MASK_V23_EXTENDED_HEADER;
    }
    if (this.experimental) {
      buffer[5] |= TagConstant.MASK_V23_EXPERIMENTAL;
    }
    file.write(buffer);

    // write size
    file.write(sizeToByteArray((int) mp3start - 10));
    if (this.extended) {
      if (this.crcDataFlag) {
        file.writeInt(10);
        buffer[0] = 0;
        buffer[0] |= TagConstant.MASK_V23_CRC_DATA_PRESENT;
        file.write(buffer, 0, 2);
        file.writeInt(this.paddingSize);
        file.writeInt(this.crcData);
      } else {
        file.writeInt(6);
        file.write(buffer, 0, 2);
        file.writeInt(this.paddingSize);
      }
    }

    // write all frames
    iterator = this.getFrameIterator();
    while (iterator.hasNext()) {
      frame = (ID3v2_3Frame) iterator.next();
      frame.write(file, parent);
    }
  }
  public DocumentWrap crawl(String uriroot, File file) throws CrawlException {
    DocumentWrap document = new DocumentWrap();

    try {
      MP3File mp3file = new MP3File(file);

      document.setAttribute("bitrate", String.valueOf(mp3file.getBitRate()));

      if (mp3file.hasID3v1Tag()) {
        ID3v1 id = mp3file.getID3v1Tag();

        document.setAttribute("album", id.getAlbum());
        document.setAttribute("artist", id.getArtist());
        document.setAttribute("leadartist", id.getLeadArtist());
        document.setAttribute("comment", id.getComment());
        document.setAttribute("year", id.getYearReleased());
        document.setAttribute("trackno", id.getTrackNumberOnAlbum());
        document.setName(id.getTitle());
      }

      // Setup the document
      document.setSize((int) file.length());
      document.setType("audio/mp3");
      document.setId(file.getCanonicalPath());

      if (uriroot != null) document.setURL(getUrl(uriroot, file));

    } catch (FileNotFoundException e) {
      throw new CrawlException("File not found: " + file, e);
    } catch (IOException e) {
      throw new CrawlException("File: " + file, e);
    } catch (Exception e) {
      throw new CrawlException("File: " + file, e);
    }

    return document;
  }
  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...");
    }
  }