发布于 2017-09-22 12:15:37 | 182 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


本篇文章中主要介绍了android 通过MediaRecorder实现简单的录音示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

整理文档,搜刮出一个android 通过MediaRecorder实现简单的录音示例,稍微整理精简一下做下分享。

MainActivity


package com.centaur.collectvoice;

import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

  private final static String TAG = "collectvoice";
  MediaRecorder mediaRecorder = new MediaRecorder();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  /**
   * 开始按钮
   * @param view
   * @throws IOException
   */
  public void onStart(View view) throws IOException {
    Toast.makeText(this, "开始收集", Toast.LENGTH_SHORT).show();
    // 第1步:设置音频来源(MIC表示麦克风)
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    //第2步:设置音频输出格式(默认的输出格式)
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    //第3步:设置音频编码方式(默认的编码方式)
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    //创建一个临时的音频输出文件
//    audioFile = File.createTempFile("record_", ".amr");
    if (FileUtils.makeFolder("VOICE")){//一个简单的判断文件夹是不是存在 不存在就创建
      String path = Environment.getExternalStorageDirectory().toString() + "/" + "VOICE/";
      String filePath =path+"record_.amr";
      File file = new File(filePath);
      //第4步:指定音频输出文件
      mediaRecorder.setOutputFile(file.getAbsolutePath());
      //第5步:调用prepare方法
      mediaRecorder.prepare();
      //第6步:调用start方法开始录音
      mediaRecorder.start();
    }
  }

  /**
   * 关闭按钮
   * @param view
   */
  public void onStop(View view) {
    Toast.makeText(this, "停止收集", Toast.LENGTH_SHORT).show();
    mediaRecorder.stop();
  }
}

工具类中用到的方法


 public static boolean makeFolder(String folder){
    File filefolder = new File(Environment.getExternalStorageDirectory().toString() + "/" + folder);
    if(!filefolder.exists()){
      filefolder.mkdir();
      if(filefolder.exists()){
        Log.d(TAG,folder+"创建成功");
      }
      else {
        Log.d(TAG,folder+"创建失败");
      }

    }
    return true;
  }

布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.centaur.collectvoice.MainActivity">

  <Button
    android:onClick="onStart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="收集声音" />

  <Button
    android:onClick="onStop"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="停止声音" />

</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务