編輯:關於Android編程
MediaRecorder即錄音
要想使用首先查看API,可以看到MediaRecorder的生命周期
API不僅給了生命周期圖,還有例子如下<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
查看完API我們可以試著寫一寫了;
【1】搭界面:兩個按鈕,一個播放一個停止
【2】加點擊事件
【3】別忘加權限
【4】得到MediaRecorder並設置參數
recorder=new MediaRecorder();
//音頻來源 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//設置輸出格式 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//音頻編碼recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//設置保存在sdcard下面名為 myrecord.3gp recorder.setOutputFile(Environment.getExternalStorageDirectory() + /myrecord.3gp);
【5】開始錄制
recorder.prepare();
recorder.start();
【6】停止錄制
recorder.stop();
recorder.reset();
recorder.release();
這樣子就已經完全實現
下面是完整源碼
public class MainActivity extends Activity implements View.OnClickListener{
private Button mButtonStart;
private Button mButtonStop;
private MediaRecorder recorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonStart= (Button) findViewById(R.id.button_start);
mButtonStop= (Button) findViewById(R.id.button_stop);
mButtonStop.setOnClickListener(this);
mButtonStart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_start:
recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory() + /myrecord.3gp);
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.button_stop:
recorder.stop();
recorder.reset();
recorder.release();
break;
default:break;
}
}
}
Kotlin在Android工程中的應用 @author ASCE1885的 Github 簡書 微博 CSDN 原文鏈接簡介Kotlin是由JetBrains設計的開放
首先肯定需要引用select2.js:點擊打開鏈接,點擊鏈接,自己新建一個select2.js把鏈接中的源碼復制進去;然後這裡面需要修改的是這個方法:function m
簡單拓展RadioButton控件使用如果不看這些,就直接下托RadioButton和CheckBox的區別:1、單個RadioButton在選中後,通過點擊無法變為未選
前言:前面幾篇總結一些TV上的小Sample,開源到GitHub:https://github.com/hejunlin2013/TVSample, 點擊鏈接,可以持續關