Ionic/Cordova app plays sound in emulator but not on Android device

自作多情 提交于 2019-12-25 03:16:41

问题


I'm new to Ionic and Cordova, so I'm sure I'm missing something basic, but my problem is a packaged APK does not play sounds on an Android device. I can get the sound to play in the Ripple emulator just fine with the following code:

.controller('MainCtrl', ['$scope', function ($scope) {
        $scope.playStartBell = function () {
            var media = new Media('media/startBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        },
            $scope.playStopBell = function () {
            var media = new Media('media/stopBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        }
    }])

I've used Cordova to install the media plugin: $cordova plugin add org.apache.cordova.media

According to this SO post, a value needs to be added to the config.xml, but I'm not sure how to do it properly for Ionic/Cordova.


回答1:


Turns out that you have specify path starting with the /android_asset/www prefix like so:

/android_asset/www/

So changing my code to the following worked. Note you'll want to detect what device you're running on to determine the appropriate location.

.controller('MainCtrl', ['$scope', function ($scope) {
        ///android_asset/www/
        $scope.playStartBell = function () {
            var media = new Media('/android_asset/www/media/startBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        },
            $scope.playStopBell = function () {
            var media = new Media('/android_asset/www/media/stopBell.mp3', function () {
                console.log('good');
            }, function (err) {
                console.log('bad: ', err);
            });
            media.play();
        }
    }])


来源:https://stackoverflow.com/questions/25924724/ionic-cordova-app-plays-sound-in-emulator-but-not-on-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!