Android AudioTrack - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android AudioTrack class.

This code example shows how to use the following methods: release, stop
thread = null;
		 
 
		if(oTrack!=null){
			oTrack.stop();
			oTrack.release();
			oTrack = null;
		} 
		if(iTrack!=null){
			iTrack.release();
			iTrack = null;
		} 
		 
	} 
 
	@Override 
	protected void appPause() { 
		if(broadcastReceiver != null){
			try{ 
Full Snippet Info
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: play
            mAudio = new AudioTrack(AudioManager.STREAM_MUSIC,
                    rate,
                    channels,
                    encoding,
                    bufSize,
                    AudioTrack.MODE_STREAM );
            mAudio.play();
        } 
        return mVirtualBufSize;
    } 
 
    public byte[] getBuffer() 
    { 
        return mAudioBuffer;
    } 
 
    public int deinitAudio() 
    { 
        if( mAudio != null )
        { 
Full Snippet Info
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getAudioFormat, getChannelCount, getSampleRate, getState
				AudioFormat.CHANNEL_CONFIGURATION_STEREO :
				AudioFormat.CHANNEL_CONFIGURATION_MONO);
 
		// avoid recreation if no parameters change 
		if (track != null &&
				track.getSampleRate() == rate &&
				track.getAudioFormat() == format &&
				track.getChannelCount() == channels)
			return true; 
 
		int bufferSize = AudioTrack.getMinBufferSize(
				rate, channelConfig, format) * 2;
		if (bufferSize < 1500)
			bufferSize = 1500;
 
		try { 
			track = new AudioTrack(
					AudioManager.STREAM_MUSIC,
					rate,
					channelConfig,
Full Snippet Info
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getPlayState, play, write
    } 
 
    @CalledByNative 
    private void playOutputBuffer(byte[] buf) {
        if (mAudioTrack != null) {
            if (AudioTrack.PLAYSTATE_PLAYING != mAudioTrack.getPlayState()) {
                mAudioTrack.play();
            } 
            int size = mAudioTrack.write(buf, 0, buf.length);
            if (buf.length != size) {
                Log.i(TAG, "Failed to send all data to audio output, expected size: " +
                        buf.length + ", actual size: " + size);
            } 
        } 
    } 
 
    @CalledByNative 
    private void setVolume(double volume) {
        if (mAudioTrack != null) {
            mAudioTrack.setStereoVolume((float) volume, (float) volume);
Full Snippet Info
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: play, setStereoVolume
		int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT);
		 
		if (minBufferSize < bufferSize * 4)
			minBufferSize = bufferSize * 4;
		 
		audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
		 
		float audioVolume = AudioTrack.getMaxVolume();
		audioTrack.setStereoVolume(audioVolume, audioVolume);
		 
		audioTrack.play();
	} 
	 
	public void updateSound() 
	{ 
		audioTrack.write(audioBuffer, 0, bufferSize);
	} 
 
	public void setPhysicalScreenResolution(int width, int height)
	{ 
Full Snippet Info
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getState, play, release, setPlaybackRate, write
        final int TEST_MODE = AudioTrack.MODE_STREAM;
        final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC;
         
        //-------- initialization -------------- 
        int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT);
        AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, 
                minBuffSize, TEST_MODE);
        byte data[] = new byte[minBuffSize/2];
        int outputSR = AudioTrack.getNativeOutputSampleRate(TEST_STREAM_TYPE);
        //--------    test        -------------- 
        track.write(data, 0, data.length);
        track.write(data, 0, data.length);
        assumeTrue(TEST_NAME, track.getState() == AudioTrack.STATE_INITIALIZED);
        track.play();
        assertTrue(TEST_NAME, track.setPlaybackRate(2*outputSR) == AudioTrack.SUCCESS);
        //-------- tear down      -------------- 
        track.release();
    } 
     
    //Test case 7: setPlaybackRate() and retrieve value, should be the same for half the content SR 

6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: play, write
 
    private void playPlayer(int previousState) {
        (new Thread() {
            @Override 
            public void run() { 
                audiotrack.play();
                audiotrack.write(audio, 0, audio.length);
            } 
        }).start();
    } 
 
    private void stopPlayer(boolean reinit) {
        if(audiotrack != null) {
            try { audiotrack.stop(); } catch(Exception e) { e.printStackTrace(); }
            audiotrack.release();
            audiotrack = null;
            audio = null;
        } 
        if(reinit) {
            initializeAudioTrack(); 
Full Snippet Info
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: play
	AndroidAudioDevice (int samplingRate, boolean isMono) {
		this.isMono = isMono;
		int minSize = AudioTrack.getMinBufferSize(samplingRate, isMono ? AudioFormat.CHANNEL_CONFIGURATION_MONO
			: AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT);
		track = new AudioTrack(AudioManager.STREAM_MUSIC, samplingRate, isMono ? AudioFormat.CHANNEL_CONFIGURATION_MONO
			: AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM);
		track.play();
		latency = minSize / (isMono?1:2);
	} 
 
	@Override public void dispose () { 
		track.stop();
		track.release();
	} 
 
	@Override public boolean isMono () { 
		return isMono;
	} 
 
	@Override public void writeSamples (short[] samples, int offset, int numSamples) {
Full Snippet Info
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getState, play, write
mAudioTrack = null;
    } 
 
    public void playBuffer(byte[] audioData, int bufferSize) {
        if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED)
            return; 
        if (mAudioTrack.write(audioData, 0, bufferSize) != bufferSize) {
            Log.w(TAG, "Could not write all the samples to the audio device");
        } 
        mAudioTrack.play();
    } 
 
} 
Full Snippet Info
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: play

Related Android AudioTrack Questions & Answers:

See Code Examples for Android AudioTrack Methods: