中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android語音錄制的代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
使用MediaRecorder的步驟:  
1、創(chuàng)建MediaRecorder對象  
2、調(diào)用MediRecorder對象的setAudioSource()方法設(shè)置聲音的來源,一般傳入MediaRecorder.MIC  
3、調(diào)用MediaRecorder對象的setOutputFormat()設(shè)置所錄制的音頻文件的格式  
4、調(diào)用MediaRecorder對象的setAudioRncoder()、setAudioEncodingBitRate(int bitRate)、setAudioSamlingRate(int SamplingRate)設(shè)置所錄音的編碼格式、編碼位率、采樣率等,  
5、調(diào)用MediaRecorder對象的setOutputFile(String path)方法設(shè)置錄制的音頻文件的保存位置  
6、調(diào)用MediaRecoder對象的Prepare()方法準(zhǔn)備錄制  
7、調(diào)用MediaRecoder對象的start()方法開始錄制  
8、調(diào)用MediaRecoder對象的stop()方法停止錄制,并調(diào)用release()方法釋放資源  
  
-。啟用權(quán)限  
<uses-permission  android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/>  
    <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
    <uses-permission  android:name="android.permission.RECORD_AUDIO"/>  
  
布局就不用說了 ,附上代碼吧  
主要代碼:  
  
 public class MainActivity extends Activity implements OnClickListener {   
  private Button start;  
 private Button stop;  
 private ListView listView;  
 // 錄音文件播放  
 private MediaPlayer myPlayer;  
 // 錄音  
 private MediaRecorder myRecorder;  
 // 音頻文件保存地址  
 private String path;  
 private String paths = path;  
 private File saveFilePath;  
 // 所錄音的文件  
 String[] listFile = null;   
  ShowRecorderAdpter showRecord;  
 AlertDialog aler = null;   
  @Override  
 protected void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_main);  
  start = (Button) findViewById(R.id.start);  
  stop = (Button) findViewById(R.id.stop);  
  listView = (ListView) findViewById(R.id.list);  
  myPlayer = new MediaPlayer();  
  myRecorder = new MediaRecorder();  
  // 從麥克風(fēng)源進(jìn)行錄音  
  myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);  
  // 設(shè)置輸出格式  
  myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);  
  // 設(shè)置編碼格式  
  myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
  showRecord = new ShowRecorderAdpter();  
  if (Environment.getExternalStorageState().equals(  
    Environment.MEDIA_MOUNTED)) {  
   try {  
    path = Environment.getExternalStorageDirectory()  
      .getCanonicalPath().toString()  
      + "/RECORDERS";  
    File files = new File(path);  
    if (!files.exists()) {  
     files.mkdir();  
    }  
    listFile = files.list();  
   } catch (IOException e) {  
    e.printStackTrace();  
   }  
  }   
   start.setOnClickListener(this);  
  stop.setOnClickListener(this);  
  if (listFile != null) {  
   listView.setAdapter(showRecord);  
  }   
  }   
  @Override  
 public boolean onCreateOptionsMenu(Menu menu) {  
  getMenuInflater().inflate(R.menu.main, menu);  
  return true;  
 }   
  class ShowRecorderAdpter extends BaseAdapter {   
   @Override  
  public int getCount() {  
   return listFile.length;  
  }   
   @Override  
  public Object getItem(int arg0) {  
   return arg0;  
  }   
   @Override  
  public long getItemId(int arg0) {  
   return arg0;   
   }   
   @Override  
  public View getView(final int postion, View arg1, ViewGroup arg2) {  
   View views = LayoutInflater.from(MainActivity.this).inflate(  
     R.layout.list_show_filerecorder, null);  
   TextView filename = (TextView) views  
     .findViewById(R.id.show_file_name);  
   Button plays = (Button) views.findViewById(R.id.bt_list_play);  
   Button stop = (Button) views.findViewById(R.id.bt_list_stop);   
    filename.setText(listFile[postion]);  
   // 播放錄音  
   plays.setOnClickListener(new OnClickListener() {  
    @Override  
    public void onClick(View arg0) {  
     try {  
      myPlayer.reset();  
      myPlayer.setDataSource(path + "/" + listFile[postion]);  
      if (!myPlayer.isPlaying()) {   
        myPlayer.prepare();  
       myPlayer.start();  
      } else {  
       myPlayer.pause();  
      }   
      } catch (IOException e) {  
      e.printStackTrace();  
     }  
    }  
   });  
   // 停止播放  
   stop.setOnClickListener(new OnClickListener() {   
     @Override  
    public void onClick(View arg0) {  
     if (myPlayer.isPlaying()) {  
      myPlayer.stop();  
     }  
    }  
   });  
   return views;  
  }   
  }   
  @Override  
 public void onClick(View v) {  
  switch (v.getId()) {  
  case R.id.start:  
   final EditText filename = new EditText(this);  
   Builder alerBuidler = new Builder(this);  
   alerBuidler  
     .setTitle("請輸入要保存的文件名")  
     .setView(filename)  
     .setPositiveButton("確定",  
       new DialogInterface.OnClickListener() {  
        @Override  
        public void onClick(DialogInterface dialog,  
          int which) {  
         String text = filename.getText().toString();  
         try {  
          paths = path  
            + "/"  
            + text  
            + new SimpleDateFormat(  
              "yyyyMMddHHmmss").format(System  
              .currentTimeMillis())  
            + ".amr";  
          saveFilePath = new File(paths);  
          Log.e("activity", saveFilePath+"");  
          myRecorder.setOutputFile(saveFilePath  
            .getAbsolutePath());  
          saveFilePath.createNewFile();  
          myRecorder.prepare();  
          // 開始錄音  
          myRecorder.start();  
          start.setText("正在錄音中。。");  
          start.setEnabled(false);  
          aler.dismiss();  
          // 重新讀取 文件  
          File files = new File(path);  
          listFile = files.list();  
          // 刷新ListView  
          showRecord.notifyDataSetChanged();  
         } catch (Exception e) {  
          e.printStackTrace();  
         }   
         }  
       });  
   aler = alerBuidler.create();  
   aler.setCanceledOnTouchOutside(false);  
   aler.show();  
   break;  
  case R.id.stop:  
   if (saveFilePath.exists() && saveFilePath != null) {  
    myRecorder.stop();  
    myRecorder.release();  
    // 判斷是否保存 如果不保存則刪除  
    new AlertDialog.Builder(this)  
      .setTitle("是否保存該錄音")  
      .setPositiveButton("確定", null)  
      .setNegativeButton("取消",  
        new DialogInterface.OnClickListener() {  
         @Override  
         public void onClick(DialogInterface dialog,  
           int which) {  
          saveFilePath.delete();  
          // 重新讀取 文件  
          File files = new File(path);  
          listFile = files.list();  
          // 刷新ListView  
          showRecord.notifyDataSetChanged();  
         }  
        }).show();   
    }  
   start.setText("錄音");  
   start.setEnabled(true);  
  default:  
   break;  
  }   
  }   
  @Override  
 protected void onDestroy() {  
  // 釋放資源  
  if (myPlayer.isPlaying()) {  
   myPlayer.stop();  
   myPlayer.release();  
  }  
  myPlayer.release();  
  myRecorder.release();  
  super.onDestroy();  
 }   
 }  

標(biāo)簽: isp 代碼 權(quán)限

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:ios怎么判斷日期是周末?

下一篇:Go語言爬取網(wǎng)站磁力鏈接