当前位置: 首页>>代码示例>>Java>>正文


Java AudioTrack类代码示例

本文整理汇总了Java中android.media.AudioTrack的典型用法代码示例。如果您正苦于以下问题:Java AudioTrack类的具体用法?Java AudioTrack怎么用?Java AudioTrack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


AudioTrack类属于android.media包,在下文中一共展示了AudioTrack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: playSound

import android.media.AudioTrack; //导入依赖的package包/类
/**
 * This method plays the sound data in the specified buffer.
 *
 * @param buffer specifies the sound data buffer.
 */
public void playSound(short[] buffer)
{
    final String funcName = "playSound";

    if (debugEnabled)
    {
        dbgTrace.traceEnter(funcName, TrcDbgTrace.TraceLevel.API);
        dbgTrace.traceExit(funcName, TrcDbgTrace.TraceLevel.API);
    }

    audioTrack = new AudioTrack(
            AudioManager.STREAM_MUSIC,
            sampleRate,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            buffer.length*2,    //buffer length in bytes
            AudioTrack.MODE_STATIC);
    audioTrack.write(buffer, 0, buffer.length);
    audioTrack.setNotificationMarkerPosition(buffer.length);
    audioTrack.setPlaybackPositionUpdateListener(this);
    audioTrack.play();
    playing = true;
}
 
开发者ID:trc492,项目名称:Ftc2018RelicRecovery,代码行数:29,代码来源:FtcAndroidTone.java

示例2: onCreate

import android.media.AudioTrack; //导入依赖的package包/类
@Override
public void onCreate() {
    super.onCreate();
    mHandler = new Handler();
    fetchAccessToken();

    int outputBufferSize = AudioTrack.getMinBufferSize(16000,
            AudioFormat.CHANNEL_IN_STEREO,
            AudioFormat.ENCODING_PCM_16BIT);

    try {
        mAudioTrack = new AudioTrack(AudioManager.USE_DEFAULT_STREAM_TYPE, 16000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, outputBufferSize, AudioTrack.MODE_STREAM);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mAudioTrack.setVolume(DEFAULT_VOLUME);
        }
        mAudioTrack.play();
    }catch (Exception e){
        e.printStackTrace();
    }
}
 
开发者ID:hsavaliya,项目名称:GoogleAssistantSDK,代码行数:21,代码来源:SpeechService.java

示例3: stop

import android.media.AudioTrack; //导入依赖的package包/类
@Override
public void stop() {
    getAudioTrackCurrentPosition();
    mCurrentState = PlayState.STOPPED;
    if (writeWorkThread != null) {
        writeWorkThread.stopWrite();
    }
    try {
        Log.d(TAG, "stop-PlayState:" + mAudioTrack.getPlayState());
        if (mAudioTrack != null && mAudioTrack.getPlayState() != AudioTrack.STATE_UNINITIALIZED) {
            mAudioTrack.pause();
            mAudioTrack.flush();
            Log.d(TAG, "stop-ok");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "stop()", e);
    }
    fireStopped();
}
 
开发者ID:dueros,项目名称:dcs-sdk-java,代码行数:21,代码来源:AudioTrackPlayerImpl.java

示例4: AudioSink

import android.media.AudioTrack; //导入依赖的package包/类
/**
 * Constructor. Will create a new AudioSink.
 *
 * @param packetSize	size of the incoming packets
 * @param sampleRate	sample rate of the audio signal
 */
public AudioSink (int packetSize, int sampleRate) {
	this.packetSize = packetSize;
	this.sampleRate = sampleRate;

	// Create the queues and fill them with
	this.inputQueue = new ArrayBlockingQueue<SamplePacket>(QUEUE_SIZE);
	this.outputQueue = new ArrayBlockingQueue<SamplePacket>(QUEUE_SIZE);
	for (int i = 0; i < QUEUE_SIZE; i++)
		this.outputQueue.offer(new SamplePacket(packetSize));

	// Create an instance of the AudioTrack class:
	int bufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
	this.audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO,
								AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);

	// Create the audio filters:
	this.audioFilter1 = FirFilter.createLowPass(2, 1, 1, 0.1f, 0.15f, 30);
	Log.d(LOGTAG,"constructor: created audio filter 1 with " + audioFilter1.getNumberOfTaps() + " Taps.");
	this.audioFilter2 = FirFilter.createLowPass(4, 1, 1, 0.1f, 0.1f, 30);
	Log.d(LOGTAG,"constructor: created audio filter 2 with " + audioFilter2.getNumberOfTaps() + " Taps.");
	this.tmpAudioSamples = new SamplePacket(packetSize);
}
 
开发者ID:takyonxxx,项目名称:AndroidSdrRtlTuner,代码行数:29,代码来源:AudioSink.java

示例5: init_

import android.media.AudioTrack; //导入依赖的package包/类
private void init_(boolean eccEnabled) {
    mEccEncoder = EccInstanceProvider.getEncoder(eccEnabled);
    int minBufferSizeInBytes = AudioTrack.getMinBufferSize(
            RATE,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    // 44.1kHz mono 16bit
    mAudioTrack = new AudioTrack(
            AudioManager.STREAM_MUSIC,
            RATE,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            minBufferSizeInBytes,
            AudioTrack.MODE_STREAM);
    mExecutorService = Executors.newSingleThreadExecutor();
}
 
开发者ID:egglang,项目名称:sonicky,代码行数:17,代码来源:Encoder.java

示例6: getMinBufferSize

import android.media.AudioTrack; //导入依赖的package包/类
private int getMinBufferSize(int sampleRate, int channelConfig, int audioFormat) {
    minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
    // 解决异常IllegalArgumentException: Invalid audio buffer size
    int channelCount = 1;
    switch (channelConfig) {
        // AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
        case AudioFormat.CHANNEL_OUT_DEFAULT:
        case AudioFormat.CHANNEL_OUT_MONO:
        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
            channelCount = 1;
            break;
        case AudioFormat.CHANNEL_OUT_STEREO:
        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
            channelCount = 2;
            break;
        default:
            channelCount = Integer.bitCount(channelConfig);
    }
    // 判断minBufferSize是否在范围内,如果不在设定默认值为1152
    int frameSizeInBytes = channelCount * (audioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2);
    if ((minBufferSize % frameSizeInBytes != 0) || (minBufferSize < 1)) {
        minBufferSize = 1152;
    }
    return minBufferSize;
}
 
开发者ID:dueros,项目名称:dcs-sdk-java,代码行数:26,代码来源:AudioTrackPlayerImpl.java

示例7: release

import android.media.AudioTrack; //导入依赖的package包/类
@Override
public void release() {
    mCurrentState = PlayState.IDLE;
    if (writeWorkThread != null) {
        writeWorkThread.stopWrite();
    }
    try {
        Log.d(TAG, "release-PlayState:" + mAudioTrack.getPlayState());
        if (mAudioTrack != null && mAudioTrack.getPlayState() != AudioTrack.STATE_UNINITIALIZED) {
            mAudioTrack.pause();
            mAudioTrack.flush();
            mAudioTrack.stop();
            mAudioTrack.release();
            Log.d(TAG, "release-ok");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "release()", e);
    }
    fireOnRelease();
    mediaPlayerListeners.clear();
    handlerMain.removeCallbacksAndMessages(null);
}
 
开发者ID:dueros,项目名称:dcs-sdk-java,代码行数:24,代码来源:AudioTrackPlayerImpl.java

示例8: PcmPlayer

import android.media.AudioTrack; //导入依赖的package包/类
public PcmPlayer(Context context, Handler handler) {
    this.mContext = context;
    this.audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, wBufferSize, AudioTrack.MODE_STREAM);
    this.handler = handler;
    audioTrack.setPlaybackPositionUpdateListener(this, handler);
    cacheDir = context.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
}
 
开发者ID:LingjuAI,项目名称:AssistantBySDK,代码行数:8,代码来源:PcmPlayer.java

示例9: onMarkerReached

import android.media.AudioTrack; //导入依赖的package包/类
@Override
public void onMarkerReached(AudioTrack track) {
    Log.i(TAG, "onMarkerReached>>>" + track.getNotificationMarkerPosition());
    if (playLock.tryLock()) {
        try {
            playCondition.signalAll();
        } finally {
            playLock.unlock();
        }
    }
    Log.i(TAG, "PCM SIZE=" + pcms.size());
    if (!pending.get() && pcms.size() == 0) {
        play.set(false);
        playListener.onCompleted();
    }
}
 
开发者ID:LingjuAI,项目名称:AssistantBySDK,代码行数:17,代码来源:PcmPlayer.java

示例10: createAudioTrack

import android.media.AudioTrack; //导入依赖的package包/类
public AudioTrack createAudioTrack(int frameRate) {
    int minBufferSizeBytes = AudioTrack.getMinBufferSize(frameRate,
            AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_FLOAT);
    Log.i(TAG, "AudioTrack.minBufferSize = " + minBufferSizeBytes
            + " bytes = " + (minBufferSizeBytes / BYTES_PER_FRAME)
            + " frames");
    int bufferSize = 8 * minBufferSizeBytes / 8;
    int outputBufferSizeFrames = bufferSize / BYTES_PER_FRAME;
    Log.i(TAG, "actual bufferSize = " + bufferSize + " bytes = "
            + outputBufferSizeFrames + " frames");

    AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC,
            mFrameRate, AudioFormat.CHANNEL_OUT_STEREO,
            AudioFormat.ENCODING_PCM_FLOAT, bufferSize,
            AudioTrack.MODE_STREAM);
    Log.i(TAG, "created AudioTrack");
    return player;
}
 
开发者ID:sdrausty,项目名称:buildAPKsSamples,代码行数:19,代码来源:SimpleAudioOutput.java

示例11: run

import android.media.AudioTrack; //导入依赖的package包/类
@Override
public void run() {
    super.run();
    isRunning = true;
    int buffsize = AudioTrack.getMinBufferSize(sr,
            AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
    // create an audiotrack object
    AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sr, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, buffsize,
            AudioTrack.MODE_STREAM);

    short samples[] = new short[buffsize];
    int amp = 10000;
    double twopi = 8.*Math.atan(1.);
    double ph = 0.0;

    // start audio
    audioTrack.play();

    // synthesis loop
    while(isRunning){
        double fr = tuneFreq;
        for(int i=0; i < buffsize; i++){
            samples[i] = (short) (amp*Math.sin(ph));
            ph += twopi*fr/sr;
        }
        audioTrack.write(samples, 0, buffsize);
    }
    audioTrack.stop();
    audioTrack.release();
}
 
开发者ID:karlotoy,项目名称:perfectTune,代码行数:33,代码来源:TuneThread.java

示例12: PWave

import android.media.AudioTrack; //导入依赖的package包/类
public PWave(AppRunner appRunner) {
    super(appRunner);
    appRunner.whatIsRunning.add(this);

    // set the buffer size
    buffsize = AudioTrack.getMinBufferSize(mSampleRate,
            AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

    samples = new short[buffsize];

    // create an audiotrack object
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            mSampleRate, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, buffsize,
            AudioTrack.MODE_STREAM);

    // start audio
    audioTrack.play();
}
 
开发者ID:victordiaz,项目名称:phonk,代码行数:20,代码来源:PWave.java

示例13: startPlayer

import android.media.AudioTrack; //导入依赖的package包/类
public boolean startPlayer(int streamType, int sampleRateInHz, int channelConfig, int audioFormat) {
    
    if (mIsPlayStarted) {
        Log.e(TAG, "Player already started !");
        return false;
    }
    
    mMinBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);
    if (mMinBufferSize == AudioTrack.ERROR_BAD_VALUE) {
        Log.e(TAG, "Invalid parameter !");
        return false;
    }
    Log.d(TAG , "getMinBufferSize = "+mMinBufferSize+" bytes !");
    
    mAudioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,mMinBufferSize,DEFAULT_PLAY_MODE);
    if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
        Log.e(TAG, "AudioTrack initialize fail !");
        return false;
    }            
    
    mIsPlayStarted = true;
    
    Log.d(TAG, "Start audio player success !");
    
    return true;
}
 
开发者ID:ThinkKeep,项目名称:EvilsLive,代码行数:27,代码来源:AudioPlayer.java

示例14: handlePrepare

import android.media.AudioTrack; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected int handlePrepare(MediaExtractor media_extractor) {
	int track_index = selectTrack(media_extractor, "audio/");
	if (track_index >= 0) {
		final MediaFormat format = media_extractor.getTrackFormat(track_index);
		mAudioChannels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
		mAudioSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
		final int min_buf_size = AudioTrack.getMinBufferSize(mAudioSampleRate,
				(mAudioChannels == 1 ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO),
				AudioFormat.ENCODING_PCM_16BIT);
		final int max_input_size = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
		mAudioInputBufSize =  min_buf_size > 0 ? min_buf_size * mAudioChannels * 2 : max_input_size;
		if (mAudioInputBufSize > max_input_size) mAudioInputBufSize = max_input_size;
		final int frameSizeInBytes = mAudioChannels * 2;
		mAudioInputBufSize = (mAudioInputBufSize / frameSizeInBytes) * frameSizeInBytes;
		if (DEBUG) Log.v(TAG, String.format("getMinBufferSize=%d,max_input_size=%d,mAudioInputBufSize=%d",min_buf_size, max_input_size, mAudioInputBufSize));
	}
	return track_index;
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:21,代码来源:MediaAudioDecoder.java

示例15: generateTrack

import android.media.AudioTrack; //导入依赖的package包/类
public AudioTrack generateTrack(int sampleRate, short[] buf, int len) {
    int end = len;

    int c = 0;

    if (RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO)
        c = AudioFormat.CHANNEL_OUT_MONO;

    if (RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_STEREO)
        c = AudioFormat.CHANNEL_OUT_STEREO;

    // old phones bug.
    // http://stackoverflow.com/questions/27602492
    //
    // with MODE_STATIC setNotificationMarkerPosition not called
    AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
            c, RawSamples.AUDIO_FORMAT,
            len * (Short.SIZE / 8), AudioTrack.MODE_STREAM);
    track.write(buf, 0, len);
    if (track.setNotificationMarkerPosition(end) != AudioTrack.SUCCESS)
        throw new RuntimeException("unable to set marker");
    return track;
}
 
开发者ID:NandagopalR,项目名称:Android-Audio-Recorder,代码行数:24,代码来源:Sound.java


注:本文中的android.media.AudioTrack类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。