cfjedimaster / Cordova-Examples

A collection of Cordova/Ionic/etc demos.
982 stars 1.07k forks source link

local mp3 file doesn't play in demo #20

Closed santekotturi closed 8 years ago

santekotturi commented 8 years ago

I've been trying to get the cordova-plugin-media to play a local file. I cannot get it to work in for my project so I've been trying to see if other demo projects work. Came across this mp3 demo and found I couldn't get it work on my phone. Curious if others are seeing this as well, if not, what is their working config.

I can get it working with remote urls but nothing local, neither ios nor android (with the android specific /android_asset/www/ path).

The plugin doesnt throw an error, it logs out: Playing audio sample 'sounds/button-1.mp3' into Xcode. But complete silence on both platforms.

If I use the same local sound file but use https://github.com/katzer/cordova-plugin-local-notifications, the sound file gets played on the device (thus ruling out a corrupt sound file and the device not playing sounds/silenced).

I've tried the ngCordova wrapper for the plugin as well to no avail. Also tried using .wav files for ios. in my ionic project I have the following code:

$scope.vc.playMedia = function() {
            $ionicPlatform.ready(function() {
                console.log('playing media');
                var src = device.platform == 'Android' ? "/android_asset/www/sounds/file1.mp3" : "sounds/file1.wav";
                console.log('from source ' + src)
                // Remote source works
                // src = "http://www.stephaniequinn.com/Music/Commercial%20DEMO%20-%2013.mp3";
                var media = new Media(src, null, null, mediaStatusCallback);
                media.play();

                var mediaStatusCallback = function(status) {
                    if (status == 1) {
                        $ionicLoading.show({ template: 'Loading...' });
                    } else {
                        $ionicLoading.hide();
                    }
                }
                console.log('media: ')
                console.dir(media);
            })

        }

TIA, Sante

cfjedimaster commented 8 years ago

Can you tell me which of my demos you tried? (This repo is for multiple demos.)

santekotturi commented 8 years ago

@cfjedimaster the mp3 demo (the non-ID3 version)

cfjedimaster commented 8 years ago

If you use remote debug, do you see anything in that console?

santekotturi commented 8 years ago

The plugin doesnt throw an error, it logs out: Playing audio sample 'sounds/button-1.mp3' into Xcode. But complete silence on both platforms.

turns out its an issue: https://issues.apache.org/jira/browse/CB-10723

more discussion here: https://forum.ionicframework.com/t/ios-9-2-cordova-plugin-media-no-sound-fix-on-the-way/45100/5

cfjedimaster commented 8 years ago

Not talking about an error per se. To be clear, did you try Chrome Remote Debug or Safari Remote Debug?

On Fri, Apr 15, 2016 at 10:08 AM, Sante Kotturi notifications@github.com wrote:

The plugin doesnt throw an error, it logs out: Playing audio sample 'sounds/button-1.mp3' into Xcode. But complete silence on both platforms.

turns out its an issue: https://issues.apache.org/jira/browse/CB-10723

more discussion here:

https://forum.ionicframework.com/t/ios-9-2-cordova-plugin-media-no-sound-fix-on-the-way/45100/5

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/cfjedimaster/Cordova-Examples/issues/20#issuecomment-210498284

Raymond Camden, Developer Advocate for StrongLoop at IBM

Email : raymondcamden@gmail.com Blog : www.raymondcamden.com Twitter: raymondcamden

santekotturi commented 8 years ago

when running in an ios9.3 emulator, this code:

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
    document.querySelector("#playMp3").addEventListener("touchend", playMP3, false);
};

function playMP3() {
    console.log('play mp3');
    var mp3URL = getMediaURL("sounds/button-1.mp3");
    var media = new Media(mp3URL, null, mediaError);
    media.play();
    console.log('played file')
}

function getMediaURL(s) {
    if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s;
    return s;
}

function mediaError(e) {
    alert('Media Error');
    alert(JSON.stringify(e));
}

generates these logs in Safari Remote Debug:

[Log] play mp3 (console-via-logger.js, line 174)
[Log] played file (console-via-logger.js, line 174)

but no sound is actually played.

if I change the mediaURL to a remote file: var mp3URL = getMediaURL("http://www.stephaniequinn.com/Music/Commercial%20DEMO%20-%2013.mp3");

I get the same safari remote debug logs but this time it plays the file.

santekotturi commented 8 years ago

The workaround in this post fixes the plugin:

However if you need sound ASAP in your apps, just make this small change to the following files and rebuild your app:

[root]/plugins/cordova-plugin-media/src/ios/CDVSound.m, ln 352, 355
[root]/platforms/ios/Sleepy/Plugins/cordova-plugin-media/CDVSound.m, ln 352, 355

From:

 if (audioFile.rate != nil){
    float customRate = [audioFile.rate floatValue];
    NSLog(@"Playing stream with AVPlayer & custom rate");
    [avPlayersetRate:customRate];
} else {
    NSLog(@"Playing stream with AVPlayer & custom rate");
    [avPlayer play];
}

To:

if (audioFile.rate != nil){
    float customRate = [audioFile.rate floatValue];
    NSLog(@"Playing stream with AVPlayer & custom rate");
    [audioFile.player setRate:customRate];
} else {
    NSLog(@"Playing stream with AVPlayer & custom rate");
    [audioFile.player play];
}

Hope this helps anyone out there struggling with sound.

cfjedimaster commented 8 years ago

Just now checking into it - but looks like this is a plugin issue, not a bug in my code (whew ;) - so safe to close, right?

santekotturi commented 8 years ago

Yea, nothing wrong in your code :) , just the underlying plugin :(

santekotturi commented 8 years ago

turns out there's a plugin update pushed a couple days ago that hasnt made its way to npm yet but can be installed using:

cordova plugin add https://github.com/apache/cordova-plugin-media.git#79f6232
cfjedimaster commented 8 years ago

Cool - thanks for the update. :)

On Wed, Apr 20, 2016 at 11:46 AM, Sante Kotturi notifications@github.com wrote:

turns out there's a plugin update pushed a couple days ago that hasnt made its way to npm yet but can be installed using:

cordova plugin add https://github.com/apache/cordova-plugin-media.git#79f6232

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/cfjedimaster/Cordova-Examples/issues/20#issuecomment-212554890

Raymond Camden, Developer Advocate for StrongLoop at IBM

Email : raymondcamden@gmail.com Blog : www.raymondcamden.com Twitter: raymondcamden