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


Java MediaRecorder类代码示例

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


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

示例1: startRecording

import android.media.MediaRecorder; //导入依赖的package包/类
public void startRecording() {
    setFileNameAndPath();

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setOutputFile(mFilePath);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mRecorder.setAudioChannels(1);
    mRecorder.setAudioSamplingRate(44100);
    mRecorder.setAudioEncodingBitRate(192000);

    try {
        mRecorder.prepare();
        mRecorder.start();
        mStartingTimeMillis = System.currentTimeMillis();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }
}
 
开发者ID:AndroidBoySC,项目名称:Mybilibili,代码行数:21,代码来源:RecordingService.java

示例2: createAudioRecord

import android.media.MediaRecorder; //导入依赖的package包/类
/**
 * Creates a new {@link AudioRecord}.
 *
 * @return A newly created {@link AudioRecord}, or null if it cannot be created (missing
 * permissions?).
 */
private AudioRecord createAudioRecord() {
    for (int sampleRate : SAMPLE_RATE_CANDIDATES) {
        final int sizeInBytes = AudioRecord.getMinBufferSize(sampleRate, CHANNEL, ENCODING);
        if (sizeInBytes == AudioRecord.ERROR_BAD_VALUE) {
            continue;
        }
        final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                sampleRate, CHANNEL, ENCODING, sizeInBytes);
        if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
            mBuffer = new byte[sizeInBytes];
            return audioRecord;
        } else {
            audioRecord.release();
        }
    }
    return null;
}
 
开发者ID:hsavaliya,项目名称:GoogleAssistantSDK,代码行数:24,代码来源:VoiceRecorder_.java

示例3: recordVideo

import android.media.MediaRecorder; //导入依赖的package包/类
public void recordVideo(VideoStartCallback videoStartCallback, VideoStopCallback videoStopCallback, VideoErrorCallback videoErrorCallback) {
    try {
        this.videoStartCallback = videoStartCallback;
        this.videoStopCallback = videoStopCallback;
        this.videoErrorCallback = videoErrorCallback;
        if(mCameraDevice == null || !mTextureView.isAvailable() || mPreviewSize == null){
            this.videoErrorCallback.onVideoError("Camera not ready.");
            return;
        }
        videoFile = Environment.getExternalStorageDirectory() + "/" + formatter.format(new Date()) + ".mp4";
        mMediaRecorder = new MediaRecorder();
        //mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setOutputFile(videoFile);
        mMediaRecorder.setVideoEncodingBitRate(10000000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        //mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        if(swappedDimensions) {
            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(mDisplayOrientation));
        } else {
            mMediaRecorder.setOrientationHint(ORIENTATIONS.get(mDisplayOrientation));
        }
        mMediaRecorder.prepare();
        closePreviewSession();
        createCameraRecordSession();
    } catch(IOException ex) {
        Log.d(TAG, ex.getMessage());
    }
}
 
开发者ID:EzequielAdrianM,项目名称:Camera2Vision,代码行数:33,代码来源:Camera2Source.java

示例4: startRecording

import android.media.MediaRecorder; //导入依赖的package包/类
public static void startRecording(Context context, MediaRecorder recorder, String word) {
    recorder.reset();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    recorder.setMaxDuration(10000);
    recorder.setAudioSamplingRate(16000);
    recorder.setAudioEncodingBitRate(16000*4*1);
    recorder.setAudioSamplingRate(44100);
    recorder.setAudioChannels(1);
    recorder.setOutputFile(context.getFilesDir().getAbsolutePath() + "/" + word + ".aac");
    try {
        recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    recorder.start();
}
 
开发者ID:Cesarsk,项目名称:Say_it,代码行数:19,代码来源:UtilityRecordings.java

示例5: prepareAudio

import android.media.MediaRecorder; //导入依赖的package包/类
public void prepareAudio() {
    try {
        this.mCurrentFile = new File(getVoiceCacheDir(this.mContext), UUID.randomUUID()
                .toString());
        this.mMediaRecorder = new MediaRecorder();
        this.mMediaRecorder.setOutputFile(this.mCurrentFile.getAbsolutePath());
        this.mMediaRecorder.setAudioSource(1);
        this.mMediaRecorder.setOutputFormat(3);
        this.mMediaRecorder.setAudioEncoder(1);
        this.mMediaRecorder.prepare();
        this.mMediaRecorder.start();
        this.mIsPrepared = true;
        if (this.mCallback != null) {
            this.mCallback.wellPrepared();
        }
    } catch (Exception e) {
        if (this.mCallback != null) {
            this.mCallback.onAudioRecorderNoPermission();
        }
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:22,代码来源:MQAudioRecorderManager.java

示例6: startRecording

import android.media.MediaRecorder; //导入依赖的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

示例7: recordAudio

import android.media.MediaRecorder; //导入依赖的package包/类
static Audio recordAudio(UQI uqi, long duration) throws IOException {
    List<Integer> amplitudes = new ArrayList<>();

    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(Globals.AudioConfig.audioSource);
    recorder.setOutputFormat(Globals.AudioConfig.outputFormat);
    recorder.setAudioEncoder(Globals.AudioConfig.audioEncoder);

    String audioPath = "temp/audio_" + TimeUtils.getTimeTag() + ".amr";
    File tempAudioFile = StorageUtils.getValidFile(uqi.getContext(), audioPath, false);
    recorder.setOutputFile(tempAudioFile.getAbsolutePath());

    recorder.prepare();
    recorder.start();   // Recording is now started

    long startTime = System.currentTimeMillis();
    while (true) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime > duration) {
            break;
        }
        amplitudes.add(recorder.getMaxAmplitude());
    }

    recorder.stop();
    recorder.reset();   // You can reuse the object by going back to setAudioSource() step
    recorder.release(); // Now the object cannot be reused

    AudioData audioData = AudioData.newTempRecord(tempAudioFile, amplitudes);

    return new Audio(startTime, audioData);
}
 
开发者ID:PrivacyStreams,项目名称:PrivacyStreams,代码行数:33,代码来源:AudioRecorder.java

示例8: startRecording

import android.media.MediaRecorder; //导入依赖的package包/类
private void startRecording() {
    audioRec = new MediaRecorder();
    audioRec.setAudioSource(MediaRecorder.AudioSource.MIC);
    audioRec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    audioRec.setOutputFile(audioFilePath);
    audioRec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    audioRec.setAudioSamplingRate(256);
    try {
        audioRec.prepare();
        audioRec.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
开发者ID:gvsucis,项目名称:mobile-app-dev-book,代码行数:15,代码来源:AudioActivity.java

示例9: configureMediaRecorderDataSource

import android.media.MediaRecorder; //导入依赖的package包/类
private boolean configureMediaRecorderDataSource(Context context, File recordFile, int audioFormat)
{
	try
	{
		mediaRecorder.setOutputFormat(audioFormat);
		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		mediaRecorder.setOutputFile(recordFile.getAbsolutePath());
	}
	catch (Exception e)
	{
		Log.w("RecordFileWriter", "configureMediaRecorderDataSource : " + context.getString(R.string.log_record_file_writer_error_data_source) + " : " + e);
		databaseManager.insertLog(context, "" + context.getString(R.string.log_record_file_writer_error_data_source), new Date().getTime(), 2, false);
		return false;
	}

	return true;
}
 
开发者ID:vassela,项目名称:AC2RD,代码行数:18,代码来源:RecordFileWriter.java

示例10: startListening

import android.media.MediaRecorder; //导入依赖的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

示例11: fromDefaultMicrophone

import android.media.MediaRecorder; //导入依赖的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

示例12: setUpMediaRecorder

import android.media.MediaRecorder; //导入依赖的package包/类
private void setUpMediaRecorder() throws IOException {
    final Activity activity = (Activity) getContext();
    if (null == activity) {
        return;
    }
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    if (sensorOrientation == SENSOR_ORIENTATION_DEFAULT_DEGREES) {
        mediaRecorder.setOrientationHint(ORIENTATION_90);
    } else {
        mediaRecorder.setOrientationHint(ORIENTATION_270);
    }

    CamcorderProfile profile = getCamcorderProfile();
    profile.videoFrameWidth = videoSize.getWidth();
    profile.videoFrameHeight = videoSize.getHeight();

    mediaRecorder.setProfile(profile);
    mediaRecorder.setVideoEncodingBitRate(BITRATE);
    mediaRecorder.prepare();
}
 
开发者ID:team-supercharge,项目名称:SCCameraView,代码行数:24,代码来源:Camera2View.java

示例13: configureRecorder

import android.media.MediaRecorder; //导入依赖的package包/类
protected void configureRecorder(int quality, MediaRecorder recorder) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraInfo.cameraId, info);
    int displayOrientation = getDisplayOrientation(info, false);
    recorder.setOrientationHint(displayOrientation);

    int highProfile = getHigh();
    boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile);
    boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW);
    if (canGoHigh && (quality == 1 || !canGoLow)) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile));
    } else if (canGoLow) {
        recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW));
    } else {
        throw new IllegalStateException("cannot find valid CamcorderProfile");
    }
    isVideo = true;
}
 
开发者ID:chengzichen,项目名称:KrGallery,代码行数:19,代码来源:CameraSession.java

示例14: onInfo

import android.media.MediaRecorder; //导入依赖的package包/类
@Override
public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
    if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED || what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED || what == MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN) {
        MediaRecorder tempRecorder = recorder;
        recorder = null;
        if (tempRecorder != null) {
            tempRecorder.stop();
            tempRecorder.release();
        }
        if (onVideoTakeCallback != null) {
            final Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(recordedFile, MediaStore.Video.Thumbnails.MINI_KIND);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    if (onVideoTakeCallback != null) {
                        onVideoTakeCallback.onFinishVideoRecording(bitmap);
                        onVideoTakeCallback = null;
                    }
                }
            });
        }
    }
}
 
开发者ID:chengzichen,项目名称:KrGallery,代码行数:24,代码来源:CameraController.java

示例15: clickRec

import android.media.MediaRecorder; //导入依赖的package包/类
@Click(R.id.fabRec)
void clickRec() {
    if(!isInitRecorder) {
        isInitRecorder = true;
        mAudioFile = new File(
                Environment.getExternalStorageDirectory().getAbsolutePath() +
                        File.separator + "lecrec" + File.separator + System.nanoTime() + ".file.m4a");
        mAudioRecorder.prepareRecord(MediaRecorder.AudioSource.MIC,
                MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.AudioEncoder.AAC,
                mAudioFile);
    }

    if(!isRecording) {
        isRecording = true;
        startTimer();
        mAudioRecorder.startRecord();
        fabRec.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorPrimary)));
        fabRec.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_stop_white_24dp));
    } else {
        isRecording = false;
        stopTimer();
        fabRec.setVisibility(View.INVISIBLE);
        mAudioRecorder.stopRecord();
        convertToWav();
    }
}
 
开发者ID:lecrec,项目名称:lecrec-android,代码行数:27,代码来源:ActivityRecordVoice.java


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