ASP. NET web service and flash to create MP3 players

Source: Internet
Author: User
Yesterday I installed Flash MX 2004 pro. After reading the introduction, I added a lot of new features and added a lot of data components. Then I felt like ASP. net is so simple to develop Web services, so it's a whim: Why don't I use C # And flash as a player. The following describes the simple principle.

Write a web service with a csung file, list all the metadata files in a specified directory (mainly using some Members in system. Io), add them to an arraylist, and finally publish the arraylist as a web service. Then, use Flash MX 2004 to create a flash document, use the webserviceconnector component to obtain the newly released web service, bind the returned results to the list component of flash, and finally, add an action to the list component to enable it to play the double-click MP3 file.

  I,Develop WebService

The specific code is as follows, which is very simple. We will not talk about it much more. We are all familiar with Web Service Development. Let alone such a simple example. Create an MP3 folder under the root directory of the site, and then put some MP3 files in it. Finally, you can preview and test the web service. The preview address is related to the created virtual directory path, my example is as follows.

Http: // onlytiancai/bak/flashmp3/mp3services. asmx

<% @ WebService Language = "C #" class = "mp3service" %>
Using system;
Using system. collections;
Using system. Web. Services;
Using system. IO;

Public class mp3service: system. Web. Services. WebService
{
Public mp3service ()
{
}

[Webmethod]
Public arraylist getmp3 ()
{
Arraylist mp3list = new arraylist ();
String mp3path = server. mappath ("MP3 ");
Try
{
Directoryinfo di = new directoryinfo (mp3path );
Fileinfo [] Fi = Di. getfiles ();
Foreach (fileinfo fitemp in FI)
{
If (fitemp. extension. tolower () = ". MP3" | fitemp. extension. tolower () = ". wma ")
{
Mp3list. Add (fitemp. Name );
}
}
}
Catch (exception E)
{
Console. writeline ("the process failed: {0}", E. tostring ());
}
Return mp3list;
}
}

II,Developing flashClient

Open flash and create a new flash document. Press Ctrl + Shift + F10 to open the Web service panel. Click the Add Web Service icon on the left to add the Web service address we developed in step 1, as follows.

The Web Service Panel is shown in figure.

Drag a list component from the component panel to the scenario, name it mp3list In the attribute panel, right-click the mp3list component, and select [arbitrary deformation ], drag to the appropriate size. Drag and Drop a button component and place it under mp3list. It is called submit. In the component checker, set the label attribute of the submit button to "get music list ". Then adjust the film size to 250*400. The final effect is as follows.

Open the Web Service Panel, right-click the getmp3 () method, and select Add method call. Then a web service instance is automatically added to the scenario. Select it and name it mp3serivice In the attribute. Keep mp3serivice selected, open the component checker panel, switch to the binding label, click the plus icon in the upper left corner of the panel, select results: array, and click OK. Then, select results in the BIND tag, double-click the following bound to, select the mp3list component in the bind to dialog box, and click OK. Last shown in figure

Select the submit button, press SHIFT + F3 to bring up the behavior panel, click the plus icon in the upper left corner of the Panel, and select data => trigger data source ], in the "trigger data source" dialog box that appears, select mp3serivice and click "OK.

At this time, the following code is automatically added to the submit action panel.

On (click ){
// Trigger data source Behavior
// Macromedia 2003
This._parent=serivice. Trigger ();
}

URL of the hosts file: Enter http: // onlytiancai/bak/flashmp3/MP3 in the text box, enter an example named MP3 in the instance name below, and click OK ,.

Invalid. loadsound ("http: // onlytiancai/bak/flashmp3/MP3", and the trueappsmetadata is changed to _global.behaviors.sound.pdf. loadsound ("http: // onlytiancai/bak/flashmp3/MP3/" + this. selecteditem, true); finally, the mp3list component's Action Code is as follows.

On (Change ){
// Load streaming MP3 Behavior
If (_ global. behaviors = NULL) _ global. behaviors = {};
If (_ global. behaviors. Sound = NULL) _ global. behaviors. Sound = {};
If (typeof this. createemptymovieclip = 'undefined '){
This. _ parent. createemptymovieclip ('bs _ mp3', new date (). gettime ()-(math. floor (new date (). gettime ()/10000) * 10000 ));
_Global.behaviors.sound.pdf = new sound (this. _ parent. bs_mp3 );
} Else {
This. createemptymovieclip ('_ MP3 _', new date (). gettime ()-(math. floor (new date (). gettime ()/10000) * 10000 ));
_Global.behaviors.sound.pdf = new sound (this. bs_mp3 );
}
_Global.behaviors.sound.pdf. loadsound ("http: // onlytiancai/bak/flashmp3/MP3/" + this. selecteditem, true );

}

Okay, press Ctrl + enter to test your flash MP3 player. If the test is successful, CTRL + ALT + Shift + s export the video. I put a few songs in the MP3 directory and tested them. Double-click the name of the song to play the song. Then I can put the song I want to hear into that directory ,.

Improvement:

  This is a simple demonstration. There are some media playback control components in Flash MX 2004. If you use these components to improve the player, you can control the MP3 volume and playback progress, it can also support more sound formats. Another point is that the Web service we developed is also very simple. You can expand it to add some search and Song favorites functions, you can develop a website at the same time and use access as the background database. After registering and logging on to the user, you can add the favorite songs to your own private database. After that, you will be able to call them out. In addition, you can independently publish music as a Windows Media Server in the form of streaming media, so that your applications can have higher performance and scalability, because you can provide the streaming media service on another server, right.

  Summary:

  Actually, I feel like ASP. net is the most efficient service segment technology, and Flash is the most expressive client performance technology. I downloaded flash remoting and installed it. I don't want to test it when I saw it in English. Applications developed using their advantages have greatly improved the user experience, so they are just out of order. Although Microsoft's Smart Client technology can also effectively enhance user experience, the RIA technology represented by Flash is also very good, at least comparable to Ajax.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.