當前位置: 首頁>>代碼示例>>Java>>正文


Java AudioRecord.startRecording方法代碼示例

本文整理匯總了Java中android.media.AudioRecord.startRecording方法的典型用法代碼示例。如果您正苦於以下問題:Java AudioRecord.startRecording方法的具體用法?Java AudioRecord.startRecording怎麽用?Java AudioRecord.startRecording使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.media.AudioRecord的用法示例。


在下文中一共展示了AudioRecord.startRecording方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: startRecording

import android.media.AudioRecord; //導入方法依賴的package包/類
private void startRecording(final String filename) {
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE,
            RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING,
            bufferSize);
    int i = recorder.getState();
    if (i==1)
        recorder.startRecording();

    isRecording = true;

    recordingThread = new Thread(new Runnable() {
        @Override
        public void run() {
            writeAudioDataToFile(filename);
        }
    }, "AudioRecorder Thread");

    recordingThread.start();
}
 
開發者ID:interritus1996,項目名稱:memento-app,代碼行數:22,代碼來源:Recorder.java

示例2: startRecording

import android.media.AudioRecord; //導入方法依賴的package包/類
private void startRecording(){
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

    int i = recorder.getState();
    if(i==1)
        recorder.startRecording();

    isRecording = true;

    recordingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            writeAudioDataToArrayList();
            reorganiseDataArray(flattenArray());
        }
    },"AudioRecorder Thread");

    recordingThread.start();
}
 
開發者ID:ranatrk,項目名稱:AudioGenderIdentifier,代碼行數:22,代碼來源:MainActivity.java

示例3: startListening

import android.media.AudioRecord; //導入方法依賴的package包/類
public void startListening() {
    synchronized (API_LOCK) {
        if (this.disabled) {
            return;
        }
        if (!isListening) {
            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                    RECORDER_AUDIO_ENCODING, ELEMENTS_TO_RECORD * BYTES_PER_ELEMENT);
            recorder.startRecording();
            isListening = true;
            if (!isRecording) {
                isRecording = true;
                recordingThread = new Thread(new Runnable() {
                    public void run() {
                        detectHotword();
                    }
                }, "AudioRecorder Thread");
                recordingThread.start();
            }
        }
    }
}
 
開發者ID:timstableford,項目名稱:P-BrainAndroid,代碼行數:24,代碼來源:HotwordDetector.java

示例4: fromDefaultMicrophone

import android.media.AudioRecord; //導入方法依賴的package包/類
/**
 * Create a new AudioDispatcher connected to the default microphone.
 * 
 * @param sampleRate
 *            The requested sample rate.
 * @param audioBufferSize
 *            The size of the audio buffer (in samples).
 * 
 * @param bufferOverlap
 *            The size of the overlap (in samples).
 * @return A new AudioDispatcher
 */
public static AudioDispatcher fromDefaultMicrophone(final int sampleRate,
		final int audioBufferSize, final int bufferOverlap) {
	int minAudioBufferSize = AudioRecord.getMinBufferSize(sampleRate,
			android.media.AudioFormat.CHANNEL_IN_MONO,
			android.media.AudioFormat.ENCODING_PCM_16BIT);
	int minAudioBufferSizeInSamples =  minAudioBufferSize/2;
	if(minAudioBufferSizeInSamples <= audioBufferSize ){
	AudioRecord audioInputStream = new AudioRecord(
			MediaRecorder.AudioSource.MIC, sampleRate,
			android.media.AudioFormat.CHANNEL_IN_MONO,
			android.media.AudioFormat.ENCODING_PCM_16BIT,
			audioBufferSize * 2);

	TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(sampleRate, 16,1, true, false);
	
	TarsosDSPAudioInputStream audioStream = new AndroidAudioInputStream(audioInputStream, format);
	//start recording ! Opens the stream.
	audioInputStream.startRecording();
	return new AudioDispatcher(audioStream,audioBufferSize,bufferOverlap);
	}else{
		throw new IllegalArgumentException("Buffer size too small should be at least " + (minAudioBufferSize *2));
	}
}
 
開發者ID:gstraube,項目名稱:cythara,代碼行數:36,代碼來源:AudioDispatcherFactory.java

示例5: run

import android.media.AudioRecord; //導入方法依賴的package包/類
@Override
public void run() {
    byte[] buffer = new byte[audioBufferSize];
    recorder = new AudioRecord(audioSource, sampleRate, channel, audioFormat, audioBufferSize);
    try {
        while (recorder.getState() != AudioRecord.STATE_INITIALIZED)
            Thread.sleep(100, 0);
    } catch (InterruptedException e) {
        recorder.release();
        return;
    }

    recorder.startRecording();
    for (; ; ) {
        int length = recorder.read(buffer, 0, buffer.length);
        if (length < 0)
            Log.e("Record", "error: " + Integer.toString(length));
        else {
            for (WebSocket ws : wss)
                ws.sendBinary(buffer);
        }
        if (Thread.interrupted()) {
            recorder.stop();
            return;
        }
    }
}
 
開發者ID:zackszhu,項目名稱:hack_sjtu_2017,代碼行數:28,代碼來源:LiuLiShuo.java

示例6: StartRecord

import android.media.AudioRecord; //導入方法依賴的package包/類
public void StartRecord() {
        Log.i(TAG,"開始錄音");
        isRecording=true;

//生成PCM文件
        File file = audioFile;//new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
        Log.i(TAG,"生成文件");
//如果存在,就先刪除再創建
        if (file.exists())
            file.delete();
        Log.i(TAG,"刪除文件");
        try {
            file.createNewFile();
            Log.i(TAG,"創建文件");
        } catch (IOException e) {
            Log.i(TAG,"未能創建");
            throw new IllegalStateException("未能創建" + file.toString());
        }
        try {
//輸出流
            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream dos = new DataOutputStream(bos);
            int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
            AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);
            byte[] buffer = new byte[bufferSize];
            audioRecord.startRecording();
            Log.i(TAG, "開始錄音");
            isRecording = true;
            while (isRecording) {
                int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                dos.write(buffer,0,bufferReadResult);
            }
            audioRecord.stop();
            dos.close();
        } catch (Throwable t) {
            Log.e(TAG, "錄音失敗");
        }
    }
 
開發者ID:yippeesoft,項目名稱:NotifyTools,代碼行數:40,代碼來源:VoicePcm.java

示例7: startRecord

import android.media.AudioRecord; //導入方法依賴的package包/類
public void startRecord() throws IOException {
        synchronized (REC_LOCK){
            isRecordStarted=true;
            MediaFormat audioFormat=mConfig.getAudioFormat();
            mAudioEncoder=MediaCodec.createEncoderByType(audioFormat.getString(MediaFormat.KEY_MIME));
            mAudioEncoder.configure(audioFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            MediaFormat videoFormat=mConfig.getVideoFormat();
            mVideoEncoder=MediaCodec.createEncoderByType(videoFormat.getString(MediaFormat.KEY_MIME));
            //此處不能用mOutputSurface,會configure失敗
            mVideoEncoder.configure(videoFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            mEncodeSurface=mVideoEncoder.createInputSurface();

            mAudioEncoder.start();
            mVideoEncoder.start();
            mMuxer=new MediaMuxer(mOutputPath,MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
            mRecordBufferSize = AudioRecord.getMinBufferSize(mRecordSampleRate,
                    mRecordChannelConfig, mRecordAudioFormat)*2;
//        buffer=new byte[bufferSize];
            mAudioRecord=new AudioRecord(MediaRecorder.AudioSource.MIC,mRecordSampleRate,mRecordChannelConfig,
                    mRecordAudioFormat,mRecordBufferSize);

            mAudioThread=new Thread(new Runnable() {
                @Override
                public void run() {
                    mAudioRecord.startRecording();
                    while (!audioEncodeStep(isTryStopAudio)){};
                    mAudioRecord.stop();
                }
            });
            mAudioThread.start();
            isRecordAudioStarted=true;
        }
    }
 
開發者ID:aiyaapp,項目名稱:AAVT,代碼行數:34,代碼來源:CameraRecorder.java

示例8: startCapture

import android.media.AudioRecord; //導入方法依賴的package包/類
public boolean startCapture(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat) {
    if (mIsCaptureStarted) {
        Log.e(TAG, "Capture already started !");
        return false;
    }

    int minBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
    if (minBufferSize == AudioRecord.ERROR_BAD_VALUE) {
        Log.e(TAG, "Invalid parameter !");
        return false;
    }

    mAudioRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, minBufferSize * 4);
    if (mAudioRecord.getState() == AudioRecord.STATE_UNINITIALIZED) {
        Log.e(TAG, "AudioRecord initialize fail !");
        return false;
    }

    mAudioRecord.startRecording();

    mIsLoopExit = false;
    mCaptureThread = new Thread(new AudioCaptureRunnable());
    mCaptureThread.start();

    mIsCaptureStarted = true;

    Log.d(TAG, "Start audio capture success !");

    return true;
}
 
開發者ID:pili-engineering,項目名稱:PLDroidRTCStreaming,代碼行數:31,代碼來源:ExtAudioCapture.java

示例9: start

import android.media.AudioRecord; //導入方法依賴的package包/類
public void start(){
    if(!isStarted){
        stopFlag=false;

        mRecordBufferSize = AudioRecord.getMinBufferSize(mRecordSampleRate,
                mRecordChannelConfig, mRecordAudioFormat)*2;
        mRecord=new AudioRecord(MediaRecorder.AudioSource.MIC,mRecordSampleRate,mRecordChannelConfig,
                mRecordAudioFormat,mRecordBufferSize);
        mRecord.startRecording();
        try {
            MediaFormat format=convertAudioConfigToFormat(mConfig.mAudio);
            mAudioEncoder=MediaCodec.createEncoderByType(format.getString(MediaFormat.KEY_MIME));
            mAudioEncoder.configure(format,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            mAudioEncoder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while (!stopFlag&&!audioEncodeStep(false)){};
                audioEncodeStep(true);
                Log.e("wuwang","audio stop");
                if(isStarted){
                    mRecord.stop();
                    mRecord.release();
                    mRecord=null;
                }
                if(mAudioEncoder!=null){
                    mAudioEncoder.stop();
                    mAudioEncoder.release();
                    mAudioEncoder=null;
                }
                isStarted=false;
            }
        });
        thread.start();
        startTime=SystemClock.elapsedRealtimeNanos();
        isStarted=true;
    }
}
 
開發者ID:aiyaapp,項目名稱:AAVT,代碼行數:42,代碼來源:SoundRecorder.java

示例10: isCanUseAudio

import android.media.AudioRecord; //導入方法依賴的package包/類
/**
   * 判斷是是否有錄音權限
   */
  public static boolean isCanUseAudio(Activity activity) {
/*      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){//6.0以上係統
          if (ContextCompat.checkSelfPermission(activity,Manifest.permission.RECORD_AUDIO)!= PackageManager.PERMISSION_GRANTED) {
              return false;
          }else{
              return true;
          }

      }else {//6.0以下係統*/
      // 音頻獲取源
      int audioSource = MediaRecorder.AudioSource.MIC;
      // 設置音頻采樣率,44100是目前的標準,但是某些設備仍然支持22050,16000,11025
      int sampleRateInHz = 44100;
      // 設置音頻的錄製的聲道CHANNEL_IN_STEREO為雙聲道,CHANNEL_CONFIGURATION_MONO為單聲道
      int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
      // 音頻數據格式:PCM 16位每個樣本。保證設備支持。PCM 8位每個樣本。不一定能得到設備支持。
      int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
      // 緩衝區字節大小
      int bufferSizeInBytes;
      bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
              channelConfig, audioFormat);
      AudioRecord audioRecord = new AudioRecord(audioSource, sampleRateInHz,
              channelConfig, audioFormat, bufferSizeInBytes);
      //開始錄製音頻
      try {
          // 防止某些手機崩潰,例如聯想
          audioRecord.startRecording();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      }

      //根據開始錄音判斷是否有錄音權限
      if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
          // context.startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
          return false;
      } else {
          audioRecord.stop();
          audioRecord.release();
          return true;
      }

      // }


  }
 
開發者ID:zhonglikui,項目名稱:cardinalsSample,代碼行數:49,代碼來源:DevicesUtil.java

示例11: startCapture

import android.media.AudioRecord; //導入方法依賴的package包/類
public boolean startCapture(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat) {

        if (mIsCaptureStarted) {
            Log.e(TAG, "hujd Capture already started !");
            return false;
        }

        mMinBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);

        if (mMinBufferSize == AudioRecord.ERROR_BAD_VALUE) {
            Log.e(TAG, "hujd Invalid parameter !");
            return false;
        }

        Log.e(TAG, "hujd getMinBufferSize = " + mMinBufferSize + " bytes !");

        mAudioRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, mMinBufferSize);

        if (mAudioRecord.getState() == AudioRecord.STATE_UNINITIALIZED) {
            Log.e(TAG, "hujd AudioRecord initialize fail !");
            return false;
        }

        mAudioRecord.startRecording();


        mIsLoopExit = false;
        mCaptureThread = new Thread(new AudioCaptureRunnable());

        mCaptureThread.start();

        mIsCaptureStarted = true;

        Log.e(TAG, "hujd Start audio capture success !");
        return true;

    }
 
開發者ID:ThinkKeep,項目名稱:EvilsLive,代碼行數:38,代碼來源:AudioCapture.java


注:本文中的android.media.AudioRecord.startRecording方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。