问题
I am using cordova-media-plugin 1.0.1. in an Ionic mobile app. I am using the plugin to play the audio file.
I am able to get it to play using:
var media = $cordovaMedia.newMedia(fileToPlay,
// success callback
mediaSuccess,
// error callback
mediaFailure,
// status callback
mediaStatus
);
I can then call media.play() at it plays the file.
However, the callbacks never seem to execute. I have them defined as:
function mediaSuccess () {
console.log("Successfully finished task.");
}
function mediaFailure (err) {
console.log("An error occurred: " + err.code);
}
function mediaStatus (status) {
console.log("A status change occurred: " + status.code);
}
But they are never called. However, it my console, I am seeing logging from the player itself as it starts and stops playing:
Will attempt to use file resource '//var/mobile/Containers/Data/Application/931BFA01-CDA4-43CD-BC16-7FB6A64305DC/Library/NoCloud/DateTime-1446772191539audio_007.wav'
Playing audio sample '//var/mobile/Containers/Data/Application/931BFA01-CDA4-43CD-BC16-7FB6A64305DC/Library/NoCloud/DateTime-1446772191539audio_007.wav'
Stopped playing audio sample '//var/mobile/Containers/Data/Application/931BFA01-CDA4-43CD-BC16-7FB6A64305DC/Library/NoCloud/DateTime-1446772191539audio_007.wav'
These logging events are going to the console, but they are NOT in my code so they must be coming from the media object.
I need to get the status change and/or success call backs as I need to update the model to enable the play but again when the clip finishes play.
Any thoughts?
回答1:
After digging into this deeper, I have found that (despite the documentation) callbacks are not implemented for iOS in version 1.0.1 of the cordova-media-plugin. They are implemented for Android, FireOS, and Windows ONLY.
At the bottom of the Media.js file in the module's www directory, it creates a cordova channel which lets it subscribe to messages from the native player. Unfortunately, that code is only executed for android, fireos, and windows phone:
if (cordova.platformId === 'android' || cordova.platformId === 'amazon-fireos' || cordova.platformId === 'windowsphone') {
var channel = require('cordova/channel');
channel.createSticky('onMediaPluginReady');
channel.waitForInitialization('onMediaPluginReady');
channel.onCordovaReady.subscribe(function() {
exec(onMessageFromNative, undefined, 'Media', 'messageChannel', []);
channel.initializationComplete('onMediaPluginReady');
});
}
I tried adding ios to the list to see if it was just a miss in that code and it blew up:
ERROR: Method 'messageChannel:' not defined in Plugin 'Media'
So... those of us building for iOS or other non-Android/Windows platforms are SOL when it comes to callbacks (though playing still works). There seems to be no way to submit a bug report to Apache about this unless it is an email coming from an apache.org email address, so I am not sure they even know about it.
来源:https://stackoverflow.com/questions/33558012/cordova-plugin-media-callback-methods