1

我在 www 文件夹中有一个 mp3,我需要播放它。该文件存在,我已经解压缩了构建,我可以从 IDE (Monaca) 中看到它。我正在使用 Cordova 6.2 和 cordova-plugin-media 2.3.0,在 HTC ONE M7 上进行测试。

//"use strict";
var app = angular.module('starter.controllers')
//http://www.tutorialspoint.com/ionic/ionic_media.htm 
.controller('PlayerCtrl',  function($scope, $ionicPlatform, $cordovaMedia,$cordovaFile) {

    $ionicPlatform.ready(function() {    

        var src = "/android_asset/www/test.mp3";

        $scope.media = new Media(src, 
            function() {
                console.log("playAudio Success");
            }, 
            function(error) {
                console.log("playAudio Error: " + error.code);
                console.log(JSON.stringify(error));
            });

        $scope.media.play();

        console.log('play '+ JSON.stringify( $scope.media));
        var mediaTimer = setInterval(function () {

            console.log('play '+ JSON.stringify( $scope.media));
            if($scope.media){
                // get media position
                $scope.media.getCurrentPosition(
                    // success callback
                    function (position) {
                        if (position > -1) {
                            console.log('Position '+(position) + " sec");
                        }
                    },
                    // error callback
                    function (e) {
                        console.log("Error getting pos=" + e);
                    }
                );
            }else{
                console.log('no media');
            }
        }, 5000);


        $cordovaFile.checkFile(cordova.file.applicationDirectory, 'www/test.mp3').then(function(result) {
            console.log('file found' ) ;
            console.log('URL: ' + JSON.stringify(result));
            fileUrl = result.nativeURL;
          }, function(err) {
        console.log('file not found') ;
          });

    }); 

});

当我在手机上运行它时,cordovaFile 找不到它并且cordovaMedia 无法播放它,我在下面的日志中收到错误,但文件在 APK 中。

playAudio Error: 1
www/js/PlayerCtrl.js:16 HTC One{"code":1}
www/js/PlayerCtrl.js:50 HTC Onefile not found
www/js/PlayerCtrl.js:24 HTC Oneplay {"id":"1a6fb84c-b28f-f227-1ff2-d2baf5bf0610","src":"/android_asset/www/test.mp3","_duration":-1,"_position":-1}
www/js/PlayerCtrl.js:31 HTC OnePosition -0.001 sec

它就在那里,我不是在暗示!

我没有想法,有什么建议吗?非常感谢!

4

3 回答 3

0

这是我用来在 Android 上获取正确路径的功能,它甚至可以在 iOS 上运行。有了这个,我可以播放 Cordova 媒体插件演示的媒体。

function getPath (){
             var str = location.pathname;
             var i = str.lastIndexOf('/');
             return str.substring(0,i+1);
         }

因此,对于您的示例,它将是:

var src = new Media(getPath() + 'test.mp3');
src.play();
于 2016-09-02T04:51:22.960 回答
0

Apparently, when running an app with the Monaca Ide debugger, the www folder is not reacheable, or at least it's not on /android_assets. I built and installed the app as a normal apk and it worked.

于 2016-08-27T20:16:44.490 回答
0

我在媒体插件 的官方文档中发现了

`NOTE: cdvfile path is supported as src parameter:`
var my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);
于 2016-08-26T16:30:41.527 回答