PhoneGap的媒体API - 录制和播放音频 - 安卓 [英] Phonegap Media API - Record and Play Audio - Android
问题描述
我希望能够记录一些声音,然后必须将其播放的能力。其重要的,我要能<一个href="http://stackoverflow.com/questions/18542547/capture-audio-phonegap-3-0-without-interface">customize记录界面。
I'm hoping to record some audio and then have the ability to play it back. Its important for me to be able to customize the record interface.
在下面的例子中记录后,我得到了-1的持续时间,并没有音频播放。
In the below example after recording I get a duration of -1 and no audio is played.
第1步 新增媒体插件
cordova plugin add org.apache.cordova.media
第2步我的code
请注意SRC是穆萨从文档的要求。
Note src is "amr" as requested from docs.
在自适应多速率格式的Android设备录制音频。该 指定的文件应该结束了.AMR扩展。
Android devices record audio in Adaptive Multi-Rate format. The specified file should end with a .amr extension.
不过,myrecording.amr并不在我的文件结构存在,我希望它会被创建。
However, "myrecording.amr" does not exist in my file structure as I'm hoping it will be created.
Javascript的
var data = {
rec: ""
};
$scope.record = function(){
console.log('record');
var src = "myrecording.amr";
data.rec = new Media(src,
function() {
console.log("recordAudio():Audio Success");
},
function(err) {
console.log("recordAudio():Audio Error: "+ err.code);
});
data.rec.startRecord();
}
$scope.stopRecording = function(){
console.log('stop');
data.rec.stopRecord();
}
$scope.playRecording = function(){
console.log('play');
data.rec.play();
}
$scope.logDuration = function(){
console.log(data.rec.getDuration());
}
HTML
<button ng-click="record()">Record</button>
<button ng-click="stopRecording()">Stop Record</button>
<button ng-click="playRecording()">Play Record</button>
<button ng-click="logDuration()">Log Duration</button>
输出从上面
没有播放音频时,我点击播放。
No audio is played when I click play.
0 999846 log record
1 001845 log stop
2 002000 log recordAudio():Audio Success
3 004657 log play
4 008989 log -1
任何帮助将是很大的AP preciated。让我知道如果我能回答任何问题。
Any help would be greatly appreciated. Let me know if I can answer any questions.
推荐答案
下面是我的步骤,得到这个工作。
Here are my steps for getting this to work.
ionic start RecordTest blank
ionic platform add ios
cordova plugin add org.apache.cordova.media
2。创建 RecordTest / WWW / myrecording.wav
3。粘贴低于code。在 RecordTest / WWW / index.html的
2. Create RecordTest/www/myrecording.wav
3. Paste the below code in RecordTest/www/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<!-- <script src="js/app.js"></script> -->
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Record audio
//
function recordAudio() {
var src = "myrecording.wav";
var mediaRec = new Media(src, onSuccess, onError);
// Record audio
mediaRec.startRecord();
// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
setAudioPosition(recTime + " sec");
if (recTime >= 3) {
clearInterval(recInterval);
mediaRec.stopRecord();
mediaRec.play();
}
}, 1000);
}
// PhoneGap is ready
//
function onDeviceReady() {
recordAudio();
}
// onSuccess Callback
//
function onSuccess() {
console.log("recordAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
//
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
</script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
<title>Device Properties Example</title>
<!-- <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script> -->
<p id="media">Recording audio...</p>
<p id="audio_position"></p>
</ion-content>
</ion-pane>
</body>
</html>
4。运行离子的emulate IOS
以上应该工作。
4. Run ionic emulate ios
The above should work.
注意:这取决于你正在开发的平台上的扩展变化
这篇关于PhoneGap的媒体API - 录制和播放音频 - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!