|
Continuando com o MP3 Player, dessa vez vamos carregar também a imagem do album, usando o webservice da LasFm.
Primeiro criamos um MXML com os texts e labels para receber essas informações:
mp3Player.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="225" width="357">
<mx:Text x="65" y="23" id="tTitulo" fontWeight="bold" color="#FFFFFF"/>
<mx:Label x="10" y="23" text="Música:" fontWeight="bold" color="#000000"/>
<mx:Label x="10" y="60" text="Artista:" fontWeight="bold" color="#000000"/>
<mx:Label x="11" y="96" text="Album:" fontWeight="bold" color="#000000"/>
<mx:Label x="24" y="130" text="Ano:" fontWeight="bold" color="#000000"/>
<mx:Text x="65" y="60" id="tArtista" fontWeight="bold" color="#FFFFFF"/>
<mx:Text x="65" y="96" id="tAlbum" fontWeight="bold" color="#FFFFFF"/>
<mx:Text x="64" y="130" id="tAno" fontWeight="bold" color="#FFFFFF"/>
<mx:Button x="24" y="176" label="Play" id="btPlay" click="{PlaySong()}"/>
<mx:Canvas x="245" y="9" width="104" height="104" borderStyle="solid" borderColor="#000000" backgroundColor="#FFFFFF">
<mx:Image x="1" y="1" id="img" width="100" height="100"/>
</mx:Canvas>
</mx:Application>
Precisamos importar algumas classes que são necessárias para carregar o MP3 e a tag:
import flash.media.Sound;
import flash.media.ID3Info;
import flash.net.URLLoader;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
Definiremos algumas variáveis para tratar as informações:
private var musica:Sound;
private var load:URLLoader;
private var id3:ID3Info;
private var played:Boolean = false;
private var request:HTTPService;
private var lasFmPath:String = "http://ws.audioscrobbler.com/2.0/?method=album.search&api_key=b25b959554ed76058ac220b7b2e0a026&album=";
O Path do webservice, requer o uso da api_key, sem ele não é possível obter o resultado correto.
Vamos ver o Código ActionScript com toda a implementação:
<mx:Script>
<![CDATA[
import flash.media.Sound;
import flash.media.ID3Info;
import flash.net.URLLoader;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
private var musica:Sound;
private var load:URLLoader;
private var id3:ID3Info;
private var played:Boolean = false;
private var request:HTTPService;
private var lasFmPath:String = "http://ws.audioscrobbler.com/2.0/?method=album.search&api_key=b25b959554ed76058ac220b7b2e0a026&album=";
private function Constructor():void
{
musica = new Sound(new URLRequest("mp3/lulu.mp3"));
musica.addEventListener(Event.ID3, id3Handler);
request = new HTTPService();
}
private function id3Handler(e:Event):void
{
id3 = musica.id3;
tAlbum.text = id3.album;
tAno.text = id3.year;
tArtista.text = id3.artist;
tTitulo.text = id3.songName;
request.url = lasFmPath + id3.album;
request.send();
request.addEventListener(ResultEvent.RESULT,imageHandler);
}
private function imageHandler(e:ResultEvent):void
{
img.source = e.result.lfm.results.albummatches.album[1].image[2].value;
}
private function PlaySong():void
{
if(!played)
{
musica.play();
played = true;
}
}
]]>
</mx:Script>
Bom, dessa forma, teremos o carregamento do MP3 na tela, e ao clicar no botão play, tocaremos a música carregada.
Veja funcionando abaixo:
|