简单的录音功能,测试一下代码,直接贴::::总结
package com.example.testbuttomtoolbar.testbuttomtoolbar;
import androID.Manifest;
import androID.app.Activity;
import androID.content.Intent;
import androID.content.pm.PackageManager;
import androID.media.MediaRecorder;
import androID.os.Build;
import androID.os.Environment;
import androID.support.v7.app.AppCompatActivity;
import androID.os.Bundle;
import androID.vIEw.SurfaceHolder;
import androID.vIEw.SurfaceVIEw;
import androID.vIEw.VIEw;
import androID.Widget.button;
import androID.Widget.Toast;
import java.io.file;
import java.io.IOException;
public class MainActivity extends Activity {
private button btnRecord=null;
private button btnStop=null;
private SurfaceVIEw mSurfaceVIEw=null;
private SurfaceHolder mSurfaceHolder=null;
private MediaRecorder recorder=null;
private file myRecAudiofile=null;
private file dir=null;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.activity_main);
mSurfaceVIEw = (SurfaceVIEw) findVIEwByID(R.ID.surfacevIEw);
mSurfaceHolder = mSurfaceVIEw.getHolder();
mSurfaceHolder.setKeepScreenOn(true);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btnRecord=findVIEwByID(R.ID.record);
btnStop=findVIEwByID(R.ID.stop);
file defaultDir = Environment.getExternalStorageDirectory();
String path = defaultDir.getabsolutePath()+file.separator+"V"+file.separator;
//创建文件夹存放视频
dir = new file(path);
if(!dir.exists()){
dir.mkdir();
}
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.RECORD_AUdio,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
return;
}
}
}
recorder = new MediaRecorder();
//开始录制视频
btnRecord.setonClickListener(new VIEw.OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
recorder();
}
});
//停止录制视频
btnStop.setonClickListener(new VIEw.OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
recorder.stop();
recorder.reset();
recorder.release();
recorder=null;
}
});
}
public voID recorder() {
try {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setoutputfile("/mnt/sdcard/luyin2.3gp");
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printstacktrace();
}
}
}
以上是内存溢出为你收集整理的Andriod- 设置输出音频MediaRecorder全部内容,希望文章能够帮你解决Andriod- 设置输出音频MediaRecorder所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)