예제 #1
0
  /**
   * If this is an audio ubin, we store the resource as a file that can be played with MediaPlayer.
   *
   * @param ubinData An object honding information about this resource.
   * @param resHandle The id of the audio resource.
   */
  public void storeIfAudioUBin(UBinData ubinData, int resHandle) {
    SYSLOG("MoSyncSound.storeIfAudioUBin - ubinData.getSize(): " + ubinData.getSize() + "bytes");

    if (!checkIfMimeAudioType(ubinData)) {
      // This is not an audio resource.
      return;
    }

    try {
      // Open the resource file.
      AssetManager assetManager = getActivity().getAssets();
      // RESOURCE_FILE = "resources.mp3"
      AssetFileDescriptor fileDescriptor = assetManager.openFd(MoSyncThread.RESOURCE_FILE);
      FileInputStream inputStream = fileDescriptor.createInputStream();

      // Jump to beginning of resource.
      inputStream.skip(ubinData.getOffset() - mMoSyncThread.getResourceStartOffset());

      // Read mime string.
      String mimeType = readMimeStringFromFile(inputStream);
      int mimeStringLength = mimeType.length() + 1;

      // Calculate size of audio data.
      int length = ubinData.getSize() - mimeStringLength;

      // Create buffer to hold audio data.
      byte[] buffer = new byte[length];

      // We should be at the start of audio data after reading the mime string.
      inputStream.read(buffer);

      // Close input stream.
      inputStream.close();

      // Create a temporary audio file
      String fileName = "MOSYNCTEMP:audio" + resHandle + ".tmp";
      FileOutputStream outputStream =
          getActivity()
              .openFileOutput(fileName, Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);

      // Write audio data.
      outputStream.write(buffer);

      // Close output steram.
      outputStream.close();

      // Store sound data in audio table.
      // TODO: Unify AudioStore with UBinData ?
      mAudioStores.put(resHandle, new AudioStore(mimeType, length, fileName));
    } catch (Exception ex) {
      Log.e("MoSyncAudio.storeIfAudioUBin", "Unable to save temporary audio file.");
      ex.printStackTrace();
    }
  }
예제 #2
0
  /**
   * Checks if the header of a resource is of audio mime type.
   *
   * @param ubinData A description of a ubin resource.
   * @return true if the header is of type "audio", false if not.
   */
  final boolean checkIfMimeAudioType(UBinData ubinData) {
    try {
      // Open the resource file.
      AssetManager assetManager = getActivity().getAssets();
      // RESOURCE_FILE = "resources.mp3"
      AssetFileDescriptor fileDescriptor = assetManager.openFd(MoSyncThread.RESOURCE_FILE);
      FileInputStream stream = fileDescriptor.createInputStream();

      // Jump to start of this audio resource.
      stream.skip(ubinData.getOffset() - mMoSyncThread.getResourceStartOffset());

      // Determine if this is an audio file by examining the header bytes.
      byte header[] = new byte[5];
      stream.read(header);
      stream.close();
      return checkIfMimeAudioType(header);
    } catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
  }