Ionic:Media插件 - 不能让它工作 [英] Ionic: Media plugin - can’t get it working

查看:512
本文介绍了Ionic:Media插件 - 不能让它工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经在 ionic 的旧版本中工作,但现在我已经完成更新此应用程序的其余部分,并返回完成这个声音不会播放。

This had been working in an old version of ionic but now I've finished updating the rest of this app and gone back to finalise this the sound won't play.

我有一个开关,用于在应用程序中播放演示声音。这里是代码,与评论和与注释掉的一部分我认为可能会导致我的问题。它不是。

I have an on-off switch for playing a demo sound in the app. Here is the code, with comments and with the commented out part I thought might be causing my problem. It isn't.

.controller( 'SoundCtrl', function( $scope, $timeout ) {

    $scope.sound_on = false;
    var media       = new Media( '100bpm.wav' );

    $scope.soundPlayer = function() {
        console.log( "in soundPlayer" );
        if( $scope.sound_on == false ) {
            $scope.sound_on = true;
            media.setVolume( '1.0' );
            media.play();
            console.log( "sound on" );
            console.log( media );

            /*$timeout(function(){
             $scope.sound_on=false;
             console.log("should change");
             }, 12600);*/

        } else {
            media.stop();
            $scope.sound_on = false;
            console.log( "sound off" );
        }
    }
});

我获得所有正确的控制台日志,我把wav文件放在同一个文件夹脚本。

I get all the right console logs, and I put the wav file in the same folder as my js scripts.

仍然没有。

任何帮助?

推荐答案

https://forum.ionicframework.com/t/how-to-play-local-audio-files/7479/4rel =nofollow>这篇文章在Ionic论坛,你必须添加'/ android_asset / www>

According to this post on Ionic forum you have to add '/android_asset/www/' to the path of your media file on Android device.

因此,您的代码如下所示:

So your code becomes as below:

$scope.media = new Media( '/android_asset/www/'+'100bpm.wav',
        function() {
            console.log("[mediaSuccess]");
        }, function(err) {
            console.log("[mediaError]", err);
        }, function(status) {
            console.log("[mediaStatus]", status);
        });

在我的试用中,我使用控制器$ scope的属性(媒体)来存储媒体对象attach also successHandler,errorHandler,statusHandler

In my trial I used a property (media) of the controller $scope to store media object and I attached also successHandler, errorHandler, statusHandler

注意:注意 Media 在SoundCtrl中使用 new media()实例化它。它发生在SoundCtrl之前创建 onDeviceReady (即当cordova.plugin.media可用),所以我添加了新媒体。) $ scope.soundPlayer()函数。

N.B.: pay attention that Media class (function) is already available when you instantiate it with new media() in SoundCtrl. It happens to me that SoundCtrl be created before onDeviceReady (that is when cordova.plugin.media becomes available), so I added new Media(...) to $scope.soundPlayer() function.

这篇关于Ionic:Media插件 - 不能让它工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆