与PhoneGap的,我想录制语音,停止录制,并发挥它在Android中 [英] with Phonegap, I would like to record voice, stop recording, and playing it in Android

查看:162
本文介绍了与PhoneGap的,我想录制语音,停止录制,并发挥它在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此输入code HTML文件有4个按钮,录制,停止录制声音,播放,停止播放。在code看起来是这样的。

enter code hereThe HTML file has 4 buttons that record, stop recording the voice, and play, stop playing it. the code looks like this.

<!DOCTYPE HTML>
<html>
    <head>
        <title>Cordova</title>
        <script type="text/javascript" charset="utf-8" src="scripts/cordova-1.9.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="scripts/jquery-1.7.2.min.js"></script>

        <script>
            $(document).ready(function(){
                $("#record").on("click", function(){
                    alert("record start");  
                    window.plugins.VoicePlugin.record(function(){alert("yo");}, 
                                                    function(){alert("yol");},
                                                    "voice.3gp");
                });

                $("#stoprecord").on('click', function(){
                    alert("record stop");
                    window.plugins.VoicePlugin.stoprecord(function(){},
                                                        function(){},
                                                        "voice.3pg");
                });

                $("#play").on("click", function(){
                    alert("play");
                    window.plugins.VoicePlugin.play(function(){},
                            function(){},
                            "voice.3pg");
                });

                $("#stopplay").on("click", function(){
                    alert("stop play");
                    window.plugins.VoicePlugin.stopplay(function(){},
                            function(){},
                            "voice.3pg");
                });
            }); 
        </script>
    </head>
    <body>
        <button id="record">Start Recording</button>
        <button id="stoprecord">Stop Recording</button>
        <button id="play">Start Playing</button>
        <button id="stopplay">Stop Playing</button>
    </body>
</html>

Android的插件部分是

The Android Plugin part is

/ **  *  * /

/** * */

package com.saxoo.voice;

import java.io.IOException;

    import org.apache.cordova.api.Plugin;
    import org.apache.cordova.api.PluginResult;
    import org.json.JSONArray;

    import android.media.MediaPlayer;
    import android.media.MediaRecorder;
    import android.util.Log;

/**
 * @author sbapp008
 *
 */
public class VoicePlugin extends Plugin {

    /* (non-Javadoc)
     * @see org.apache.cordova.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
     */

    public static final String Record = "record";
    public static final String Play = "play";
    public static final String Stopplaying = "stopplaying";
    public static final String Stoprecording = "stoprecording";
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;

    private static MediaRecorder mRecorder = null;
    private static MediaPlayer mPlayer = null;

    @Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
        PluginResult result = null;

        if(Record.equals(action)){ //data에 filename
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            try {
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }

            mRecorder.start();

        } else if(Play.equals(action)){ //data에 filename 
            mPlayer = new MediaPlayer();

            try {
                mPlayer.setDataSource(mFileName);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }
        } else if(Stopplaying.equals(action)){
            mPlayer.release();
            mPlayer = null;
        } else{
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }


        return null;
    }

}

的一点是,我应该留在一个活动,记录,停止录制,播放和停止播放声音。如果我使用PhoneGap的,因为它只是通过插件发送一些字符串,则MediaRecorder和MediaPlayer对象被创建,每次销毁。我按下录音按钮是在HTML中,创建了MediaRecorder对象,但按下停止录制按钮无法阻止刚创建的MediaRecorder对象。是否有此问题的任何解决方案?

The point is, i should stay in an Activity that record, stop recording, play and stop playing the voice. If I use phonegap, since it just send some strings via plugin, the MediaRecorder and MediaPlayer object is created and destroyed each time. I push the record button which is in html, the MediaRecorder object is created, but pushing the stop recording button cannot stop the MediaRecorder object that is just created. Is there any solution for this problem?

推荐答案

通过PhoneGap的我记录的声音和播放录制的声音不加插件。 PhoneGap的媒体我只瞬移插件,我的应用程序是这样的:

With Phonegap I record voice and play recording voice without addition plugins. I only conect plugin of Phonegap Media to my app like this:

app/res/xml/config.xml

<plugin name="Media" value="org.apache.cordova.AudioHandler" />

app/AndroidManifest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

code有:

Code there:

<!DOCTYPE html>
<html>
  <head>
  <title></title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta charset="utf-8">

    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" src="jquery-1.7.2.js"></script>

    <script type="text/javascript">

        var deviceready = false;
        var mediaVar = null;
        var recordFileName = "recording.wav";
        function onBodyLoad()
        {        
            document.addEventListener("deviceready", onDeviceReady, false);
            deviceready = true;
        }

        $(document).ready(function(){

            //validation to check if device is ready is skipped

            $("#recordBtn").click(function(){
                record();                  
            });

            $("#playBtn").click(function(){
                play();
            });

            $("#stopBtn").click(function(){
                stop();
            });
        });

        function record()
        {
        if (mediaVar != null) {
                mediaVar.release();
            }
            createMedia(function(){
                status = "recording";
                mediaVar.startRecord();
            },onStatusChange);
        }

        function createMedia(onMediaCreated, mediaStatusCallback){
            if (mediaVar != null) {
                onMediaCreated();
                return;
            }

            if (typeof mediaStatusCallback == 'undefined') 
                mediaStatusCallback = null;

                mediaVar = new Media(recordFileName, function(){
                    log("Media created successfully");
                }, onError, mediaStatusCallback); 
                onMediaCreated();
        }

        function stop()
        {
            if (mediaVar == null)
                return;

            if (status == 'recording')
            {
                mediaVar.stopRecord();
                log("Recording stopped");
            }
            else if (status == 'playing')
            {
                mediaVar.stop();            
                log("Play stopped");
            } 
            else
            {
                log("Nothing stopped");
            }
            status = 'stopped';
        }

        function play()
        {
            createMedia(function(){
                status = "playing";
            mediaVar = new Media('/sdcard/'+recordFileName, function(){
                    log("Media created successfully");
                }, onError); 

                mediaVar.play();    
            });
        }

        function onStatusChange()
        {
        }

        function onSuccess()
        {
            //do nothing
        }

        function onError(err)
        {
            if (typeof err.message != 'undefined')
                err = err.message;
            alert("Error : " + err);
        }

        function log(message)
        {
            console.info(message);
        }
 </script>
  </head>
  <body onload="onBodyLoad()">
      Recorder<br>
      <input type="button" name="recordBtn" id="recordBtn" value="Record">
      <input type="button" name="stopBtn" id="stopBtn" value="Stop">
      <input type="button" name="playBtn" id="playBtn" value="Play">
  </body>
</html>

在Android设备上,PhoneGap的媒体对象有以下重要的行为,没有得到很好的记录(的http://software.intel.com/en-us/articles/media-sample-using-phonegap#id_android):

On Android devices, the PhoneGap media object has the following important behaviors that are not well documented (http://software.intel.com/en-us/articles/media-sample-using-phonegap#id_android):

  • 在my_audio =新媒体(myRecording100.wav',onMediaCallSuccess, onMediaCallError):当myRecording100.wav不存在,它 将创建该文件;当它的存在,它会打开它。
  • 在my_audio.sto precord():当STO precord()之称,在.WAV
    媒体文件移动到/ SD卡文件夹中。所以,检查时,如果previous 录制.WAV存在,code应该在/ SD卡文件夹中。如果 它确实存在,它应该打开从
    文件 /sdcard/myRecording100.wav。
  • "my_audio = new Media('myRecording100.wav', onMediaCallSuccess, onMediaCallError)": when "myRecording100.wav" does not exist, it will create the file; when it exists, it opens it.
  • "my_audio.stopRecord()": when "stopRecord()" is called, the .wav
    media file is moved to "/sdcard" folder. So when checking if previous recorded .wav exists, the code should look under "/sdcard" folder. If it does exist, it should open the file from
    "/sdcard/myRecording100.wav".

希望这有助于你。

这篇关于与PhoneGap的,我想录制语音,停止录制,并发挥它在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆