Exemple #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();
    }
  }
  public int enablePreviewEvents(
      int type, int memBuffer, int left, int top, int width, int height) {
    if (mPreview == null && mCamera == null) return MAAPI_consts.MA_CAMERA_RES_FAILED;

    if (mMoSyncPreviewEventEnabled) return MAAPI_consts.MA_CAMERA_RES_EVENTS_ALREADY_ENABLED;

    if (type == MAAPI_consts.MA_CAMERA_PREVIEW_FRAME) {
      if (mMoSyncPreviewAutoFocusEventEnabled)
        return MAAPI_consts.MA_CAMERA_RES_EVENTS_ALREADY_ENABLED;

      // enable preview frame events
      mMoSyncPreviewFrameEventEnabled = true;

    } else if (type == MAAPI_consts.MA_CAMERA_PREVIEW_AUTO_FOCUS) {
      if (mMoSyncPreviewFrameEventEnabled) return MAAPI_consts.MA_CAMERA_RES_EVENTS_ALREADY_ENABLED;

      // enable preview auto foucs events
      mMoSyncPreviewAutoFocusEventEnabled = true;

    } else return MAAPI_consts.MA_CAMERA_RES_FAILED;

    // Get the size of the preview and make sure the rect
    // ins't too big!
    // Camera.Parameters camParam = mCamera.getParameters();
    Camera.Size previewSize = mPreview.mPreviewSize;

    if (previewSize == null) {
      SYSLOG("Preview size is null");
      return MAAPI_consts.MA_CAMERA_RES_FAILED;
    }

    int w = previewSize.width;
    int h = previewSize.height;

    if ((left + width) > w) return MAAPI_consts.MA_CAMERA_RES_INVALID_PREVIEW_SIZE;
    if ((top + height) > h) return MAAPI_consts.MA_CAMERA_RES_INVALID_PREVIEW_SIZE;

    int sliceSize = width * height * 4;

    mMoSyncPreviewEventBuffer = mMoSyncThread.getMemorySlice(memBuffer, sliceSize).asIntBuffer();

    if (mMoSyncPreviewEventBuffer == null) return MAAPI_consts.MA_CAMERA_RES_FAILED;

    mPreviewEventBufferLeft = left;
    mPreviewEventBufferTop = top;
    mPreviewEventBufferRight = left + width;
    mPreviewEventBufferBottom = top + height;

    mMoSyncPreviewEventEnabled = true;

    return MAAPI_consts.MA_CAMERA_RES_OK;
  }
 @Override
 public void onPictureTaken(byte[] data, Camera camera) {
   if (rawMode == false) {
     lock.lock();
     try {
       mMoSyncThread.nativeCreateBinaryResource(resourceIndex, data.length);
       ByteBuffer byteBuffer = mMoSyncThread.mBinaryResources.get(resourceIndex);
       byteBuffer.put(data);
       dataReady = true;
       condition.signalAll();
     } catch (Exception e) {
       SYSLOG("Failed to create the data pool");
     } finally {
       lock.unlock();
     }
   }
 }
Exemple #4
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;
    }
  }
Exemple #5
0
 /** @return The Activity object. */
 private Activity getActivity() {
   return mMoSyncThread.getActivity();
 }
Exemple #6
0
  /**
   * 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();
    }
  }
 /** @return The Activity object. */
 public Activity getActivity() {
   return mMoSyncThread.getActivity();
 }