IBM工作灯6.1 - 无法播放使用科尔多瓦本地媒体文件科尔、无法播放、媒体文件、多瓦

2023-09-04 03:14:03 作者:至屎方休

在测试Android模拟器。

我想用科尔多瓦媒体API来管理音频的工作灯项目内存储在本地播放。起初,我试图更新我的项目来完成这个任务,但没有成功,所以我决定使用记录了Apache的科尔多瓦媒体API页面上的完整的例子。我用全样本播放,暂停和停止音频文件。我所做的唯一变化是发挥本地存储在我的工作灯项目中的音频文件。

然而,上述的失败...

我创建了一个新的文件夹,WavAudo,在包含.MP3示例项目,和.wav版本的音频播放。

当我启动应用程序,我收到警报弹出的错误。code 返回Error.message 未定义。使用Chrome的调试器我看到了,关于 deviceready 我得到未捕获的Ref​​erenceError媒体没有定义

我beleive我已经尝试了所有可能的路径+文件组合,找到该文件,但我一定是错过了一些东西。

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 // EN
              http://www.w3.org/TR/html4/strict.dtd">
< HTML>
  < HEAD>
    <冠军>媒体例< /标题>

    <脚本类型=文/ JavaScript的字符集=utf-8SRC =cordova.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的字符集=utf-8>

    //等待设备的API库加载
    //
    document.addEventListener(deviceready,onDeviceReady,假);

    //设备API可用
    //
    传播onDeviceReady(){
        playAudio(/ android_asset / WWW / WavAudio / 16300.mp3);
    }

    //音频播放
    //
    VAR my_media = NULL;
    VAR mediaTimer = NULL;

    //播放音频
    //
    功能playAudio(SRC){
        //创建一个从SRC媒体对象
        my_media =新媒体(SRC,的onSuccess,onError的);

        //播放音频
        my_media.play();

        每一秒//更新my_media位置
        如果(mediaTimer == NULL){
            mediaTimer =的setInterval(函数(){
                //获取my_media位置
                my_media.getCurrentPosition(
                    //成功回调
                    功能(位置){
                        如果(位置> -1){
                            setAudioPosition((位置)+秒);
                        }
                    },
                    //错误回调
                    功能(E){
                        的console.log(错误获取POS =+ E);
                        setAudioPosition(错误:+ E);
                    }
                );
            },1000);
        }
    }

    //暂停音频
    //
    传播pauseAudio(){
        如果(my_media){
            my_media.pause();
        }
    }

    //停止音频
    //
    功能stopAudio(){
        如果(my_media){
            my_media.stop();
        }
        clearInterval(mediaTimer);
        mediaTimer = NULL;
    }

    //的onSuccess回调
    //
    功能的onSuccess(){
        的console.log(playAudio():音频成功);
    }

    // onerror的回调
    //
    功能onError的(错误){
        警报('code:'+错误code +'\ N'+
              信息:+ +返回Error.message'\ N');
    }

    //设置音频位置
    //
    功能setAudioPosition(位置){
        。的document.getElementById('audio_position)的innerHTML =位置;
    }

    < / SCRIPT>
  < /头>
  <身体GT;
    < A HREF =#级=BTN大的onclick =playAudio
    (/android_asset/www/WavAudio/16300.mp3');">Play音频< / A>
    < A HREF =#级=BTN大的onclick =pauseAudio();>暂停播放音频< / A>
    < A HREF =#级=BTN大的onclick =stopAudio();>停止播放音频< / A>
    &其中,P的id =audio_position>&所述; / P>
  < /身体GT;
 

我还添加这些到Android清单:

 <使用-权限的Andr​​oid:名称=android.permission.RECORD_AUDIO/>
 <使用-权限的Andr​​oid:名称=android.permission.MODIFY_AUDIO_SETTINGS/>
 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 
拥有正版4K资源 卓典4K高清播放机评测

解决方案

在科尔多瓦的网站作品所提供的样品code,但它看起来像他们指向坏的网址。我与另一取代它,然后它的工作。

至于播放本地媒体文件时,需要提供完整路径媒体文件的位置的它会在生成的Andr​​oid项目的。例如,如果你创建一个通用\音频文件夹中的工作灯的应用,并在其中放置一个.MP3文件,则需要使用以下路径(在HTML或JavaScript或者,但是你会喜欢的......):

 < A HREF =#的onclick =playAudio('/ android_asset / WWW /默认/音频/ myMediaFile.mp3');>播放音频< / A>
 

这是因为在工作灯,每个应用程序被认为是皮肤。有默认皮肤和如果添加另一个,它都会有自己的 WWW 文件夹中的文件夹。

示例项目:的Andr​​oid科尔多瓦媒体API

PS,我不觉得这是一个很好的做法,复制粘贴的一个例子是,从科尔多瓦到工作灯项目。

在没有cordova.js链接到。 这是多余的,听取 deviceready ,因为这是内部处理。把这样说:如果你已经达到了 wlCommonInit(),你是好去 在无需增加额外的权限,除非你想要的这些权限提供...

Tested in Android Emulator.

I am trying to use Cordova Media API to manage the playing of audio stored locally within the Worklight project. Initially I attempted to update my project to accomplish this task, but was not successful, so I decide to use the complete example documented on the Apache Cordova Media API page. I used the full sample to play, pause, and stop an audio file. The only change I made was to play the audio file stored locally in my worklight project.

However, the above fails...

I created a new folder, "WavAudo", in the sample project that contains .mp3, and .wav versions of the audio to play.

When I start the application I receive an alert popup that the error.code, and error.message are undefined. Using Chrome debugger I see the that on deviceready I get Uncaught ReferenceError Media is not defined.

I beleive I've tried all possible path+file combinations to locate the file, but I must have missed something.

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Media Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        playAudio("/android_asset/www/WavAudio/16300.mp3");
    }

    // Audio player
    //
    var my_media = null;
    var mediaTimer = null;

    // Play audio
    //
    function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, onSuccess, onError);

        // Play audio
        my_media.play();

        // Update my_media position every second
        if (mediaTimer == null) {
            mediaTimer = setInterval(function() {
                // get my_media position
                my_media.getCurrentPosition(
                    // success callback
                    function(position) {
                        if (position > -1) {
                            setAudioPosition((position) + " sec");
                        }
                    },
                    // error callback
                    function(e) {
                        console.log("Error getting pos=" + e);
                        setAudioPosition("Error: " + e);
                    }
                );
            }, 1000);
        }
    }

    // Pause audio
    //
    function pauseAudio() {
        if (my_media) {
            my_media.pause();
        }
    }

    // Stop audio
    //
    function stopAudio() {
        if (my_media) {
            my_media.stop();
        }
        clearInterval(mediaTimer);
        mediaTimer = null;
    }

    // onSuccess Callback
    //
    function onSuccess() {
        console.log("playAudio():Audio Success");
    }

    // onError Callback
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // Set audio position
    //
    function setAudioPosition(position) {
        document.getElementById('audio_position').innerHTML = position;
    }

    </script>
  </head>
  <body>
    <a href="#" class="btn large" onclick="playAudio
    ('/android_asset/www/WavAudio/16300.mp3');">Play Audio</a>
    <a href="#" class="btn large" onclick="pauseAudio();">Pause Playing Audio</a>
    <a href="#" class="btn large" onclick="stopAudio();">Stop Playing Audio</a>
    <p id="audio_position"></p>
  </body>

I also added these to Android Manifest:

 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   

解决方案

The sample code provided at Cordova's website works, however it looks like the URL they point to is broken. I've replaced it with another and then it worked.

As for playing a local media file, you need to provide the full path to the media file's location where it'll be in the generated Android project. For example, if you create a common\audio folder in the Worklight application and place a .mp3 file in it, you need to use the following path (in the HTML or JavaScript, or however you'd like...):

<a href="#" onclick="playAudio('/android_asset/www/default/audio/myMediaFile.mp3');">Play Audio</a>

This is because in Worklight, every application is considered a "skin". There is the "default" skin, and if you add another, it will have its own folder in the www folder.

Sample project: Android Cordova Media API

P.S., I do not find it a good practice to copy-paste an example as-is from Cordova to a Worklight project.

There is no cordova.js to link to. It is redundant to listen to deviceready as this is handled internally. Putting it this way: if you've reached wlCommonInit(), you're good to go. There is no need to add extra permissions, unless you want what those permissions offer...