I have an mp3 in the www folder and I need to play it. The file exists, I've unzipped the build and I can see it from the IDE (Monaca). I'm using Cordova 6.2 and cordova-plugin-media 2.3.0, testing on an 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') ;
});
});
});
When I run it on my phone cordovaFile can't find it and cordovaMedia can't play it, I get the errors in the log below, but the file is in the 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
I'm out of ideas, any suggestions? Thanks a lot!
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.