簡介
使用Media插件可以在設備上播放音頻與錄製音頻。
注意:當前實現並不是遵循W3C規範的媒體捕捉,這裏只是提供方便。未來的實現將堅持最新的W3C規範和可能棄用當前的API。
安裝
cordova plugin add cordova-plugin-media
支持的平臺
· Android
· BlackBerry 10
· iOS
· Windows Phone 7 and 8
· Tizen
· Windows 8
· Windows
· Browser
使用方法
這個插件定義一個全局媒體構造函數
雖然在全局範圍內,這是deviceready事件以後纔可用
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(Media);
}
詳情
Media
初始化Media對象
var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);
參數Parameters
src: 一個URI包含了這個音頻內容。
mediaSuccess: (可選)一個媒體對象已完成當前播放,記錄,或停止行動,執行這個回調函數。(函數)
mediaError: (可選)如果出現錯誤,執行這個回調函數。(函數)
mediaStatus: (可選)執行顯示狀態變化的回調函數。(函數)
注意:cdvfile路徑可支持到src參數使用:
var my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);
常量Constants
下面的常量是報道的唯一參數mediastatus回調:
· Media.MEDIA_NONE = 0;
· Media.MEDIA_STARTING = 1;
· Media.MEDIA_RUNNING = 2;
· Media.MEDIA_PAUSED = 3;
· Media.MEDIA_STOPPED = 4;
方法Methods
media.getCurrentAmplitude: 返回在音頻文件的當前位置。
media.getCurrentPosition: 返回在音頻文件的當前位置。
media.getDuration: 返回一個音頻文件的持續時間。
media.play: 開始或恢復播放一個音頻文件。
media.pause: 暫停播放音頻文件
media.pauseRecord: 音頻文件暫停錄音
media.release: 釋放操作系統底層的音頻資源。
media.resumeRecord: 恢復音頻文件記錄
media.seekTo: 移動到當前播放音頻文件的位置。
media.setVolume: 音頻播放的音量設置
media.startRecord: 開始錄製音頻文件
media.stopRecord: 停止錄製音頻文件
media.stop: 停止播放音頻文件
media.getCurrentAmplitude
返回當前記錄的電流幅值。
media.getCurrentAmplitude(mediaSuccess, [mediaError]);
支持的平臺Supported Platforms
· Android
· iOS
參數Parameters
mediaSuccess: 回調,通過電流的幅值(0 - 1)。
mediaError: (可選)回調執行如果出現錯誤。
media.getCurrentPosition
返回在一個音頻文件的當前位置。還更新了媒體對象的位置參數.
media.getCurrentPosition(mediaSuccess, [mediaError]);
參數Parameters
mediaSuccess: 回調,當前位置的秒數。
mediaError: (可選)回調執行如果出現錯誤。
media.getDuration
在短時間內返回一個音頻文件的持續時間。如果時間是未知的,它返回的值爲1。
media.getDuration();
media.pause
暫停播放音頻文件
media.pause();
media.pauseRecord
暫停錄製音頻文件
media.pauseRecord();
支持的平臺Supported Platforms
· iOS
media.play
開始或恢復播放音頻文件。
media.play();
media.release
發佈操作系統底層的音頻資源。這對於Android來說尤爲重要,因爲有一個有限的媒體播放OpenCore實例。應用程序不再需要任何媒體資源應該調用release 函數。
media.release();
media.resumeRecord
恢復錄製音頻文件
media.resumeRecord();
支持的平臺Supported Platforms
· iOS
media.seekTo
設置當前的位置在一個音頻文件。
media.seekTo(milliseconds);
參數Parameters
· milliseconds: 設置播放音頻中的位置,以毫秒爲單位。
media.setVolume
一個音頻文件的音量設置。
media.setVolume(volume);
參數Parameters
· volume: 音量設置播放。該值必須在0到1的範圍內。
支持的平臺Supported Platforms
· Android
· iOS
media.startRecord
開始錄製音頻文件
media.startRecord();
支持的平臺Supported Platforms
· Android
· iOS
· Windows Phone 7 and 8
· Windows
media.stop
停止播放音頻文件
media.stop();
media.stopRecord
停止錄製音頻文件
media.stopRecord();
支持的平臺Supported Platforms
· Android
· iOS
· Windows Phone 7 and 8
· Windows
示例
示例一:音頻播放/暫定
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>Hello World</title>
<style>
.line{
padding: 10px;
}
</style>
</head>
<body>
<div class="app">
<h1>media插件</h1>
<div class="line"><button id="play">播放</button></div>
<div class="line"><button id="pause">暫定</button></div>
<div class="line"><button id="stop">停止</button></div>
<div class="line"><button id="release">釋放</button></div>
<div id="audio_current"></div>
<div id="audio_duration"></div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
index.js:
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
onDeviceReady: function() {
this.receivedEvent();
},
$$: function(id) {
return document.getElementById(id);
},
receivedEvent: function() {
var src = "http://ws.stream.qqmusic.qq.com/C1000031Ecjg1tdNzL.m4a?fromtag=38";
var my_media = null;
var mediaTimer = null;
var timerDur = null;
function mediaSuccess() {
console.log("Media成功")
}
function mediaError(err) {
console.log("Media失敗")
}
// 開始或恢復播放一個音頻文件
function playAudio() {
if(my_media == null) {
// 初始化Media對象
my_media = new Media(src, mediaSuccess, mediaError);
}
// 播放音頻
my_media.play();
}
// 暫停播放音頻文件
function pauseAudio() {
if(my_media) {
my_media.pause();
}
// 清除定時器對象
clearInterval(mediaTimer);
mediaTimer = null;
}
// 釋放操作系統底層的音頻資源。
function releaseAudio() {
if(my_media) {
my_media.release();
}
}
// 停止播放音頻文件
function stopAudio() {
if(my_media) {
}my_media.stop();
// 清除定時器對象
clearInterval(mediaTimer);
mediaTimer = null;
}
// 返回在音頻文件的當前位置。
function getCurrent() {
if(mediaTimer == null){
mediaTimer = setInterval(function() {
my_media.getCurrentPosition(
// success callback
function(position) {
if(position > -1) {
console.log((position) + " sec");
}
document.getElementById('audio_current').innerHTML = position;
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
}
);
}, 1000);
}
}
// 返回一個音頻文件的持續時間。
function getDuration() {
// Get duration
var counter = 0;
var timerDur = setInterval(function() {
counter = counter + 100;
if(counter > 2000) {
clearInterval(timerDur);
}
var dur = my_media.getDuration();
if(dur > 0) {
clearInterval(timerDur);
document.getElementById('audio_duration').innerHTML = (dur) + " sec";
}
}, 100);
}
this.$$("play").onclick = function() {
playAudio();
getCurrent();
getDuration();
}
this.$$("pause").onclick = function() {
pauseAudio();
}
this.$$("release").onclick = function() {
releaseAudio();
}
this.$$("stop").onclick = function() {
stopAudio();
}
}
};
app.initialize();
運行:
點擊“播放”按鈕,開始播放音頻。
點擊“暫停”按鈕,暫定當前音頻播放,點擊“播放”按鈕,繼續播放音頻。
點擊“停止”按鈕,音頻播放停止。
點擊“釋放”按鈕,音頻播放停止,並釋放系統底層音頻資源。
示例二:錄製音頻
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>Hello World</title>
<style>
.line{
padding: 10px;
}
</style>
</head>
<body>
<div class="app">
<h1>media插件</h1>
<div class="line"><button id="startRecord">開始錄製</button></div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
index.js:
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
onDeviceReady: function() {
this.receivedEvent();
},
receivedEvent: function() {
function mediaSuccess() {
console.log("Media成功")
}
function mediaError(err) {
console.log("Media失敗")
}
// 錄製音頻
function recordAudio(){
var src = "myrecord.mp3";
var mediaRec = new Media(src, mediaSuccess, mediaError);
// 啓動錄製音頻
mediaRec.startRecord();
// 10秒後停止錄製
var recTime = 0;
var recInterval = setInterval(function(){
recTime = recTime + 1;
if(recTime >= 10){
clearInterval(recInterval);
mediaRec.stopRecord();
alert("錄製完成")
}
},1000);
}
document.getElementById("startRecord").onclick = function(){
recordAudio();
}
}
};
app.initialize();
運行:
點擊“開始錄製”按鈕,開始錄音
10秒後,自動停止
打開手機文件管理器的根目錄,可以找到我們錄製的音頻文件