【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

今天下午,无意看到android一个原生的app代码,也就一个文件SpeechRecorderActivity.java。开发android app原来不难。插句话,android所有的自带app都是开源的,它们其实才是最好的学习资料。接着大家可以看一下,具体内容如下,

/** Copyright (C) 2007 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.android.speechrecorder;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.speech.srec.Recognizer;
import android.speech.srec.WaveHeader;
import android.speech.srec.MicrophoneInputStream;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class SpeechRecorderActivity extends Activity {private static final String TAG = "SpeechRecorderActivity";private static final int DURATION_SEC = 7;private Handler mHandler;private TextView mCommand;private TextView mStatus;private Button mRecord;private Button mRedo;private RadioButton m8KHz;private RadioButton m11KHz;private RadioButton mCall;private RadioButton mDialNanp;private RadioButton mDialPairs;private InputStream mMicrophone;private ByteArrayOutputStream mBaos;private File mUtterance;private int mSampleRate;private Thread mThread;private boolean mStoppedListening;@Overrideprotected void onCreate(Bundle icicle) {super.onCreate(icicle);mHandler = new Handler();setContentView(R.layout.recorder);mCommand = (TextView) findViewById(R.id.commandText);mStatus = (TextView) findViewById(R.id.statusText);mRecord = (Button) findViewById(R.id.recordButton);mRedo = (Button) findViewById(R.id.redoButton);m8KHz = (RadioButton)findViewById(R.id.codec8KHzRadioButton);m11KHz = (RadioButton)findViewById(R.id.codec11KHzRadioButton);mCall = (RadioButton)findViewById(R.id.callRadioButton);mDialNanp = (RadioButton)findViewById(R.id.dialNanpRadioButton);mDialPairs = (RadioButton)findViewById(R.id.dialPairsRadioButton);mCommand.setText("Please click 'Record' to begin");mRecord.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (false) {Log.d(TAG, "mRecord.OnClickListener.onClick");}setupRecording();}});mRedo.setEnabled(false);mRedo.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (false) {Log.d(TAG, "mRedo.onClickListener.onClick");}mUtterance.delete();setupRecording();}});m8KHz.setText("PCM/16bit/8KHz");m11KHz.setText("PCM/16bit/11KHz");m11KHz.setChecked(true);mCall.setChecked(true);}private void setupRecording() {Log.d(TAG, "setupRecording");// disable buttonsmRedo.setEnabled(false);mRecord.setEnabled(false);m8KHz.setFocusable(false);m11KHz.setFocusable(false);mCall.setFocusable(false);mDialNanp.setFocusable(false);mDialPairs.setFocusable(false);// find the first utterance not coveredString[] utterances = mCall.isChecked() ? mCallUtterances :mDialNanp.isChecked() ? mDialNanpUtterances :mDialPairs.isChecked() ? mDialPairsUtterances :null;mUtterance = null;int index = -1;for (int i = 0; i < utterances.length; i++) {File u = new File(getDir("recordings", MODE_PRIVATE),utterances[i].toLowerCase().replace(' ', '_') + ".wav");if (!u.exists()) {mUtterance = u;index = i;break;}}// check if doneif (mUtterance == null) {mCommand.setText("Finished: Thank You!");return;}Log.d(TAG, "going to record " + mUtterance.toString());// fix up UImCommand.setText("Say: \"" + utterances[index] + "\"");final String status = "item " + (index + 1) + "/" + utterances.length;// start the microphonemSampleRate = m8KHz.isChecked()? 8000 :m11KHz.isChecked() ? 11025 :11025;mBaos = new ByteArrayOutputStream(mSampleRate * 2 * 20);try {mMicrophone = new MicrophoneInputStream(mSampleRate, mSampleRate * 15);//            mMicrophone = logInputStream(mUtterance.toString(), mMicrophone, mSampleRate);} catch (IOException e) {}// post a number of delayed events to update the UI and to stop recording// after a few seconds.for (int i = 0; i <= DURATION_SEC; i++) {final int remain = DURATION_SEC - i;mHandler.postDelayed(new Runnable() {public void run() {if (remain > 0) {mStatus.setText(status + "  Recording... " + remain);}else {mStatus.setText(status);stopRecording();}}}, i * 1000);}// now start a thread to store the audio.mStoppedListening = false;mThread = new Thread() {public void run() {Log.d(TAG, "run audio capture thread");byte buffer[] = new byte[512];while (!mStoppedListening) {try {int rtn = 0;rtn = mMicrophone.read(buffer, 0, 512);if (rtn > 0) mBaos.write(buffer, 0, rtn);} catch (IOException e) {}}}};mThread.start();// to avoid the button clicktry {Thread.sleep(100);} catch (InterruptedException ie) {}}private void stopRecording() {Log.d(TAG, "stopRecording");mStoppedListening = true;try {mThread.join();} catch (InterruptedException e) {}try {OutputStream out = new FileOutputStream(mUtterance.toString());try {byte[] pcm = mBaos.toByteArray();Log.d(TAG, "byteArray length " + pcm.length);WaveHeader hdr = new WaveHeader(WaveHeader.FORMAT_PCM,(short)1, mSampleRate, (short)16, pcm.length);hdr.write(out);out.write(pcm);} finally {out.close();mMicrophone.close();mBaos.close();}} catch (IOException e) {} finally {}// stop the recordingmRecord.setEnabled(true);mRedo.setEnabled(true);mCommand.setText("Got it!");}private final static String[] mCallUtterances = new String[] {"Call Adam Varro","Call Alex Lloyd","Call Amod Karve","Call Ana Maria Lopez","Call Ben Sigelman","Call Chris Vennard","Call Dana Pogoda","Call Daryl Pregibon","Call Davi Robison","Call David Barrett Kahn","Call David Hyman","Call Douglas Gordin","Call Gregor Rothfuss","Call James Sheridan","Call Jason Charo","Call Jeff Reynar","Call Joel Ward","Call John Milton","Call Lajos Nagy","Call Lori Sobel","Call Martin Jansche","Call Meghan McGarry","Call Meghan Shakar","Call Nilka Thomas","Call Pedro Colijn","Call Pramod Adiddam","Call Rajeev Sivaram","Call Rich Armstrong","Call Robin Watson","Call Sam Morales",};private final static String[] mDialPairsUtterances = new String[] {// all possible pairs"Dial 000 000 0000","Dial 101 010 1010","Dial 111 111 1111","Dial 202 020 2020","Dial 212 121 2121","Dial 222 222 2222","Dial 303 030 3030","Dial 313 131 3131","Dial 323 232 3232","Dial 333 333 3333","Dial 404 040 4040","Dial 414 141 4141","Dial 424 242 4242","Dial 434 343 4343","Dial 444 444 4444","Dial 505 050 5050","Dial 515 151 5151","Dial 525 252 5252","Dial 535 353 5353","Dial 545 454 5454","Dial 555 555 5555","Dial 606 060 6060","Dial 616 161 6161","Dial 626 262 6262","Dial 636 363 6363","Dial 646 464 6464","Dial 656 565 6565","Dial 666 666 6666","Dial 707 070 7070","Dial 717 171 7171","Dial 727 272 7272","Dial 737 373 7373","Dial 747 474 7474","Dial 757 575 7575","Dial 767 676 7676","Dial 777 777 7777","Dial 808 080 8080","Dial 818 181 8181","Dial 828 282 8282","Dial 838 383 8383","Dial 848 484 8484","Dial 858 585 8585","Dial 868 686 8686","Dial 878 787 8787","Dial 888 888 8888","Dial 909 090 9090","Dial 919 191 9191","Dial 929 292 9292","Dial 939 393 9393","Dial 949 494 9494","Dial 959 595 9595","Dial 969 696 9696","Dial 979 797 9797","Dial 989 898 9898","Dial 999 999 9999",};private final static String[] mDialNanpUtterances = new String[] {"Dial 211","Dial 411","Dial 511","Dial 811","Dial 911",// random numbers"Dial 653 5763","Dial 263 9072","Dial 202 9781","Dial 379 8229","Dial 874 9139","Dial 236 0163","Dial 656 7455","Dial 474 5254","Dial 348 8687","Dial 629 8602",//"Dial 272 717 8405",//"Dial 949 516 0162",//"Dial 795 117 7190",//"Dial 493 656 3767",//"Dial 588 093 9218","Dial 511 658 3690","Dial 440 301 8489","Dial 695 713 6744","Dial 581 475 8712","Dial 981 388 3579","Dial 840 683 3346","Dial 303 467 7988","Dial 649 504 5290","Dial 184 577 4229","Dial 212 286 3982","Dial 646 258 0115","Dial 427 482 6852","Dial 231 809 9260","Dial 681 930 4301","Dial 246 650 8339",};
}

这份代码是从2.*版本的android上发现的,但是现在还在不在就不知道了。代码的主要功能就是setupRecording和stopRecording。app的创建主要是由一个create函数完成的。如果还有一点难度的话,那么就是Thread的创建和等待了。

注:

上述代码如果需要运行,需要搭建android开发环境,有几个地方需要注意下,

1、需要安装jdk,这是基础

2、需要安装eclipse,这是IDE

3、需要安装ADT,一个eclipse上的插件,这样就可以直接创建android工程了

4、安装android sdk manager,注意配置下载对应版本的sdk

5、配置avd虚拟机,下载intel x86镜像,配置成nexus即可

6、创建android app,引入第三方库

7、熟悉android常用的几个类,即Activity、Service、ContentProvider、BroadcastRecevier

8、仿真测试

9、apk导入到真机测试,有些依赖于具体设备的功能,只能真机测试

​​​​​​https://www.cnblogs.com/gufengchen/p/11038029.html

随想录(一个android原生app的代码赏析)相关推荐

  1. android 界面代码,一个漂亮的app界面代码

    效果如下: device-2017-12-22-234223.png device-2017-12-22-234149.png 代码如下所示: xmlns:android="http://s ...

  2. android获取app用户数据,Android 原生app获取用户授权访问Autodesk云应用数据

    oAuth机制对于网站间的授权管理是很容易实现的,设置好app回调端口,当数据服务提供方拿到其用户授权,则返回授权码发送到回调端口.上一篇文章介绍了如何授权Forge app访问Autodesk 云应 ...

  3. android原生app转成web,转战WebApp: 最适合Android开发者的WebApp框架

    原文出处:林法鑫的博客. ps:原以为又是一篇前端从业者人云亦云的水文,其实是篇干货,文中提到的AndroidUI4Web也是作者开发的. 为什么需要转战WebApp开发 随着移动端设备越来越多, 微 ...

  4. Android原生app与H5交互

    1.开发背景. 随着当前开发迭代要求速度更快,改动更灵活,不需要发布版本就能实时更新app的内容,故当前H5制作界面越来越受到科技公司和开发者的欢迎. 2.实现交互 今天记录一下开发过程中用到的H5交 ...

  5. android app在线商城,ECMobile:只需一步即可生成iOS或Android原生APP商城应用

    App 快速开发服务已经屡见不鲜,国内有简网 App 工场.道有道.ECMobile这么简洁--你只需将一个集成过的文件夹放进ECShop(一款被广泛使用的开源 B2C 网店)安装根目录,无需任何配置 ...

  6. android生成ios程序,ECMobile:只需一步即可生成iOS或Android原生APP商城应用

    App 快速开发服务已经屡见不鲜,国内有简网 App 工场.道有道.ECMobile这么简洁--你只需将一个集成过的文件夹放进ECShop(一款被广泛使用的开源 B2C 网店)安装根目录,无需任何配置 ...

  7. Android原生App跳转到React Native App

    前言 众所周知,react native开发是前几年较火的一个话题,随着各种框架的更新换代,一些技术也会逐渐被掩埋.本篇是楼主在项目中总结出的一些开发经验.主要讲了react native和原生的ap ...

  8. Android原生app安全测试

    1.应用权限检测 将apk通过apktool进行反编译,得到androidManifest.xml文件,可查看应用权限信息,如图所示: 应不存在用户不知情或未授权的情况子啊,获取相应信息. 2.代码混 ...

  9. 提供一个Android原生的Progress——SwipeToRefreshLayout下拉刷新时的等待动画

    先来上个图看看效果: 这里我为什么要单独把这个拿出来呢,因为最近才开始接触Android最新的东西,也就是5.0以上的东西,发现Android提供的SwipeToRefreshLayout是没有上拉加 ...

最新文章

  1. Spring Boot解决无法访问图片的问题
  2. 阿尔法大蛋智能机器人功能_智能机器人怎么选?阿尔法蛋大蛋2.0助力孩子新学期...
  3. java 对象锁定_少锁定Java对象池
  4. CXF学习(2) helloworld
  5. java户名的正则表达式_java – 用于验证用户名的正则表达式
  6. epoll nio区别_高性能网络服务器编程:为什么linux下epoll是最好,Netty要比NIO.2好?...
  7. 微信公众号开发 ----微信服务的接入(1)
  8. a+ open python_python编程之文件操作
  9. linux实验的试验环境,Linux基本环境_实验日志解析.docx
  10. php连接oracle数据库超详细教程
  11. java线程状态与操作系统线程状态的关系
  12. 佛系前端面试题记录--第五周
  13. 如何利用SPSS软件进行数据的描述性分析
  14. Win7任务管理器找不到进程怎么办
  15. Winform开发框架之通用Windows摄像头调用拍照--SNF快速开发平台3.3-Spring.Net.Framework...
  16. css文字抖动解决办法
  17. 对辊柱塞式成型机总体设计
  18. python 安装已下载好的模块
  19. 雅思在线模拟测试软件,上海雅思在线模拟测试
  20. 什么是API?(详细解说)

热门文章

  1. 初学者一些常用的SQL语句(二)
  2. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14
  3. php __FILE__ __DIR__魔术常量的使用【PHP进阶教程】
  4. 使用vSphere Power Cli初始化数据中心
  5. centos下查看调优apache状态的相关命令
  6. 平衡二叉树【学习笔记】
  7. Django的认证系统(auth)
  8. 将jar包制作成docker镜像
  9. Spotlight on MySQL
  10. MongoDB - 分片管理