【问题标题】:phonegap 3.5 play local audio filesphonegap 3.5 播放本地音频文件
【发布时间】:2014-11-15 23:05:13
【问题描述】:

我的 phonegap 3.5 应用程序带有 angularjs,并且我的 song.mp3 和 index.html 文件在我的根目录中 该应用程序在我的 android 设备和 phonegap 应用程序中运行良好,但我无法播放 song.mp3 文件 我使用科尔多瓦媒体插件,它总是给我代码 1 错误 我也尝试了 ngCordova 插件相同的代码错误。 我的代码是

 var src ="song.mp3";
 // I tried also 
//  var src="/android_asset/www/song.mp3"; 
 // and src ="file:///song.mp3";

var media = new Media(src,function(){

   console.log("playing");
 ,function(err){

 console.log(err.code);
 });
media.play();

【问题讨论】:

标签: javascript android angularjs cordova phonegap-plugins


【解决方案1】:

代码 = 1 表示“中止”(MediaError.MEDIA_ERR_ABORTED = 1)请参阅https://github.com/apache/cordova-plugin-media/blob/master/doc/index.md#constants-1

所以这通常意味着路径有问题。 song.mp3 和 index.html 是否在同一个文件夹中?也许这有帮助:Playing local sound in phonegap

只有当你想从本地文件系统玩游戏时,你才需要file://...

【讨论】:

    【解决方案2】:

    它对我有用,我使用了带有以下脚本的 cordova 媒体插件

    cordova 插件添加 org.apache.cordova.media

    function getCordovaPath() {
      var absolutePath = window.location.pathname;
      //14 is length of html file name(including .html) 
      actualPath = absolutePath.substr( path, path.length - 14 );
      return 'file://' + actualPath;
    }
    
    function playAudio() {
      //var audioElement = document.getElementById(id);
      //var url = audioElement.getAttribute('src');
      var my_media = new Media(getCordovaPath() + 'beep.mp3',
      // success callback
      function () { console.log("playAudio():Audio Success"); },
      // error callback
      function (err) { console.log("playAudio():Audio Error: " + err); });
      // Play audio
      my_media.play();
    }
    <body onload="playAudio()">

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多