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

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱: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;

    @Override
    protected 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 buttons
        mRedo.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 covered
        String[] 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 done
        if (mUtterance == null) {
            mCommand.setText("Finished: Thank You!");
            return;
        }
        Log.d(TAG, "going to record " + mUtterance.toString());

        // fix up UI
        mCommand.setText("Say: \"" + utterances[index] + "\"");
        final String status = "item " + (index + 1) + "/" + utterances.length;

        // start the microphone
        mSampleRate = 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 click
        try {
            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 recording
        mRecord.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导入到真机测试,有些依赖于具体设备的功能,只能真机测试

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式-老费

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值