Java Code Examples for android.media.AudioFormat#CHANNEL_CONFIGURATION_MONO

The following examples show how to use android.media.AudioFormat#CHANNEL_CONFIGURATION_MONO . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AudioTrackPlayerImpl.java    From dcs-sdk-java with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: AudioThread.java    From Viewer with Apache License 2.0 6 votes vote down vote up
public AudioThread(int sampleRateInHz, int channel, long streamId, long decoderId, Media media)
{
	if (channel == 1)
	{
		channel_configuration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
	} else
	{
		channel_configuration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
	}
	this.mediaStreamId = streamId;
	this.decoderId = decoderId;
	this.media = media;
	int minBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz, channel_configuration, AudioFormat.ENCODING_PCM_16BIT);
	if (minBufferSize > audioLength)
	{
		audioLength = minBufferSize;
	}
	mAudioBuffer = new byte[audioLength];
	mAudio = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz, channel_configuration, AudioFormat.ENCODING_PCM_16BIT, audioLength, AudioTrack.MODE_STREAM);
}
 
Example 3
Source File: MainActivity.java    From android-apps with MIT License 5 votes vote down vote up
private void startRecord(){
	File file = new File(Environment.getExternalStorageDirectory(), "record.pcm");
	int sampleFreq = (Integer)spFrequency.getSelectedItem();
	
	try{
		file.createNewFile();
		
		OutputStream outputStream = new FileOutputStream(file);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
		DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
		
		int minBufferedSize = AudioRecord.getMinBufferSize(sampleFreq, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
		
		short[] audioData = new short[minBufferedSize];
		
		AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleFreq, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferedSize);
		audioRecord.startRecording();
		
		while (recording) {
			int numberOfShort = audioRecord.read(audioData, 0, minBufferedSize);
			for(int i = 0; i < numberOfShort; i++){
				dataOutputStream.writeShort(audioData[i]);
			}
		}
		
		audioRecord.stop();
		dataOutputStream.close();
		
	} catch(Exception ex){
		ex.printStackTrace();
	}
}
 
Example 4
Source File: AudioRecorder.java    From euphony with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param sampleRate
 *            The sample rate (e.g. 44100 Hz).
 */
public AudioRecorder(int sampleRate) {
	int bufferSize = AudioRecord.getMinBufferSize(sampleRate,
			AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
	
	audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
			sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
			AudioFormat.ENCODING_PCM_16BIT, bufferSize);
	swtWindowing = false;
}