Cordova 6.2 doesn't play local file. $cordovaFile.checkFile doesn't find it

338 Views Asked by At

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

It is there, I'm not allucinating!

I'm out of ideas, any suggestions? Thanks a lot!

3

There are 3 best solutions below

3
On

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.

0
On

I had found that in official documentation of media plugin

`NOTE: cdvfile path is supported as src parameter:`
var my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);
0
On

This is the function I use to get the proper path on Android and it may even work on iOS. With this, I am able to play media as demonstrated with the Cordova media plugin.

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

So for your example, it would be:

var src = new Media(getPath() + 'test.mp3');
src.play();