Rexsee API介紹:Android音頻錄製,AudioRecorder函數與源碼

Audio系統在Android中負責音頻方面輸入/輸出層次,通常負責播放PCM聲音輸出和從外部獲取PCM聲音,以及管理聲音設備和設置。這裏主要是談談其中錄製這塊:AudioRecorder。
相對於MediaRecorder來講,AudioRecorder更接近底層,爲咱們封裝的方法也更少。並且音頻的採樣率、聲道等等均可以參數配置,可是問題是在模擬器中AudioRecorder卻不怎麼好用。

關於Audio系統中其它幾個類,如AudioCapture、AudioPlayer,也能夠在rexsee的開源社區裏找到: http://www.rexsee.com/CN/helpReference.php

【函數】 void record()
【說明】 錄製音頻。錄製成功觸發事件onRecordAudioSuccessed,失敗則觸發onRecordAudioFailed。
【返回】
【示例】
rexseeAudioRecorder.record();

【函數】 void upload(String action,String name)
【說明】 將錄製好的音頻直接上傳,一般在onRecordAudioSuccessed事件中調用。
【返回】
【參數】 action:上傳的服務器程序url,等同於HTML中<form action="......">中的action。 
name:上傳名稱,等同於HTML中<input type="file" name="......">中的name。 
action指向的服務器程序和HTML中表單對應的程序相同,示例的服務器程序參見rexseeUpload對象。

rexseeAudioRecorder.java源碼。。
/* 
* Copyright (C) 2011 The Rexsee Open Source Project 
* 
* Licensed under the Rexsee License, Version 1.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.rexsee.com/CN/legal/license.html 
* 
* 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 rexsee.multimedia;  
 
import rexsee.core.browser.ActivityResult.ActivityResultListener;  
import rexsee.core.browser.JavascriptInterface;  
import rexsee.core.browser.RexseeBrowser;  
import rexsee.core.browser.UrlListener;  
import rexsee.core.lang.RexseeLanguage;  
import rexsee.core.transportation.RexseeUpload;  
import android.app.Activity;  
import android.content.ContentResolver;  
import android.content.Context;  
import android.content.Intent;  
import android.database.Cursor;  
import android.net.Uri;  
import android.provider.MediaStore.Audio.Media;  
 
public class RexseeAudioRecorder implements JavascriptInterface {  
 
       private static final String INTERFACE_NAME = "AudioRecorder";  
       @Override  
       public String getInterfaceName() {  
               return mBrowser.application.resources.prefix + INTERFACE_NAME;  
       }  
       @Override  
       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {  
               return this;  
       }  
       @Override  
       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {  
               return new RexseeAudioRecorder(childBrowser);  
       }  
 
       public static final String EVENT_ONRECORDAUDIOSUCCESSED = "onRecordAudioSuccessed";  
       public static final String EVENT_ONRECORDAUDIOFAILED = "onRecordAudioFailed";  
 
       public static final String URI_MEDIA_AUDIO = "content://media/external/audio/media";  
 
       private String mediaFile = null;  
 
       private final RexseeBrowser mBrowser;  
 
       public RexseeAudioRecorder(RexseeBrowser browser) {  
 
               mBrowser = browser;  
               browser.eventList.add(EVENT_ONRECORDAUDIOSUCCESSED);  
               browser.eventList.add(EVENT_ONRECORDAUDIOFAILED);  
 
               browser.urlListeners.add(new UrlListener(mBrowser.application.resources.prefix + ":audio") {  
                       @Override  
                       public void run(Context context, final RexseeBrowser browser, final String url) {  
                               new Thread() {  
                                       @Override  
                                       public void run() {  
                                               String html = "<HTML><HEAD><TITLE>" + url + "</TITLE></HEAD>";  
                                               html += "<BODY style='margin:0px;background-color:black;color:white;'>";  
                                               ContentResolver contentResolver = mBrowser.getContext().getContentResolver();  
                                               browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);  
                                               Cursor cursor = contentResolver.query(Uri.parse(URI_MEDIA_AUDIO), new String[]{"_id", "_data"}, null, null, "_id desc");  
                                               html += "<table width=100% cellspacing=0 style='color:white;font-size:16px;'>";  
                                               if (cursor.getCount() == 0) {  
                                                       html += "<tr><td style='padding:10px;'> audio found.</td></tr>";  
                                               } else {  
                                                       for (int i = 0; i < cursor.getCount(); i++) {  
                                                               cursor.moveToPosition(i);  
                                                               html += "<tr><td onclick=\"" + browser.function.getInterfaceName() + ".go('" + URI_MEDIA_AUDIO + "/" + cursor.getInt(0) + "');\" style='border-bottom:1px solid #222222; padding:10 5 10 5;word-break:break-all;'>" + cursor.getString(1) + "</td></tr>";  
                                                       }  
                                               }  
                                               cursor.close();  
                                               html += "</table>";  
                                               html += "</BODY>";  
                                               html += "</HTML>";  
                                               browser.function.loadHTMLWithoutHistory(html);  
                                       }  
                               }.start();  
                       }  
                       @Override  
                       public boolean shouldAddToHistory(Context context, RexseeBrowser browser, String url) {  
                               return true;  
                       }  
               });  
 
       }  
 
       public String getLastMediaAudio() {  
               ContentResolver contentResolver = mBrowser.getContext().getContentResolver();  
               Cursor cursor = contentResolver.query(Uri.parse(URI_MEDIA_AUDIO), new String[]{"_data"}, null, null, "_id desc");  
               if (cursor == null || cursor.getCount() == 0) return "";  
               cursor.moveToFirst();  
               String rtn = "file://" + cursor.getString(0);  
               cursor.close();  
               return rtn;  
       }  
 
       public void record() {  
               try {  
                       Intent intent = new Intent(Media.RECORD_SOUND_ACTION);  
                       mBrowser.activityResult.start(intent, new ActivityResultListener() {  
                               @Override  
                               public void run(int resultCode, Intent resultIntent) {  
                                       if (resultCode == Activity.RESULT_OK) {  
                                               mediaFile = getLastMediaAudio();  
                                               mBrowser.eventList.run(EVENT_ONRECORDAUDIOSUCCESSED, new String[]{mediaFile});  
                                       } else {  
                                               mediaFile = null;  
                                               mBrowser.eventList.run(EVENT_ONRECORDAUDIOFAILED);  
                                       }  
                               }  
                       });  
               } catch (Exception e) {  
                       mBrowser.exception(getInterfaceName(), e);  
               }  
       }  
 
       public void prepareUpload() {  
               if (mediaFile == null) {  
                       mBrowser.exception(getInterfaceName(), "Audio is not ready.");  
               } else {  
                       RexseeUpload uploadObject = (RexseeUpload) mBrowser.interfaceList.get(mBrowser.application.resources.prefix + RexseeUpload.INTERFACE_NAME);  
                       if (uploadObject == null) {  
                               mBrowser.exception(getInterfaceName(), "Upload object is not ready.");  
                       } else {  
                               uploadObject.selectedFiles.add(mediaFile);  
                       }  
               }  
       }  
 
       public void upload(String action, String name) {  
               if (mediaFile == null) {  
                       mBrowser.exception(getInterfaceName(), "Audio is not ready.");  
               } else {  
                       RexseeUpload uploadObject = (RexseeUpload) mBrowser.interfaceList.get(mBrowser.application.resources.prefix + RexseeUpload.INTERFACE_NAME);  
                       if (uploadObject == null) {  
                               mBrowser.exception(getInterfaceName(), "Upload object is not ready.");  
                       } else {  
                               uploadObject.clear();  
                               uploadObject.selectedFiles.add(mediaFile);  
                               uploadObject.upload(action, name);  
                       }  
               }  
       }  
 
}

 僅對Rexsee的源碼和函數事件作了整理,相關的demo或源碼解析能夠在Rexsee社區瞭解,目前Rexsee已提供了近2000個擴展,覆蓋Android原生功能超過90%,且所有開放: http://www.rexsee.com/ 
相關文章
相關標籤/搜索