简体   繁体   English

科尔多瓦插件媒体不从本地 URI 播放

[英]cordova-plugin-media not playing from local URI

Trying to play audio files from the external storage of phone.尝试播放手机外部存储中的音频文件。

Went through this plugin first finding the URI for a particular mp3 file.通过这个插件首先找到特定 mp3 文件的 URI。 Then passed that to the cordova-plugin-media instance.然后将其传递给cordova-plugin-media实例。

// find music file URI from the sdcard
new ExternalStorageSdcardAccess( fileHandler ).scanPath( "file:///storage/C67A-18F7/Music/" );
function fileHandler( fileEntry ) {
    // console.log( fileEntry.name + " | " + fileEntry.toURL() );
}    

// create a button instance
let mbutton = new Button({
  centerX: 0, top: [fbutton,100],
  text: 'play music'  
}).appendTo(ui.contentView);    

// call the play media function from the button
mbutton.on('select', () => {
    // load and play the music:
    playAudio("file:///storage/C67A-18F7/Music/demo/testSound.mp3")

});    

function playAudio(url) {
    // Play the audio file at url
    var my_media = new Media(url,
        // success callback
        function () {
            console.log("playAudio():Audio Success");
        },
        // error callback
        function (err) {
            console.log("playAudio():Audio Error: " + err.code + err.message);
        },
        // status callback
        function (status) {
            console.log("playAudio():Audio Status: " + status);
        }
    );
    // Play audio
    my_media.play();
    my_media.setVolume('1.0');
}

I checked the adb for log.我检查了adb的日志。 This is what I got specifically hitting those buttons - http://paste.ubuntu.com/25767292/这就是我专门点击这些按钮的结果 - http://paste.ubuntu.com/25767292/

incoming-operation: 'Call' on 'cordova.plugin' (o13) with method 'exec' with properties {action=create, arguments=["d39113d0-b5f5-bf2d-8a84-5afbbc6ae9a0","file:///storage/C67A-18F7/Music/demo/testSound.mp3"], callbackId=Media975330222}
incoming-operation: 'Call' on 'cordova.plugin' (o13) with method 'exec' with properties {action=startPlayingAudio, arguments=["d39113d0-b5f5-bf2d-8a84-5afbbc6ae9a0","file:///storage/C67A-18F7/Music/demo/testSound.mp3",null], callbackId=INVALID}
outgoing-operation: 'Notify' o13 of 'finish' with arguments {message=S01 Media975330222 s, status=1, keepCallback=false, callbackId=Media975330222}
ExtMediaPlayer-JNI: env->IsInstanceOf fails
MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
ExtMediaPlayer-JNI: env->IsInstanceOf fails
MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0

I am using Capacitor with an ionic react project and was able to get cordova-plugin-media playing local files by using it in conjunction with cordova-plugin-file .我正在使用 Capacitor 和一个离子反应项目,并且能够通过将它与cordova-plugin-file结合使用来让cordova-plugin-media播放本地文件 I am the ionic-native wrappers, but I think its the same code here.我是 ionic-native 包装器,但我认为这里的代码相同。

NOTE: iOS requires you to remove 'file://' and Android does not.注意:iOS 要求您删除“file://”,而 Android 不需要。

    const finishedFilePath = `${File.applicationDirectory}public/assets/audio/file.mp3`;
const hybridTransitionAudioFile = Media.create(isPlatform('ios') ? transitionFilePath.replace('file://', '') : transitionFilePath);
hybridTransitionAudioFile.play();

1- install this 1-安装

https://play.google.com/store/apps/details?id=com.adobe.phonegap.app&hl=en https://play.google.com/store/apps/details?id=com.adobe.phonegap.app&hl=zh_CN

2- install this 2-安装

http://docs.phonegap.com/getting-started/1-install-phonegap/desktop/ http://docs.phonegap.com/getting-started/1-install-phonegap/desktop/

3- connect your laptop and your phone on same network 3-在同一网络上连接笔记本电脑和手机

4- follow the getting started on this and run it on your phone , i suppose you wont get the same error , because im my case it dose not work in browser nor emulator in case of phone gap 4-按照此入门指南并在手机上运行它,我想您不会收到相同的错误,因为在我看来,在手机间隙的情况下,它无法在浏览器或仿真器中运行

Reference : https://phonegap.com/getstarted/ 参考https : //phonegap.com/getstarted/

give feed back so i can update my answer here 给反馈,以便我可以在这里更新我的答案

HTML PART for audio src 音频src的HTML PART

        <audio id="successSound" src="/android_asset/www/audio/correct.mp3" type="audio/mpeg"></audio>
        <audio id="errorSound" src="/android_asset/www/audio/error.mp3" type="audio/mpeg" ></audio>
    </body>
</html>

Example of my version that works i hope there is help in it 我的版本可以正常工作的示例 ,希望对您有所帮助

playAudio("errorSound");


var my_media = null;
var mediaTimer = null;

function playAudio(id) {
    var audioElement = document.getElementById(id);
    var src = audioElement.getAttribute('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);
    }
}

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


// onSuccess Callback
//
function onSuccess() {
}

// onError Callback
function onError(error) {
    switch(error.code){
        case MediaError.MEDIA_ERR_ABORTED:
        alert('MEDIA_ERR_ABORTED code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NETWORK:
        alert('MEDIA_ERR_NETWORK code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_DECODE:
        alert('MEDIA_ERR_DECODE code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NONE_SUPPORTED:
        alert('MEDIA_ERR_NONE_SUPPORTED code: '    + error.code);
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 播放 m3u8 文件时cordova-plugin-media 出错 - Error in cordova-plugin-media while playing m3u8 file iOS中的Ionic应用程序无法使用Cordova-Plugin-Media播放本地音频 - Ionic app in iOS unable to play local audio with cordova-plugin-media cordova-plugin-media不会触发成功或失败事件 - cordova-plugin-media not firing success or fail events Cordova 6.5.0无法使用Cordova插件媒体FAILED重命名/storage/emulated/0/tmprecording-1489806941198.3gp录制音频 - Cordova 6.5.0 fails recording audio using cordova-plugin-media FAILED renaming /storage/emulated/0/tmprecording-1489806941198.3gp 在不使用媒体插件的情况下在Android Cordova Project中播放音频 - Playing audio in Android Cordova Project without using the media plugin Cordova媒体插件,如何测试浏览器中播放的声音? 我总是收到ReferenceError:未定义媒体 - Cordova media plugin, how to test sound playing in browser? I always got ReferenceError: Media is not defined 使用Media插件使用Cordova / PhoneGap在android中播放本地音频 - play a local Audio in android with Cordova/PhoneGap using Media plugin cordova媒体插件状态不起作用 - cordova media plugin status is not working 科尔多瓦媒体,如何获取循环播放的回调? - Cordova media, how to get callback for loop playing? 使用Cordova Media插件时出现问题 - Issue in using Cordova Media plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM