Monday, 31 August 2015

How To: Create a Playlist for HTML5 Audio

Adding an audio element to your page

The very first step is to add the audio element to your page. You can do this by declaring an <audio> tag in your markup, by instantiating a new audio element in the JavaScript code, or by embedding the audio stream in the page:
<audio src="audio/sample.mp3" autoplay>
</audio>

var audio = document.createElement("audio");
if (audio != null && audio.canPlayType && audio.canPlayType("audio/mpeg"))
{
audio.src = "audio/sample.mp3";
audio.play();
}

<audio src="data:audio/mpeg,ID3%02%..." autoplay>
</audio>

Loops

Another frequent request for scenarios with audio is the ability to loop a sound clip. With the HTML5 <audio>, you can do this using the “loop” property; this setting will loop your clip forever, or until the user or the application activates thepause() audio control.
<audio src="audio/sample.mp3" autoplay loop>
</audio>

Another approach to loop an audio file is to programmatically call the play() method when the audio clip ends; doing so will allow you eventually to manage the delay between one loop and the other.
var audio = document.createElement("audio");
audio.src = "piano/3C.mp3";
audio.addEventListener('ended', function () {
// Wait 500 milliseconds before next loop
setTimeout(function () { audio.play(); }, 500);
}, false);
audio.play();

Note that any play() call executed on the audio element before the sound actually ended won’t have any effect. If you are interested to “cancel and restart” the current sound, you will need to reset the currentTime.
var audio = null;
audio = document.createElement("audio");
audio.src = "piano/3C.mp3";
audio.addEventListener('ended', function () {
audio.play();
}, false);
function play() {
audio.play();
}
function restart() {
audio.currentTime = 0;
audio.play();
}

Multiple audio tags

If your scenario needs the same audio file to be played several times concurrently (that is, with overlapping sounds), you can achieve this result by creating multiple audio tags pointing to the same file. Obviously the same approach also works if you are using different audio files at the same time. As we explained earlier in this post, you can either add those programmatically or by instantiating them in the markup.
The following code snippet shows how to load and play multiple audio files using markup. The audio samples all have the same length; at the end of the execution, they will loop starting from the beginning. As you play them in Internet Explorer 9, you can notice that they are automatically synchronized throughout various loops. You will notice that the combination of these 5 sounds will play like the audio file used in the previous demo (“sample.mp3”).
<body>
<audio src="audio/Bass.mp3" autoplay loop>
</audio>
<audio src="audio/Drum.mp3" autoplay loop>
</audio>
<audio src="audio/Crunch.mp3" autoplay loop>
</audio>
<audio src="audio/Guitar.mp3" autoplay loop>
</audio>
<audio src="audio/Pizzicato.mp3" autoplay loop>
</audio>
</body>

While this approach is very simple and straightforward, in most scenarios developers prefer to create the audio clips programmatically. The following code snippet shows how to add 3 audio clips dynamically using code. As you play them together, you will get the C Major chord!
AddNote("3C");
AddNote("3E");
AddNote("3G");
function AddNote(name) {
var audio = document.createElement("audio");
audio.src = "piano/" + name + ".mp3";
audio.autoplay = true;
}

Friday, 28 August 2015

How Access Login page with help of Facebook in Asp.Net

Step-1: Register site in Facebook:

In order to use Facebook login, you should must have a verified developer account in Facebook (which is very easy to create just login to http://developers.facebook.com using your Facebook credentials and follow the screen instructions) and you must register you website in the form of an App in Facebook.
So lets start with registering your site with Facebook:
  1. Open http://developers.facebook.com and login with your Facebook credentials after logging in you will get a screen like this:
  1. Click on "Build for Websites" link, you will reach to https://developers.facebook.com/docs/guides/web/ , just click on 'Apps' menu Items (the last menu Item from right side), you will reach to https://developers.facebook.com/apps. Click on  button, you will get following screen.
  2. Enter the name of your website in place of App Name, rest of the fields are optional so fill them if you require to use them and click on 'Continue' button. you will be asked to fill a captcha screen and then you will get the summary screen like below screenshot (Just masked the AppID).
  3. Give the URL of you website/ application Site URL text box of the app summery screen, like below screenshot. I have given localhost address as I will be testing the app from my local build, you will have to give your website URL in this.
Now you are ready to create ASP.Net Web Application, to use Facebook integration. Step-2: Create ASP.NET application using Facebook login:
  1. Open Visual Studio.
  2. Select File --> New --> Project/Solution --> 'ASP.NET Empty Application'
  3. Now Right Click on Project Name Select Add--> New Item from the pop-up menu and click on 'Web Form' to add Default.Aspx page. (See screenshot).
  4. Most of the code we will be doing on the HTML code of ASP.NET Page.
  5. For using Facebook login, we will have to use Facebook JavaScript SDK. There are three ways of using it:
    1. Load the JavaScript SDK asynchronously.
       <script>
          // Load the SDK Asynchronously
          (function (d) {
              var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
              if (d.getElementById(id)) { return; }
              js = d.createElement('script'); js.id = id; js.async = true;
              js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
              ref.parentNode.insertBefore(js, ref);
          } (document));
      </script>
      
    2. Load the JavaScript SDK synchronously
      <script src=https://connect.facebook.net/en_US/all.js type="text/javascript"></script>
    3. Download the SDK JavaScript file in your local project folder and use it from there. Implemented in 'Default.aspx' of solution.
      <script src="scripts/all.js" type="text/javascript"></script>
  6. We will also require jquery for initializing the library. so copy following code in header section of the page.
    <script
    src="scripts/jquery-1.8.0.min.js"
    type="text/javascript"></script> <script
    src="scripts/all.js"
    type="text/javascript"></script> 7) Now to initialize the Facebook SDK copy following code in a new script section :
             $("document").ready(function () {
                // Initialize the SDK upon load
                FB.init({
                    appId: 'YOUR_APP_ID', // App ID
                    channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File
                    scope: 'id,name,gender,user_birthday,email', // This to get the user details back from Facebook
                    status: true, // check login status
                    cookie: true, // enable cookies to allow the server to access the session
                    xfbml: true  // parse XFBML
                });
                // listen for and handle auth.statusChange events
                FB.Event.subscribe('auth.statusChange', OnLogin);
            });
    Here YOUR_APP_ID will be the App Id you will get from the Facebook App.
  7. Also copy the following code after Above code in to get the response back and fill the values.
    // This method will be called after the user login into facebook.
    function OnLogin(response) {
        if (response.authResponse) {
            FB.api('/me?fields=id,name,gender,email,birthday', LoadValues);
        }
    }
    
    //This method will load the values to the labels
    function LoadValues (me) {
        if (me.name) {
            document.getElementById('displayname').innerHTML = me.name;
            document.getElementById('FBId').innerHTML = me.id;
            document.getElementById('DisplayEmail').innerHTML = me.email;
            document.getElementById('Gender').innerHTML = me.gender;
            document.getElementById('DOB').innerHTML = me.birthday;
            document.getElementById('auth-loggedin').style.display = 'block';
                        }
    }
    
  8. Copy following code in the Body tag of the Page.
    <div id="fb-root"></div> <!-- This initializes the FB controls-->
    <div class="fb-login-button" autologoutlink="true" scope="user_birthday,email" >
      Login with Facebook
     </div> <!-- FB Login Button -->
    <!-- Details -->
    <div id="auth-status">
    <div id="auth-loggedin" style="display: none">
        Hi, <span id="displayname"></span><br/>
        Your Facebook ID : <span id="FBId"></span><br/>
        Your Email : <span id="DisplayEmail"></span><br/>
        Your Sex:, <span id="Gender"></span><br/>
        Your Date of Birth :, <span id="DOB"></span><br/>
    </div>
    </div>
    
  9. Your application is ready now, execute the application.
In this blog we have learned how to register an application in Facebook and the use that application for Facebook authentication in our ASP.NET website/application. Using this the user of your web site/application will be free from the hassle of registering and entering their details again if they have Facebook Id they can use that to login.

Commit And Rollback Command

Commit Command:

Commit is used for the permanent changes. When we use Commit in any query then the change made by that query will be permanent and visible. We can't Rollback after the Commit.
Syntax:

begin tran tranName
Command for operation
commit tran tranName
Here tranName is the name of the transaction and the command for operation is the SQL statement that is used for the operation like making a change or inserting data etc.
Example:

begin
 tran d
update
 emp set empName ='D' where empid=11
commit
 tran d
Rollback Command:
Rollback is used to undo the changes made by any command but only before a commit is done. We can't Rollback data which has been committed in the database with the help of the commit keyword.

Syntax:
begin tran tranName
Command for operation
Rollback tran tranName
Here tranName is the name of the transaction and the command for the operation is the SQL statement that is used for performing operations like to make any change or insert data etc.

Example:
We want that, if data entered by user has an empId less than 10 then the command is rolled back and a message isshown to the user "An id less than 10 is not valid; query is rolled back".
 

begin tran t
declare
 @id int;
set
 @id=1;
insert
 into emp values(@id,'d')
if
(@id<10)
begin
print
'An id less than 10 is not valid; query is rolled back';
rollback
 tran t;
end 
else
begin
print
 'data is inserted'
end