1
該HTML文件有4個按鈕,記錄,停止錄製語音,播放,停止播放它。代碼看起來像這樣。與Phonegap,我想錄制語音,停止錄製,並在Android中播放
<!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插件部分是
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
對象。有沒有解決這個問題的方法?