Monday, November 26, 2012

  • Buffer

                    This is SoundCloud Tutorial Series. Until now we have learned Getting Started, Authentication and Uploading Audio. Now we will learn how to play the sounds that we have uploaded.
                   There are different ways to play sounds from SoundCloud. We can create playlist and play them or we can access individual tracks with there Id,So on.....
SoundCloud Tutorials:Playing Audio Files


                  But I think its better to continue with the previous lessons code and develop our version based on the previous version.
                  There are different sound players available to play our sounds. oEmbed ,HTMl5 and even we can create our own player. But we will use SoundCloud's oEmbed player in this lesson.
                  oEmbed is the default player that comes with SoundCloud-PHP-SDK. It is very easy to create player with oEmbed. We can create oEmbed player with just 2 lines of code, it is that easy.
                 As I said earlier we will develop our version based on previous version Sound Cloud Tutorials:Uploading Audio Files(AJAX). Just go there and see what we have done until now.
                 The previous version and the current version are almost same only change we are going to do is in ajaxupload.php. Just see the code below.
ajaxupload.php
<?php
if($_POST){

include("config.php");
$soundcloud->setAccessToken($_POST['access_token']);
$mytrack = array(
    'track[title]' => $_POST["audioname"],
    'track[asset_data]' => '@'.$_FILES["audiofile"]["tmp_name"] 
     );
$track = json_decode($soundcloud->post('tracks', $mytrack));
$soundcloud->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
$track_url =(string) $track->permalink_url;
//Sleep function very important here other wise you will get 404 error.
sleep(3);
$embed_html = json_decode($soundcloud->get('oembed', array('url' => $track_url)));
echo $embed_html->html;
}
?>
In the previous version after we got the track URL we simply displaying the track URL using
$track->permalink_url; code.
But now we using the same url to get the oEmbed player code and simply displaying it on page.

Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 404.
                      Actually the code is simple but this error wasted around 3 hours of my time. I searched on net to solve the error, but no luck. Finally I thought in this way that 404 means page not found or URL not found. That means when we uploading our sound to SoundCloud it will take some seconds of time to create the resource URL.
                      So I add the php sleep function in the code just before calling for oEmbed player. This function will stop the execution of the script number of seconds we want and continue execute after that interval. In above code we put 3 so the script will stop execute for 3 seconds, Mean while SoundCloud will create resource.
                     Ya it worked like a magic and I am correct.
                     So remember this trick very carefully may be it would helpful in some other projects.

CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode error
                        Unfortunately, I could't provide demo yet because I am using free hosting for demos. My hosting provider is in safe mode. So when i run the script i am getting this error CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode. I think there is some work around this problem. I am working on it and i will come up with solution soon. Anyway the script works perfect in local host. Just download the code and check it in your localhost(xampp).
That's It guys. We covered the basic things in SoundCloud SDK. If you need any help I am always there to help you.

0 comments :

Post a Comment