try{ long totalAudioLen = 0; InputStream inputStream = getResources().openRawResource(R.raw.abordage); // open the file totalAudioLen = inputStream.available(); byte[] rawBytes = new byte[(int)totalAudioLen]; AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, (int)totalAudioLen, AudioTrack.MODE_STATIC); int offset = 0; int numRead = 0; track.setPlaybackHeadPosition(100); // IMPORTANT to skip the click while (offset < rawBytes.length && (numRead=inputStream.read(rawBytes, offset, rawBytes.length-offset)) >= 0) { offset += numRead; } //don't really know why it works, it reads the file track.write(rawBytes, 0, (int)totalAudioLen); //write it in the buffer? track.play(); // launch the play track.setPlaybackRate(88200); inputStream.close(); } catch (FileNotFoundException e) { Log.e(TAG, "Error loading audio to bytes", e); } catch (IOException e) { Log.e(TAG, "Error loading audio to bytes", e); } catch (IllegalArgumentException e) { Log.e(TAG, "Error loading audio to bytes", e); }
private static void beep() { if(audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) { // On the initial play, we play including the attenuated lead-in // After that, we periodically feed in full periods of wave to get a continuous beep. audioTrack.flush(); currentlyBeeping = true; // Call back every time this many frames have been played, so we know we can // enqueue another bit of audio data. audioTrack.setPositionNotificationPeriod(body); audioTrack.setPlaybackHeadPosition(0); audioTrack.write(sampHead, 0, sampHead.length); // Playback will not start if we haven't filled up minBuffer bytes. // The exact number is device-dependent! // Prime the buffer with enough full waveform periods. audioTrack.write(sampBody, 0, sampBody.length); audioTrack.write(sampBody, 0, sampBody.length); audioTrack.play(); } }
@Override public Object play (final InputStream ais, final StreamInfo streamInfo, final Object stopMonitor, final int skipMilliSeconds) throws PlayObjectException { final int channelConf = this.getChannelConfiguration (streamInfo); final int frameLength; try { frameLength = findFrameLength (ais, streamInfo); } catch (IOException ioe) { throw new PlayObjectException (new SoundTransformException (PlaySoundErrorCode.COULD_NOT_PLAY_SOUND, ioe)); } final AudioTrack audioTrack = new AudioTrack (AudioManager.STREAM_MUSIC, (int) streamInfo.getSampleRate (), channelConf, streamInfo.getSampleSize () == AndroidPlayObjectProcessor.TWO ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT, frameLength * streamInfo.getSampleSize (), AudioTrack.MODE_STATIC); final byte [] baSoundByteArray = new byte [frameLength * streamInfo.getSampleSize ()]; try { final int byteArraySize = ais.read (baSoundByteArray); this.log (new LogEvent (AndroidPlaySoundProcessorEventCode.READ_BYTEARRAY_SIZE, byteArraySize)); } catch (final IOException e1) { throw new PlayObjectException (new SoundTransformException (PlaySoundErrorCode.COULD_NOT_PLAY_SOUND, e1)); } audioTrack.write (baSoundByteArray, 0, baSoundByteArray.length); audioTrack.flush (); audioTrack.setPlaybackHeadPosition ((int) (streamInfo.getSampleRate () * 1.0 * skipMilliSeconds / 1000.0)); audioTrack.play (); final Thread soundMonitorThread = this.getSoundMonitorThread (stopMonitor, audioTrack); final Thread playFrameMonitorThread = this.getPlayFrameMonitorThread (stopMonitor, audioTrack); playFrameMonitorThread.start (); if (soundMonitorThread != null) { soundMonitorThread.start (); } return playFrameMonitorThread; }