1

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!

MazarD
  • 2,759
  • 2
  • 22
  • 33
rrabio
  • 143
  • 2
  • 6

3 Answers3

0

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', ...);
Hassan ALi
  • 1,313
  • 1
  • 23
  • 51
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.

rrabio
  • 143
  • 2
  • 6
  • If you are meaning the preview component of Monaca Cloud IDE, then of course the assets aren't available when you are relying on Cordova plugins. This is due to it not being an Android device and thus the plugins are not loaded, therefore functions such as Media are not available. – Munsterlander Aug 31 '16 at 06:22
  • No, I mean the Monaca IDE debugger that you install on the Android device. You can use Media, but if you open the assets/www folder, you find the www folder of the debugger application, not of the application you are running. – rrabio Sep 01 '16 at 11:05
  • Ok, I posted an answer of working code then to solve your issue. Let me know if it doesn't work, but it is what I use for my apps and I have not had any issues. – Munsterlander Sep 02 '16 at 04:47
0

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();
Munsterlander
  • 1,356
  • 1
  • 16
  • 29