zoukankan      html  css  js  c++  java
  • Android recording 录音功能 简单使用小实例

    package com.app.recordingtest;
    
    import java.io.File;
    import java.io.IOException;
    
    import android.app.Activity;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;
    /**
     * v1.0 version
     * 
     * @author Administrator
     * 
     */
    public class MainActivity extends Activity implements OnClickListener {
    	Button button1, button2;
    	MediaRecorder recorder;
    	ListView listView1;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		button1 = (Button) findViewById(R.id.button1);
    		button2 = (Button) findViewById(R.id.button2);
    		listView1 = (ListView) findViewById(R.id.listView1);
    		button1.setOnClickListener(this);
    		button2.setOnClickListener(this);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    	@Override
    	public void onClick(View v) {
    		switch (v.getId()) {
    			case R.id.button1 :
    				try {
    					begin();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    				break;
    			case R.id.button2 :
    				stop();
    				break;
    
    			default :
    				break;
    		}
    
    	}
    
    	private void stop() {
    		recorder.stop(); // 暂停
    		recorder.reset(); // 重置
    		recorder.release(); // 释放
    		Toast.makeText(MainActivity.this, "暂停..", Toast.LENGTH_SHORT).show();
    	}
    
    	private void begin() throws IOException {
    		final String PATH = "/mnt/sdcard"; // 设置路径
    		File filePath = new File(PATH);
    		filePath = File.createTempFile("/fileName", ".amr/", filePath);
    		String outPath = filePath.getPath().toString();
    		Toast.makeText(MainActivity.this, "开始.." + outPath, Toast.LENGTH_SHORT)
    				.show();
    		recorder = new MediaRecorder(); // 创建对象
    		recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置来源
    		recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // 设置输出格式
    		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 设置编码
    		recorder.setOutputFile(outPath); // 设置输出路径
    		Toast.makeText(MainActivity.this,
    				" .." + filePath.getAbsolutePath().toString(),
    				Toast.LENGTH_SHORT).show();
    		try {
    			recorder.prepare(); // 准备录音
    			recorder.start(); // 开始录音
    			Toast.makeText(MainActivity.this, "开始..", Toast.LENGTH_SHORT)
    					.show();
    		} catch (IllegalStateException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    
    }
    

  • 相关阅读:
    spring boot 启动报错(spring-boot-devtools热部署后):The elements [spring.resources.cache-period] were left unbound. Update your application's configuration
    一分钟搞定:spring boot 热部署 (基于Idea)
    如何将新项目添加到github仓库中?只需简单几步~即可实现
    spring boot 日志文件配置(logback-spring.xml)亲测可用!
    java后台读取/解析 excel表格
    String字符串存入数据库中超出最大长度(oracle varchar2 4000)?应合理分条存储(java实现-工具/方法)
    笔试题-sql语句
    总结--看帖收“货”
    Oracle-1
    蓝桥杯- 基础练习: 字母图形
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/3697396.html
Copyright © 2011-2022 走看看