Example #1
0
 @Override
 public boolean handleMotionEvent(MotionEvent event) {
   if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
     int actionPointerIndex = event.getActionIndex();
     int action = event.getActionMasked();
     switch (action) {
       case MotionEvent.ACTION_MOVE:
         SDLJoystick joystick = getJoystick(event.getDeviceId());
         if (joystick != null) {
           for (int i = 0; i < joystick.axes.size(); i++) {
             InputDevice.MotionRange range = joystick.axes.get(i);
             /* Normalize the value to -1...1 */
             float value =
                 (event.getAxisValue(range.getAxis(), actionPointerIndex) - range.getMin())
                         / range.getRange()
                         * 2.0f
                     - 1.0f;
             SDLActivity.onNativeJoy(joystick.device_id, i, value);
           }
           for (int i = 0; i < joystick.hats.size(); i += 2) {
             int hatX =
                 Math.round(
                     event.getAxisValue(joystick.hats.get(i).getAxis(), actionPointerIndex));
             int hatY =
                 Math.round(
                     event.getAxisValue(joystick.hats.get(i + 1).getAxis(), actionPointerIndex));
             SDLActivity.onNativeHat(joystick.device_id, i / 2, hatX, hatY);
           }
         }
         break;
       default:
         break;
     }
   }
   return true;
 }
    /**
     * Opens a connection to the media source of the associated <tt>DataSource</tt>.
     *
     * @throws IOException if anything goes wrong while opening a connection to the media source of
     *     the associated <tt>DataSource</tt>
     */
    public synchronized void connect() throws IOException {
      javax.media.format.AudioFormat af = (javax.media.format.AudioFormat) getFormat();
      int channels = af.getChannels();
      int channelConfig;

      switch (channels) {
        case Format.NOT_SPECIFIED:
        case 1:
          channelConfig = AudioFormat.CHANNEL_IN_MONO;
          break;
        case 2:
          channelConfig = AudioFormat.CHANNEL_IN_STEREO;
          break;
        default:
          throw new IOException("channels");
      }

      int sampleSizeInBits = af.getSampleSizeInBits();
      int audioFormat;

      switch (sampleSizeInBits) {
        case 8:
          audioFormat = AudioFormat.ENCODING_PCM_8BIT;
          break;
        case 16:
          audioFormat = AudioFormat.ENCODING_PCM_16BIT;
          break;
        default:
          throw new IOException("sampleSizeInBits");
      }

      double sampleRate = af.getSampleRate();

      length =
          (int)
              Math.round(
                  20 /* milliseconds */ * (sampleRate / 1000) * channels * (sampleSizeInBits / 8));

      /*
       * Apart from the thread in which #read(Buffer) is executed, use the
       * thread priority for the thread which will create the AudioRecord.
       */
      setThreadPriority();
      try {
        int minBufferSize =
            AudioRecord.getMinBufferSize((int) sampleRate, channelConfig, audioFormat);

        audioRecord =
            new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT,
                (int) sampleRate,
                channelConfig,
                audioFormat,
                Math.max(length, minBufferSize));

        // tries to configure audio effects if available
        configureEffects();
      } catch (IllegalArgumentException iae) {
        IOException ioe = new IOException();

        ioe.initCause(iae);
        throw ioe;
      }

      setThreadPriority = true;
    }