コード例 #1
0
ファイル: MoSyncSound.java プロジェクト: jaumem/MoSync
  /**
   * If the given resource is an audio file, store it as a file. This is done because we need an
   * audio file to play the sound.
   *
   * @param soundHandle The handle to the sound object. The audio object begins with a null
   *     terminated mime string, then follows audio data.
   */
  void storeIfBinaryAudioResource(int soundHandle) {
    ByteBuffer audioData = mMoSyncThread.getBinaryResource(soundHandle);
    if (null == audioData) {
      SYSLOG("MoSyncAudio.storeIfBinaryAudioResource: " + "Sound data object not found");
      return;
    }

    // Is this an audio object?
    if (!checkIfMimeAudioType(audioData)) {
      // No it was not.
      SYSLOG("MoSyncAudio.storeIfBinaryAudioResource: " + "Not an audio object!");
      return;
    }

    // Read the mime string.
    String mimeType = readMimeString(audioData);
    if (mimeType == null) {
      SYSLOG("MoSyncAudio.storeIfBinaryAudioResource: No mime type!");
      return;
    }

    // Determine the length of the audio data.
    int mimeStringLength = mimeType.length() + 1;
    int length = audioData.capacity() - mimeStringLength;

    SYSLOG(
        "MoSyncAudio.storeIfBinaryAudioResource"
            + " initial capacity: "
            + audioData.capacity()
            + " mimeLength: "
            + mimeType.length()
            + " audio length: "
            + length);

    try {
      // Create file name.
      String fileName = "MOSYNCTEMP:audio" + soundHandle + ".tmp";

      // Create a temporary audio file.
      FileOutputStream audioFileOutputStream =
          getActivity()
              .openFileOutput(fileName, Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);

      // Open a file channel to write audio data to.
      FileChannel channel = audioFileOutputStream.getChannel();

      // Set position to start of audio data.
      audioData.position(mimeStringLength);

      // Write data.
      channel.write(audioData);

      // Close the channel, also closes the output stream.
      channel.close();

      // Add entry to audio resource table.
      mAudioStores.put(soundHandle, new AudioStore(mimeType, length, fileName));
    } catch (Exception ex) {
      Log.e("MoSyncSound.storeIfBinaryAudioResource", "Unable to save temporary audio file.");
      ex.printStackTrace();
    }
  }