Randy Arrowood
Randy Arrowood

Reputation: 1

Difference in Cordova and HTML5 when playing sound

I'm getting different results when trying to play a sound in HTML5 vs. Cordova on an iOS device/emulator. It's working fine and I always have the sound object when running HTML5 in a mobile browser. The sound object is not defined (so, I can't play it) in the iOS emulator.

    SC.initialize({
        client_id: 'MYIDGOESHERE'
    });

    var soundCloudUrl = '';

    //Grab the artist detail and then grab the soundcloud details
    $http.post(BASE_URL + 'artistdetail', {band_id: band_id}).
        success(function (data, status, headers, config) {

            $scope.artistDetails = data;
            soundCloudUrl = "https://soundcloud.com/en-flux/jomox-rode-drop";
        }).
        error(function (data, status, headers, config) {
            $scope.showAlert("Oops", "Try again later.");
        }).then(function () {
            $http.get('http://api.soundcloud.com/resolve.json?url=' + soundCloudUrl + '&client_id=MYCLIENTID').
                success(function (data) {
                    console.log(data);
                    $scope.soundcloudData = data;
                    SC.stream(data.uri, function (sound) {
                        console.log(sound); //THIS IS THE LOG THAT DIFFERS
                        $scope.playTrack = function () {
                            sound.play();
                            $scope.playing = true;
                        }
                        $scope.pauseTrack = function () {
                            sound.pause();
                            $scope.playing = false;
                        }
                        $scope.leaveDetail = function (state) {
                            if (state == 'stop') {
                                sound.stop();
                            } else {
                                sound.pause();
                            }
                            $scope.playing = false;
                        }

                    });
                }).
                error(function (data, status, headers, config) {
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                });

        });

So, in iOS, I get false for the console.log of sound, but in HTML5 straight in a browser, all is fine.... Any thoughts?

Upvotes: 0

Views: 299

Answers (2)

user5250554
user5250554

Reputation:

Your promise chain is pretty long. Maybe just break it apart and process everything step by step. You probably did already but I know as my chains/callback hell starts grow, stuff breaks and I have to go back and trace.

Setup and initialization:

// Source to the Soundcloud channels I'm requesting
$scope.scdata = soundCloud.soundCloudData;

// Initialize the Soundcloud API onload of the controller
soundCloud.initialize();

// Currently not being used will be (didn't work when I declared it in         the promise)
$scope.player = null; 

User interaction to get the SC specific info:

// Click in the element to get it's object and grab the SC id
$scope.load = function(){

  // Create the request url (/user + '-userID'/, /channels... )
  var url = '/tracks/' + this.m.id;

  // GET request to Soundcloud
  SC.stream(url).then(function(player){
    $scope.show();
    $scope.player = player;
  });
};

Then I created functions for my player:

$scope.player.pause();
  $scope.player.play();
};

// Click play to load the track
$scope.play = function () {
  $scope.player.play();
};

// Click play to close the track
$scope.stop = function () {
  $scope.player.pause();
};

After I got this working, I refactored for error, success messages...

Upvotes: 1

ULLAS MOHAN.V
ULLAS MOHAN.V

Reputation: 1531

Please add media plugin ion your application.

org.apache.cordova.media This plugin provides the ability to record and play back audio files on a device.

**Supported Platforms**

Android
BlackBerry 10
iOS
Windows Phone 7 and 8
Tizen
Windows

Upvotes: 0

Related Questions