Welcome

Welcome, thanks to look my blog

Friday, 18 April 2014

Feeling the Beat with REST

Feeling the Beat with REST

Being a big music fan, I was really excited to try out the Beats Music API. Beats recently introduced a music subscription service and opened up their api to developers. I decided to create a fun sample that allows you to search for an artist by name and then return the list of available audio tracks for that particular artist.
This example requires an SSL encrypted connection to access the api. For Windows and iOS, this means you will need to install/include the SSL libraries. For Android, that is not necessary. More info on that topic can be found here.

This application consists of a tab control that is aligned to the client, containing two tab items and a toolbar with the title of the application parented to each of the 2 tab items. Tab1 contains several graphics for the design of the application and a top aligned toolbar with the name of the application.
Tab2 includes a client aligned ListView that displays all the audio tracks that are returned based on the query.
The search bar is just a bitmap (since I was going for a specific look) with a parented TEdit. The Search button is part of the bitmap, so I just used a TRectangle with no fill or stroke color to create a clickable masked area over the Search button part of the graphic. For TEdit, I right-clicked on the component at design time and selected ‘Add Item -> TClearEditButton’. This added the ‘x’ for clearing the input field. Alternately, you could have also used the TClearingEdit component instead of the TEdit component.
You will need the following RAD Studio component to access the beats audio REST api and use it in your application:
  • RestClient
  • RestResponse
  • RestResponseAdapter
  • RestRequest
  • FDMemTable
You will also need to sign up for a developer account to get your developer key that you will need in your application (https://developer.beatsmusic.com/member/register).





I defined the following parameters:

For the display of the list on the second tab, I selected the ListItemRightDetail option. This provides me with 2 bindable members (Item.Text and Item.Detail) in the LiveBindings Designer.

I created the following bindings via the LiveBindings Designer (View->LiveBindings Designer).

This app only required a couple lines of code. When we first create the form, we want to execute the REST request:
procedure TForm1.FormCreate(Sender: TObject);
begin
RestRequest1.Execute;
end;
When the user clicks the Rectangle (the mask over the ‘Search’ bitmap), we need to change the tab (navigate to the second tab), and then also execute the REST request:
procedure TForm1.Rectangle1Click(Sender: TObject);
begin
TabControl1.ActiveTab := TabItem2;
RestRequest1.Execute;
end;
We also want the user to be able to leverage the back button on Android to navigate from the second tab back to the first tab which includes our search bar:
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
if Key = vkHardwareBack then
begin
if TabControl1.ActiveTab = TabItem2 then
begin
TabControl1.ActiveTab := TabItem1;
Key := 0;
end;
end;
end;

Here are some screenshots of the running app:


 sumber : http://blogs.embarcadero.com/sarinadupont/2014/03/21/feeling-the-beat-with-rest/

No comments:

Post a Comment