PDA

View Full Version : XML - AS3 Help



jonstle
November 17th, 2009, 02:57 PM
I am trying to create an audio that only uses the first entry in an xml file. I have most of the things working but I keep getting an error related to the audio file.

TypeError: Error #1034: Type Coercion failed: cannot convert "http://rivervalleychurch.net/sermonaudio/Podcast/06_Jesus_Save_Your_Church_First_Love.mp3" to flash.net.URLRequest.
at latestMessage_fla::MainTimeline/frame1()

I inserted the url for the file into error message so you can see if there is s problem with that. I was initially trying to use a variable to hold the url for me, because this xml file gets updated every week and I only want to access the most recent entry.

This is my first attempt at making an audio player in flash, so I am sure I have made a simple mistake, but for the life of me I cannot figure out what it is.

Here is the code I have so far: (Please Try not to Laugh, at least not out loud)


//Load the podcast.xml File
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var audioFile:String;
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://www.rivervalleychurch.net/sermonaudio/Podcast/podcast.xml"));
function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);
ParseBooks(xmlData);

}

//Load text into text fields
function ParseBooks(podcastInput:XML):void {

description_txt.text = (podcastInput.channel.item.description.text()[0]);
title_txt.text = (podcastInput.channel.item.title.text()[0]);
publish_txt.text = (podcastInput.channel.item.pubDate.text()[0]);
audioFile = (podcastInput.channel.item.link.text()[0]);

//trace (audioFile);
}

//Audio Player Controls and Stuff
var req:URLRequest = URLRequest("audioFile");
var sound:Sound = new Sound();
var controller:SoundChannel;

function soundLoaded(event:Event):void
{
controller = sound.play();
controller.stop();
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, stopSound);

}

function playSound(event:MouseEvent):void
{
controller = sound.play();
}

function stopSound(event:MouseEvent):void
{
controller.stop();
}

sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);

//Event Listeners For buttons
itunes_btn.addEventListener(MouseEvent.CLICK, goItunes);
archives_btn.addEventListener(MouseEvent.CLICK, goArchives);
rss_btn.addEventListener(MouseEvent.CLICK, goRss);

//Functions For Buttons
function goItunes(e:MouseEvent):void{
navigateToURL(new URLRequest("http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=262248424"));
}
function goArchives(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.rivervalleychurch.net/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=91"));
}
function goRss(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.rivervalleychurch.net/sermonaudio/Podcast/podcast.xml"));
}


One last question is in regards to using id3 tags versus xml tags to fill dynamic text fields in the player. Currently I am using the xml file to do this, are there any advantages one way or the other for this. The only reason I ask is that I am hoping to display an "album art" image from the id3 tag in the player. Would it be better to grab all of the information from the mp3 file?

creatify
November 17th, 2009, 03:18 PM
try something like this - even though you wait for the xml to load, you're trying to set the sound object immediately - instead of waiting for the xml to load, parsing the data etc. There are a few things I changed, see if this works:




var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML;
var audioFile:String;
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://www.rivervalleychurch.net/sermonaudio/Podcast/podcast.xml"));

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseBooks(xmlData);
}

//Load text into text fields
function ParseBooks(podcastInput:XML):void {
description_txt.text = (podcastInput.channel.item.description.text()[0]);
title_txt.text = (podcastInput.channel.item.title.text()[0]);
publish_txt.text = (podcastInput.channel.item.pubDate.text()[0]);

// not sure if the url type was wrong here, but forcing it to string
audioFile = (podcastInput.channel.item.link.text()[0].toString());
trace(audioFile);

startAudioLoad();
}


var req:URLRequest;
var sound:Sound;
var controller:SoundChannel;
function startAudioLoad():void {
//Audio Player Controls and Stuff
// TOOK QUOTE OF OF audioFile
req = URLRequest(audioFile);
sound = new Sound();
sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);
}

function soundLoaded(event:Event):void {
controller = sound.play();
controller.stop();
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, stopSound);
}

function playSound(event:MouseEvent):void {
controller = sound.play();
}

function stopSound(event:MouseEvent):void {
controller.stop();
}

//Event Listeners For buttons
itunes_btn.addEventListener(MouseEvent.CLICK, goItunes);
archives_btn.addEventListener(MouseEvent.CLICK, goArchives);
rss_btn.addEventListener(MouseEvent.CLICK, goRss);

//Functions For Buttons
function goItunes(e:MouseEvent):void{
navigateToURL(new URLRequest("http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=262248424"));
}
function goArchives(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.rivervalleychurch.net/index.php?option=com_content&view=category&layout= blog&id=37&Itemid=91"));
}
function goRss(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.rivervalleychurch.net/sermonaudio/Podcast/podcast.xml"));
}

jonstle
November 17th, 2009, 03:32 PM
Here is the error I received after trying that. I am still a relative newbie to Action Script so I greatly appreciate you taking the time to look at it for me.

TypeError: Error #1034: Type Coercion failed: cannot convert "http://rivervalleychurch.net/sermonaudio/Podcast/06_Jesus_Save_Your_Church_First_Love.mp3" to flash.net.URLRequest.
at latestMessage_fla::MainTimeline/startAudioLoad()
at latestMessage_fla::MainTimeline/ParseBooks()
at latestMessage_fla::MainTimeline/LoadXML()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

creatify
November 17th, 2009, 03:54 PM
oh - there is a syntax error I missed from your original code - the startAudioLoad function,

change this:
req = URLRequest(audioFile);

to this:
req = new URLRequest(audioFile);

without the new directive, it was trying to typecast the string audioFile as a URLRequest, which exactly what the error is informing you about.

jonstle
November 17th, 2009, 03:59 PM
That looks like it took care of it for me. Thanks for your help