0
$(document).on('click', '#bar img', function () { 



    alphaLetter = $(this).data('club-id');
    audio_file_path = '/android_asset/www/audio/'+alphaLetter+'.mp3';


    var my_media = null;
     my_media = new Media(audio_file_path);

    my_media.play();




    });

以上是我播放声音的代码。它播放声音,但是当我重新启动我的项目时,它从那时起就停止了工作,并给出了这些错误;

 E/MediaPlayer(2306): error (-19, 0)
 E/MediaPlayer(2306): Attempt to call getDuration without a valid mediaplayer
 E/MediaPlayer(2306): error (-38, 0)
 E/MediaPlayer(2306): Error (-19,0)
4

2 回答 2

1

这可能是由于您同时运行的媒体对象过多。声音结束后,您应该释放它。

PhoneGap 文档说明了有关发布的内容:

函数 media.release 是一个同步函数,用于释放底层操作系统的音频资源。此功能对于 Android 尤其重要,因为用于媒体播放的 OpenCore 实例数量有限。开发人员在不再需要媒体资源时应调用“发布”函数。

我也遇到了同样的错误,并且在完成声音后使用重置对我来说效果很好。不过记得在声音完成后调用它,例如:

var my_media = null;
my_media = new Media(audio_file_path, function()
{
   this.release();
});

my_media.play();

如果您有兴趣,我已经在这里写了更多关于这个问题的信息,但是上述解决方案应该可以很好地工作。

于 2013-10-27T19:26:24.827 回答
0

It seems as if this error pops up whenever you try to play the sound even though it is not fully loaded at the certain moment (Attempt to call getDuration).

I looked up the Phonegap documentation and couldn't find a method that may check for the availability of the sound. What you can do is:

  • Build a Timer that always waits for some time until the sound shall be played.

  • Use var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]); for some further handling when playing the sound fails.

于 2013-07-15T17:38:30.397 回答