Thursday, June 23, 2016

Create Your Own Web Based Music Player


Web Based Music Player



Host your own web based music player for FREE !

Here, just as an example, I walk you through the usage of already developed Last.fm (The popular music content provider) web based client application and its deployment on one of the cloud platform. In case you are developing a web based music player using Last.fm as a content provider, you can deploy it onto your existing website(s) or you may want to place it onto your personal blog site.


Last.fm


Last.fm is a music website which can serve as an online music database provider which might not provide full free access to the music contents. There are other online music content providers that are largely available for free of charge e.g. vTunner, FreeDB, etc. In order to make your player free, you need to opt for any of such free music content provider.

The idea can be anything like to create the web based music player with rich UI contents (requires some impressing UI designing skills) which can allow us to browse and play the music contents in the below categories.

Music tracks categorized by:
Artist
Album
Track
Playlist
Favorites
Queue

Internet Radio's categorized by:
Genre
Country
Language
Favorites


The original client application

Here is the original web based client application for Last.fm. This application is built using HTML/CSS/PHP, MySql, and using AJAX/JavaScript/JQuery.

The application gives us the useful information about the Last.fm's top entities e.g. Artists, Album's or Tracks. It allows us to fetch the details about any particular track. Details could be anything like track name, url, duration, artist, album, album art url for different sizes, etc.


Hosting client application in the Cloud

I don't have the required environment setup to build and run this application on my machine. So, I used some trick to build and run this application anyway. In my earlier blog article, Run any code without any Environment SetupI mentioned about the Cloud9 platform. I used the same platform here to manage the things as required.

First of all, you need to create the work-space to host this application in Cloud9. You can also clone it from the GitHub link that I shared earlier for this application. While you create the work-space, you need to choose the correct reconfigured work-space template mentioned there. Once the work-space is created, Colud9 provides us the required environment setup to proceed further.


Setup and Configure the MySQL Database - The Workflow

In order to proceed further, you need to setup and configure the MySql database.
  • Configure your config.ini file as below:

; if change pls use the following command: git update-index --assume-unchanged config.ini
[mysql]
hostname = "localhost"
database = "test"
username = "root"
password = ""

[lastfm]
api_key = "e85bfd5e26e0e91b53160653d86ba063"

[google]
api_key = "AIzaSyDn-UcwMbrIiX8wgyNAlLyHnmqOvZsaddw"

 
  • Right click the workspace folder and click 'Open Ternminal Here'. This should open the terminal.
  • Execute below commands to run the sql script to import the mysql database (import_db.sql).

> mysql-ctl start

> mysql -u root

 
  • Note: Log in without a password, as, the default root password is blank (i.e. empty string) not root.
  • This should open the mysql command prompt. Now, run the below commands to create the test database (db name is already configured in config.ini)

mysql> CREATE DATABASE test;
mysql> USE test

Database changed

 
  • Run the contents of import_db.sql here e.g. CREATE TABLE IF NOT EXISTS 'logtracks'... e.g. mysql> <contents of import_db.sql>
  • Now the database is created and ready to use.
A PHP class LastfmController handles the call's to the LastFM API. There are some java scripts to help you get the response from the server and parse that JSON response into the tabular form as represented in its GUI. For example, the function getTopTracksTag in lastfm_artists.js file used to get the top tracks from the Last.fm. In response to that call, server gives the result in JSON format. The response is then parsed and filled into the HTML table to show to the user. 

The sample JSON that we get is shown as below with the online JSON parser.



Run the client application

Now, you can run the application. On the created work-space there is an option "Run Project" to start running the application. Once you hit the run button, it internally starts the Apache server to host your application on. Once it is ready, the platform provides you the serving link as below using which you can test your application.


Starting Apache httpd, serving https://lastfmclient-jegaonkarsachin.c9users.io/.
Started apache2

 

When you run the application, it shows its own UI with the options to specify some inputs. Now, put some artist name and then select one of the tag names it has populated depending upon the artist name you put. It shows the top tracks tag result as below.




Playing the audio on web using the HTML5 <audio> Element

HTML5 provides a standard for playing audio files. Before HTML5, there was no standard for playing audio files on a web page.

To play an audio files or radio stations in HTML, use the <audio> element:

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
</audio>
 


Conclusion

From the things that I just have mentioned, I can say that, using Last.fm or using similar music content providers(but FREE!), one can create the rich web based music player for FREE !

You would have got an idea about generally how to consume music contents that the provider provides and how it can be used to create the intended player application.


Popular Music Content Providers


Wednesday, June 22, 2016

Call a C# Method From JavaScript



Imagine from within your Windows Forms applications, you are actually navigating different web pages and treating it as a Web Application. This is made possible by the .NET component called as WebBrowser. This control is available as a tool to be put onto your Windows Form.

The WebBrowser .NET control enables the user to navigate Web pages inside your Windows Form.

The below sample shows how to make a use of WebBrowser control and make a call C# from JavaScript or the other way round.


       
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WebBrowserDemo
{
    public partial class WebBrowserDemo : Form
    {
        private HtmlElement button;

        public WebBrowserDemo()
        {
            InitializeComponent();

            // Set the WebBrowser that can be accessed by scripting code 
            // that is contained within the web page displayed in the WebBrowser control.
            webBrowser.ObjectForScripting = new ScriptHanlder(this);

            // Set the HTML contents of the web page 
            // displayed in the WebBrowser control.
            webBrowser.DocumentText = 
    @"<html>
    <head>
        <style>
        .floating-box { float: left; width: 90px; height: 90px; margin: 20px; background-color:#73AD21;}
        .floating_icon { width:100%; text-align:center; height:90%; padding-top:30%; color:#134880;}
        </style>
    </head>
    <body>
    <div width=""120"" height=""120"" border=""3"" >
        <div id=""btnZero"" class=""floating-box"" onClick=""window.external.OnClick(0);"">
        <div class=""floating_icon""> <h2>0</h2> </div>
        </div>
        <div id=""btnOne"" class=""floating-box"" onClick=""window.external.OnClick(1);"">
        <div class=""floating_icon""> <h2>1</h2> </div>
        </div>
        <div id=""btnTwo"" class=""floating-box"" onClick=""window.external.OnClick(2);"">
        <div class=""floating_icon""> <h2>2</h2> </div>
        </div>
        <div id=""btnThree"" class=""floating-box"" onClick=""window.external.OnClick(3);"">
        <div class=""floating_icon""> <h2>3</h2> </div>
        </div>
        <div id=""btnFour"" class=""floating-box"" onClick=""window.external.OnClick(4);"">
        <div class=""floating_icon""> <h2>4</h2> </div>
        </div>
        <div id=""btnFive"" class=""floating-box"" onClick=""window.external.OnClick(5);"">
        <div class=""floating_icon""> <h2>5</h2> </div>
        </div>
        <div id=""btnSix"" class=""floating-box"" onClick=""window.external.OnClick(6);"">
        <div class=""floating_icon""> <h2>6</h2> </div>
        </div>
        <div id=""btnSeven"" class=""floating-box"" onClick=""window.external.OnClick(7);"">
        <div class=""floating_icon""> <h2>7</h2> </div>
        </div>
        <div id=""btnEight"" class=""floating-box"" onClick=""window.external.OnClick(8);"">
        <div class=""floating_icon""> <h2>8</h2> </div>
        </div>
        <div id=""btnNine"" class=""floating-box"" onClick=""window.external.OnClick(9);"">
        <div class=""floating_icon""> <h2>9</h2> </div>
        </div>
    </div>
    </body>
    </html>";
        }

        public void NumberClicked(int num)
        {
            switch (num)
            {
                case 0:
                    button = webBrowser.Document.GetElementById("btnZero");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "0" ? "<div class=\"floating_icon\"> <h2>Zero</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>0</h2> </div>";
                    break;
                case 1:
                    button = webBrowser.Document.GetElementById("btnOne");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "1" ? "<div class=\"floating_icon\"> <h2>One</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>1</h2> </div>";
                    break;
                case 2:
                    button = webBrowser.Document.GetElementById("btnTwo");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "2" ? "<div class=\"floating_icon\"> <h2>Two</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>2</h2> </div>";
                    break;
                case 3:
                    button = webBrowser.Document.GetElementById("btnThree");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "3" ? "<div class=\"floating_icon\"> <h2>Three</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>3</h2> </div>";
                    break;
                case 4:
                    button = webBrowser.Document.GetElementById("btnFour");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "4" ? "<div class=\"floating_icon\"> <h2>Four</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>4</h2> </div>";
                    break;
                case 5:
                    button = webBrowser.Document.GetElementById("btnFive");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "5" ? "<div class=\"floating_icon\"> <h2>Five</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>5</h2> </div>";
                    break;
                case 6:
                    button = webBrowser.Document.GetElementById("btnSix");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "6" ? "<div class=\"floating_icon\"> <h2>Six</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>6</h2> </div>";
                    break;
                case 7:
                    button = webBrowser.Document.GetElementById("btnSeven");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "7" ? "<div class=\"floating_icon\"> <h2>Seven</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>7</h2> </div>";
                    break;
                case 8:
                    button = webBrowser.Document.GetElementById("btnEight");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "8" ? "<div class=\"floating_icon\"> <h2>Eight</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>8</h2> </div>";
                    break;
                case 9:
                    button = webBrowser.Document.GetElementById("btnNine");
                    if (button != null)
                        button.InnerHtml = button.InnerText == "9" ? "<div class=\"floating_icon\"> <h2>Nine</h2> </div>" : 
                            "<div class=\"floating_icon\"> <h2>9</h2> </div>";
                    break;
            }
        }

        // Set the class as ComVisible to be able to access it from JavaScript.
        [ComVisible(true)]
        public class ScriptHanlder
        {
            private WebBrowserDemo demoForm;

            public ScriptHanlder(WebBrowserDemo form)
            {
                // Store the form to refer it later.
                demoForm = form;
            }

            // Method called from JavaScript.
            public void OnClick(int num)
            {
                demoForm.NumberClicked(num);
            }
        }
    }
}
       
 

More information about WebBrowser can be found here.

 
biz.