A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: help loading mp3 and images from xml into flash

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4

    help loading mp3 and images from xml into flash

    hello, I have been following this tutorial:

    http://www.developphp.com/view.php?tid=590

    Everything is working as far as this tutorial is concerned.
    however, i wanted to add a text field(which is working) and a movie clip to load images(which i cannot get to work). I have created a movie clip named imageLoader and would like to load the .jpg files here. I tried to mimic the code that was in place for the mp3s but its just not working and I don't know what I'm doing wrong.

    could someone please take a look at my code and advise?

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <XML>

    <Song>
    <songTitle>Dancing Room Only</songTitle>
    <songArtist>Harvey Scales</songArtist>
    <image>images/image1.jpg</image>
    <songInfo> this is the song info for the songs to be played in the musicality radio player</songInfo>
    </Song>

    <Song>
    <songTitle>Get Up To Get Down</songTitle>
    <songArtist>Brass Construction</songArtist>
    <image>images/image2.jpg</image>
    <songInfo> this is the second song info for the songs to be played in the musicality radio player</songInfo>
    </Song>

    <Song>
    <songTitle>Give Me Your Love</songTitle>
    <songArtist>Sylvia Striplin</songArtist>
    <image>images/image3.jpg</image>
    <songInfo> this is the third song info for the songs to be played in the musicality radio player</songInfo>
    </Song>

    </XML>




    and then my Actionscript 3

    import flash.net.URLRequest;

    //////////////////////////////////////////////
    // CODE FOR FRAME 1
    //////////////////////////////////////////////
    stop();

    var myFormat:TextFormat = new TextFormat();
    myFormat.color = "0xFFFFFF";

    list.setRendererStyle("textFormat", myFormat);

    //////////////////////////////////////////////////////////////////////////////////////////////////

    // Initialize variables
    var trackToPlay:String;
    var pausePosition:int = 0;
    var songURL:URLRequest;
    var imageURL:URLRequest;
    var i:uint;
    // Initialize the XML, place the xml file name, initialize the URLRequest
    // put URLRequest into a new URLLoader, and add event listener on
    // myLoader listening for when the XML loading is complete
    var myXML:XML = new XML();
    var XML_URL:String = "mp3_playlist.xml";
    var myXMLURL:URLRequest = new URLRequest(XML_URL);
    var myLoader:URLLoader = new URLLoader(myXMLURL);
    myLoader.addEventListener("complete", xmlLoaded);


    // Create the xmlLoaded function. What happens when the XML file is fully read
    function xmlLoaded(event:Event):void {

    // Place the xml data into the myXML object
    myXML = XML(myLoader.data);
    // Access song 1 in the XML file to start the player
    var firstSong:String = myXML..Song.songTitle[0];
    var firstArtist:String = myXML..Song.songArtist[0];
    var firstInfo:String = myXML..Song.songInfo[0];
    var firstImage:String = myXML..Song.image[0];
    songURL = new URLRequest("mp3_files/" + firstSong + ".mp3");
    imageURL = new URLRequest("images/" +firstImage + " .jpg");
    status_txt.text = "1. "+firstSong +" - "+firstArtist;
    info_txt.text = firstInfo;
    imageLoader.loadMovie = firstImage;

    // Run the "for each" loop to iterate through all of the song items listed in the external XML file
    for each (var Song:XML in myXML..Song) {

    i++; // Increment the song counter by one
    // Access the value of the "itemColor" node in our external XML file
    var songTitle:String = Song.songTitle.toString();
    // Access the value of the "itemLabel" node in our external XML file
    var songArtist:String = Song.songArtist.toString();
    var songInfo:String = Song.songInfo.toString();
    var image:String = Song.image.toString();
    // Adds each song into the list component through this loop
    list.addItem( { label: i+". "+songTitle+" - "+songArtist, songString: songTitle, Info: songInfo, Movie: image, Artist: songArtist, songNum: i } );

    }
    var myArray = new Array (0,0);
    list.selectedIndices = myArray; // This highlights song 1 by default
    gotoAndStop(3);

    }



    //////////////CODE FOR FRAME 2////////////
    // This is where and when the song switching takes place when a list cell is clicked
    songURL = new URLRequest("mp3_files/" + trackToPlay + ".mp3");





    //////////////////CODE FOR FRAME 3//////////////////////////
    stop();
    var snd:Sound = new Sound();
    var channel:SoundChannel;
    var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
    snd.load(songURL, context);
    channel = snd.play(pausePosition); // Start playing
    // Playlist item click listener
    list.addEventListener(Event.CHANGE, itemClick);
    // Playlist item click function
    function itemClick (event:Event):void {
    channel.stop(); // stop play
    status_txt.text = "" + event.target.selectedItem.label + "";
    info_txt.text = "" + event.target.selectedItem.Info + "";
    imageLoader.MovieClip = "" + event.target.selectedItem.Movie + "";
    trackToPlay = event.target.selectedItem.songString;
    gotoAndPlay(2);
    }

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to have a Loader object in your MovieClip and use the load function. This line is wrong:
    var myLoader:Loader = new Loader();
    myLoader.load(new URLRequest(firstImage));
    imageLoader.addChild(myLoader);

    Here is a tutorial for loading images using XML
    http://flashscript.biz/flashas3/xml_...slideshow.html
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    I get confused with a lot of the tutorials i find online because they are doing things that i don't need.
    the one you referenced is for a slideshow with thumbnails, etc.
    I'm having a hard time stripping out just the code that i need and applying it to my project.

    I've got a playlist of mp3's. When a song is clicked on there is a dynamic text field that displays the song name and then another dynamic text field that loads song info from a text field defined in the XML, this is all working.
    I'd like my movie clip to load corresponding images for each song from the XML. No thumbnails or transitions necessary.

    any advice on how to make this work with my code? I tend to get lost trying to find what i need in these tutorials.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I gave you the answer also in the three lines. You need to create a Loader object and add that to the movieclip. Then you have to load the image to the Loader.

    var myLoader:Loader = new Loader();
    myLoader.load(new URLRequest(firstImage));
    imageLoader.addChild(myLoader);

    firstImage can be replaced by another image.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by cancerinform View Post
    I gave you the answer also in the three lines. You need to create a Loader object and add that to the movieclip. Then you have to load the image to the Loader.

    var myLoader:Loader = new Loader();
    myLoader.load(new URLRequest(firstImage));
    imageLoader.addChild(myLoader);

    firstImage can be replaced by another image.


    Thank you. That worked. first image is loading. but images are not changing when an item from the playlist is clicked. all the info, titles and mp3's are changing just not the images in the movie clip. could you take a look at this code here and see what could be wrong/

    // Playlist item click listener
    list.addEventListener(Event.CHANGE, itemClick);
    // Playlist item click function
    function itemClick (event:Event):void {
    channel.stop(); // stop play
    status_txt.text = "" + event.target.selectedItem.label + "";
    info_txt.text = "" + event.target.selectedItem.Info + "";
    imageLoader.MovieClip = "" + event.target.selectedItem.Movie +"";
    trackToPlay = event.target.selectedItem.songString;
    gotoAndPlay(2);
    }

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You can add the imageloader only once to the stage as well as the movieclip. You can only change the image by loading another image using myLoader.load(new URLRequest(event.target.selectedItem.Movie)); or imageLoader.myLoader etc.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    thank you for your help.
    its working now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center