简体   繁体   English

播放本地MP3 Android-科尔多瓦“媒体未定义”

[英]Playing Local MP3 Android - Cordova “Media Undefined”

I'm building an app using PhoneGap/Cordova that is to play an mp3 file in the background. 我正在使用PhoneGap / Cordova构建一个可在后台播放mp3文件的应用。 I have added the Cordova Media plugin, but when I test in my browser I'm getting "Media" undefined as if it can't find the plugin. 我已经添加了Cordova Media插件,但是当我在浏览器中进行测试时,却无法定义“ Media”,好像找不到插件一样。

$(document).ready(function() {

   var bgMedia = new Media( getPhoneGapPath('timer') );
   bgMedia.play();  

    function getPhoneGapPath(audiofile) {

      var path = window.location.pathname;
          path = path.substr( path, path.length - 23 );
          path = path + 'audio/'+audiofile+'.mp3';
      return 'file://' + path;
     }
});

I have confirmed that the path is correct for the file. 我已确认该文件的路径正确。 My question is twofold, how do I get the Media plugin configured properly to actually worse (am I missing another step) and what is the proper way to call and play the file? 我的问题是双重的,我如何正确配置Media插件以使其实际上变得更糟(我错过了下一步),以及调用和播放文件的正确方法是什么?

Using current version of Cordova. 使用当前版本的Cordova。

After some digging I was able to make the following work: 经过一番挖掘,我能够进行以下工作:

 // Wait for device API libraries to load // document.addEventListener("deviceready", onDeviceReady, false); // device APIs are available // function onDeviceReady() { playAudio(); } // Audio player // var my_media = null; var mediaTimer = null; function getCordovaPath() { var path = window.location.pathname; if (device.platform == "Android") { path = "/android_asset/www/"; } // path = cordova.file.applicationDirectory + 'www/' // path = path.substr( path, path.length - 23 ); //path = path + 'audio/'+audiofile+'.mp3'; return 'file://' + path; } // Play audio // function playAudio() { // Create Media object from src //my_media = new Media(src, onSuccess, onError); my_media = new Media(getCordovaPath() + 'audio/timer.mp3', // success callback function () { console.log("playAudio():Audio Success"); }, // error callback function (err) { console.log("playAudio():Audio Error: " + err); }); // Play audio my_media.play(); } // Pause audio // function pauseAudio() { if (my_media) { my_media.pause(); } } // Stop audio // function stopAudio() { if (my_media) { my_media.stop(); } } // onSuccess Callback // function onSuccess() { console.log("playAudio():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; } 

Cordova plugins works only on devices or emulators. Cordova插件仅在设备或仿真器上有效。 If you run the project in the browser you need to mock them to prevent that the app crash. 如果您在浏览器中运行项目,则需要模拟它们以防止应用程序崩溃。

When I run the project in the browser I uncomment this line from index.html: 当我在浏览器中运行项目时,我从index.html取消注释此行:

<script src="js/mocks/plugin.media.js"></script>

plugin.media.js: plugin.media.js:

window.Media = {
  get: function(){},
  play: function(){},
  stop: function(){},
  seekTo: function(){},
  pause: function(){},
  getDuration: function(){},
  getCurrentPosition: function(){},
  startRecord: function(){},
  stopRecord: function(){},
  release: function(){},
  setVolume: function(){},
  onStatus: function(){}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM