3

我的问题:

我使用 Cordova 的媒体类。MP3 文件只播放一次(第一次)。

代码:

将此代码添加到 Cordova Starter 项目以重现我的问题:

    var playCounter = 0;
    function playMP3(){
      console.log("playMP3() counter " + playCounter);
      var my_media = new Media("app/www/test.mp3");//ressource buildAction == content
      my_media.play();
      playCounter++;
    }
    [...]
    <p onclick="playMP3();">Click to Play MP3</p>

VS输出:

[...]
GapBrowser_Navigated :: /app/www/index.html
'UI Task' (Managed): Loaded 'System.ServiceModel.Web.dll'
'UI Task' (Managed): Loaded 'System.ServiceModel.dll'
Log:"onDeviceReady. You should see this message in Visual Studio's output window."
'UI Task' (Managed): Loaded 'Microsoft.Xna.Framework.dll'
Log:"playMP3() counter 0"
'UI Task' (Managed): Loaded 'System.SR.dll'
Log:"media on status :: {\"id\": \"fa123123-bc55-a266-f447-8881bd32e2aa\", \"msg\": 1, \"value\": 1}"
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
Log:"media on status :: {\"id\": \"fa123123-bc55-a266-f447-8881bd32e2aa\", \"msg\": 1, \"value\": 2}"
Log:"media on status :: {\"id\": \"fa123123-bc55-a266-f447-8881bd32e2aa\", \"msg\": 2, \"value\": 2.141}"
Log:"media on status :: {\"id\": \"fa123123-bc55-a266-f447-8881bd32e2aa\", \"msg\": 1, \"value\": 4}"
Log:"playMP3() counter 1"
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.dll
Log:"media on status :: {\"id\": \"2de3388c-bbb6-d896-9e27-660f1402bc2a\", \"msg\": 9, \"value\": 5}"

我的配置:

  • 科尔多瓦-1.6.1.js
  • 卢米亚 800
  • WP 7.5 (7.10.7740.16)

解决方法(种类):

  • 停用应用程序(关闭屏幕)
  • 重新激活应用程序(打开屏幕)
  • -> 你再拍一张。

欢迎任何帮助,因为我从五月开始就被阻止了,而且我在任何地方都找不到有用的信息。

另外,你能告诉我这段代码是否适用于你的配置吗?

.

.

.

更新:添加演示代码,Cordova 1.8.0rc1

使用全局变量。保持实例存活。

结果

  • test2.mp3 播放完毕,可以正常回放。
  • test.mp3 根本没有播放。
  • 这是您播放的第一个文件。

代码

  function onDeviceReady()
  {
  document.getElementById("welcomeMsg").innerHTML += "Cordova is ready! version=" + window.device.cordova;
  console.log("onDeviceReady. You should see this message in Visual Studio's output window.");
  my_media = new Media("app/www/test.mp3");//ressource buildAction == content
  my_media2 = new Media("app/www/test2.mp3");//ressource buildAction == content
  }

  var playCounter = 0;
  var my_media = null;
  function playMP3(){
    console.log("playMP3() counter " + playCounter);
    my_media.play();
    playCounter++;
  }

  var my_media2 = null;
  function playMP32(){
    console.log("playMP32() counter " + playCounter);
    my_media2.play();
    playCounter++;
  }

</script>
[...]

<p onclick="playMP3();">Click to Play MP3</p>
<p onclick="playMP32();">Click to Play MP3 2</p>

VS输出:

Log:"onDeviceReady. You should see this message in Visual Studio's output window."
INFO: startPlayingAudio could not find mediaPlayer for 71888b14-86fe-4769-95c9-a9bb05d5555b
Log:"playMP32() counter 0"
INFO: startPlayingAudio could not find mediaPlayer for 71888b14-86fe-4769-95c9-a9bb05d5555b
Log:"playMP32() counter 1"
Log:"playMP3() counter 2"
INFO: startPlayingAudio could not find mediaPlayer for b60fa266-d105-a295-a5be-fa2c6b824bc1
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll
Error: El parámetro es incorrecto. 
Log:"playMP32() counter 3"
INFO: startPlayingAudio could not find mediaPlayer for 71888b14-86fe-4769-95c9-a9bb05d5555b

.

.

.

更新:科尔多瓦 2.0.0

我用 2.0.0 的测试用例更新了 Apache 错误报告。

任何人都可以重现这个吗?

错误报告链接: https ://issues.apache.org/jira/browse/CB-941

4

5 回答 5

2

我看到您在第二次单击时收到了 IsolatedStorageException,这让我认为该文件被第一个Media实例保持打开状态,这会阻止它再次被访问。

我建议确保您要么只为Media每个文件创建一个实例,要么查看是否有办法首先处理旧实例。

这是我会尝试的(但我没有测试过):

 var playCounter = 0;

 // moved my_media here so it won't be recreated
 var my_media = new Media("app/www/test.mp3");//ressource buildAction == content

    function playMP3(){
      console.log("playMP3() counter " + playCounter);
      my_media.play();
      playCounter++;
    }
    [...]
    <p onclick="playMP3();">Click to Play MP3</p>
于 2012-06-19T17:15:15.047 回答
1

差点忘了……这对我有用。希望能帮助到你 ;-)

WP7.5/科尔多瓦 1.9.0

要求:据我所知,仅适用于 .wav 文件。我不得不转换我的 .mp3 文件,因为它们会抛出丑陋的异常。

插件:C#:

namespace Cordova.Extension.Commands
{
    public class PGSoundFX : BaseCommand
    {
        public Dictionary <string, SoundEffect> fxSoundMap = new Dictionary<string,SoundEffect>();

    public enum FXType
    {
        Load=0,
        Play=1
    }

    [DataContract]
    public class FXOption
    {
        [DataMember]
        public string audioSrc;
        [DataMember]
        public string audioRef;
        [DataMember]
        public FXType fxType; 
    }

    public void groAudio(string options)
    {
        FrameworkDispatcher.Update(); 
        FXOption opts;

        opts = WP7CordovaClassLib.Cordova.JSON.JsonHelper.Deserialize<FXOption>(options);

        if (opts != null)
        {
            try
            {
                switch (opts.fxType)
                {
                    case FXType.Load:
                        loadSound(opts.audioSrc, opts.audioRef);
                        break;
                    case FXType.Play:
                        Dictionary<string, SoundEffect>.KeyCollection keyCol = fxSoundMap.Keys;
                        foreach (string tmp in keyCol)
                        {
                            if ((opts.audioRef).Equals(tmp))
                                playSound(fxSoundMap[tmp]);
                        }
                        break;
                }
            }
            catch
            {
                Debug.WriteLine("PGSoundFX: loadSound Error ... null parameter(s).");
            }
        }
    }

    /// <summary> 
    /// Loads a wav file into an XNA Framework SoundEffect. 
    /// </summary> 
    /// <param name="Sound">The SoundEffect to play.</param> 
    private void playSound(SoundEffect Sound)
    {
        Sound.Play();
        DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
    }

    /// <summary> 
    /// Loads a wav file into an XNA Framework SoundEffect. 
    /// </summary> 
    /// <param name="SoundFilePath">Relative path to the wav file.</param> 
    /// <param name="Sound">The SoundEffect to load the audio into.</param> 
    private void loadSound(String SoundFilePath, String SoundName)
    {
        if (fxSoundMap.Keys != null)
        {
            Dictionary<string, SoundEffect>.KeyCollection keyColl = fxSoundMap.Keys;
            foreach (string tmp in keyColl)
            {
                if (SoundName.Equals(tmp))
                    return;
            }
        }

        SoundEffect Sound = null;

        try
        {
            // Holds informations about a file stream. 
            StreamResourceInfo SoundFileInfo = Application.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));
            // Create the SoundEffect from the Stream 
            Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
            fxSoundMap[SoundName] = Sound;
            DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
        }
        catch (NullReferenceException)
        {
            Debug.WriteLine("Couldn't load sound " + SoundFilePath);
        }
        } 
    }
}

Javascript:插件:

PhoneGap.addConstructor(function() {

navigator.plugins.pgSoundFX = 
{
    playSound:function(fxName)
    {
    var options = {"audioRef":fxName,"fxType":1};
    PhoneGap.exec(null, null, "PGSoundFX", "groAudio", options);
    },

    loadSound:function(fxSrc, fxName)
    {
    var options = {"audioSrc":fxSrc,"audioRef":fxName,"fxType":0};
    PhoneGap.exec(null, null, "PGSoundFX", "groAudio", options);
    }
}
});

Javascript:使用:

//NOTE: Allows you to pre-load sound effect files

//initialize pgSoundFX plugin
if (!navigator.plugins) {
    console.log("plugIns do not exist!");
    navigator.plugins = {};
}

navigator.plugins.pgSoundFX.loadSound("www/audio/file1.wav", "item1");
navigator.plugins.pgSoundFX.loadSound("www/audio/file2.wav", "item2");
navigator.plugins.pgSoundFX.loadSound("www/audio/file3.wav", "item3");
    :
    :

//NOTE: Play whenever needed
navigator.plugins.pgSoundFX.playSound("item1");

当然,您的文件位置(www/audio/)、名称(file1 等)和参考名称(item1 等)都是您想要的。与大多数人一样,有很多方法可以改进此代码……请随意。我的用例预加载了 5 个 .wav 文件,大小从 28k 到 364k 不等,并在整个用户游戏过程中播放。

于 2012-07-06T21:02:01.507 回答
0

我不确定这是不是正确的答案,因为我没有尝试过,但将其视为头脑风暴。

首先使用ctor创建您的音频,var my_media = new Media(src, onSuccess, onError);成功时只需播放它,错误时首先尝试使用media.release代码发布它。

好吧,如果您知道要发布的其他事件或动作,那将更合适。

编辑:我刚刚注意到它,您没有使用stop媒体,您也可以在需要时尝试使用它,例如,playMP3()如果媒体不为空,请先尝试停止它。

干杯。

于 2012-06-20T08:38:39.773 回答
0

我认为问题可能在于 Silverlight 每页只允许一个媒体实例的限制......并且由于 PG/Cordova 本质上是一个单页 webBrowser 实例,它只能有一个媒体实例。我正在尝试构建一个 XNA SoundEffect 插件来解决这个问题。

于 2012-06-22T17:51:07.423 回答
0

是一个 Cordova 错误 https://issues.apache.org/jira/browse/CB-941

purpulecabbage 在他的 gitHub 仓库中解决了这个问题。请参阅对错误报告的评论。

于 2012-09-28T17:11:23.730 回答