我在 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
我没有想法,有什么建议吗?非常感谢!