Get Adobe Flash player

How To Read iTunes and Other Podcasts with Action Script 3.0

November 16th, 2010 | Comments Off
category: General News, Products, Software

If you have ever had issues reading namespaces and attributes in XML files with action script 3.0, then this post is for you. Recently I made a simple iTunes podcast reader that plays the MP3 associated with the file. Enough jargon, here is the code. view and/or download the project

code for XML this code handles all podcast importing… may need to change the nodes based on the podcast you are using

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// declare a string to put your xml file location in
var xmlLocation:String;

// allow for FlashVar to set the xml file location (update the xml declaration without opening flash)
if(this.loaderInfo.parameters.jxmlLocation == undefined)
{
    // if FlashVar is not set load a default xml file
    xmlLocation = "itunes_audio.rss"; //
}
else
{
    // if FlashVar is set, load the xml file from the FlashVar
    xmlLocation = "" + this.loaderInfo.parameters.jxmlLocation;
}

// query counters
var b:Number = 0;
var q_id:Number = 0;

// uncomment for browser querying (query an xml node directly from browser - i.e. http://www.yourdomain.com/player.html?q_id=3)
if(root.loaderInfo.parameters.q_id > 0)
{
    q_id = root.loaderInfo.parameters.q_id;
}

// here we handle the namespace declarations from itunes
var itunes:Namespace = new Namespace("http://www.itunes.com/dtds/podcast-1.0.dtd");

// here we set up arrays to for each xml node
var itemTitles:Array = [];
var itemAuthor:Array = [];
var itemSubtitle:Array = [];
var itemSummary:Array = [];
var itemEnclosureURL:Array = [];
var itemEnclosureLength:Array = [];
var itemEnclosureType:Array = [];
var itemGuid:Array = [];
var itemPubDate:Array = [];
var itemDuration:Array = [];
var itemKeywords:Array = [];

// xml handler
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var xmlURLLoader:URLLoader = new URLLoader();
var xmlURLRequest:URLRequest = new URLRequest(xmlLocation);
xmlURLLoader.load(xmlURLRequest);

xmlURLLoader.addEventListener(Event.COMPLETE, dataLoaded);
var theXMLData:XML;
function dataLoaded(event:Event):void
{
    theXMLData = new XML(xmlURLLoader.data);
    var a:Number = theXMLData.channel.item.length();
   
    //trace(theXMLData);
   
    // display channel information
    channelTitleTB.htmlText = theXMLData.channel.title;
    channelLinkTB.htmlText = theXMLData.channel.link;
    channelLanguageTB.htmlText = theXMLData.channel.language;
    channelCopyrightTB.htmlText = theXMLData.channel.copyright;
    channelDescriptionTB.htmlText = theXMLData.channel.description;
    channelSubtitleTB.htmlText = theXMLData.channel.itunes::subtitle;
    channelAuthorTB.htmlText = theXMLData.channel.itunes::author;
    channelSummaryTB.htmlText = theXMLData.channel.itunes::summary;
    channelOwnerNameTB.htmlText = theXMLData.channel.itunes::owner.itunes::name;
    channelOwnerEmailTB.htmlText = theXMLData.channel.itunes::owner.itunes::email;
    channelImageTB.htmlText = theXMLData.channel.itunes::image.@href;
    channelCategoryTB.htmlText = theXMLData.channel.itunes::category.@text[0] + " | " + theXMLData.channel.itunes::category.itunes::category.@text[0] + " | " + theXMLData.channel.itunes::category.@text[1];
   
    // push all item elements into the above set arrays
    while (b < a)
    {
        itemTitles.push(theXMLData.channel.item.title[b]);
        itemAuthor.push(theXMLData.channel.item.itunes::author[b]);
        itemSubtitle.push(theXMLData.channel.item.itunes::subtitle[b]);
        itemSummary.push(theXMLData.channel.item.itunes::summary[b]);
       
        itemEnclosureURL.push(theXMLData.channel.item.enclosure.@url[b]);
        itemEnclosureLength.push(theXMLData.channel.item.enclosure.@length[b]);
        itemEnclosureType.push(theXMLData.channel.item.enclosure.@type[b]);
        itemGuid.push(theXMLData.channel.item.guid[b]);
        itemPubDate.push(theXMLData.channel.item.pubDate[b]);
        itemDuration.push(theXMLData.channel.item.itunes::duration[b]);
        itemKeywords.push(theXMLData.channel.item.itunes::keywords[b]);
       
        b++;
    }
   
    // display each item block
    itemTitleTB.htmlText = itemTitles[q_id];
    itemAuthorTB.htmlText = itemAuthor[q_id];
    itemSubtitleTB.htmlText = itemSubtitle[q_id];
    itemSummaryTB.htmlText = itemSummary[q_id];
    itemEnclosureTB.htmlText = itemEnclosureURL[q_id] + " | " + itemEnclosureLength[q_id] + " | " + itemEnclosureType[q_id];
    itemGuidTB.htmlText = itemGuid[q_id];
    itemPubDateTB.htmlText = itemPubDate[q_id];
    itemDurationTB.htmlText = itemDuration[q_id];
    itemKeywordsTB.htmlText = itemKeywords[q_id];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// action for next button
nextBtn.label = "Next";
nextBtn.addEventListener(MouseEvent.CLICK, next_q_id);
function next_q_id(e:MouseEvent):void
{
    q_id++;
   
    itemTitleTB.htmlText = itemTitles[q_id];
    itemAuthorTB.htmlText = itemAuthor[q_id];
    itemSubtitleTB.htmlText = itemSubtitle[q_id];
    itemSummaryTB.htmlText = itemSummary[q_id];
    itemEnclosureTB.htmlText = itemEnclosureURL[q_id] + " | " + itemEnclosureLength[q_id] + " | " + itemEnclosureType[q_id];
    itemGuidTB.htmlText = itemGuid[q_id];
    itemPubDateTB.htmlText = itemPubDate[q_id];
    itemDurationTB.htmlText = itemDuration[q_id];
    itemKeywordsTB.htmlText = itemKeywords[q_id];
   
    prevBtn.visible = true;
   
    // specific for changing the audio reference (not needed if you decide to not play the audio)
    changeAudio();
   
    if(q_id == itemTitles.length - 1)
    {
        nextBtn.visible = false;
    }
}
prevBtn.visible = false;
prevBtn.label = "Previous";
prevBtn.addEventListener(MouseEvent.CLICK, prev_q_id);
function prev_q_id(e:MouseEvent):void
{
    q_id--;
   
    itemTitleTB.htmlText = itemTitles[q_id];
    itemAuthorTB.htmlText = itemAuthor[q_id];
    itemSubtitleTB.htmlText = itemSubtitle[q_id];
    itemSummaryTB.htmlText = itemSummary[q_id];
    itemEnclosureTB.htmlText = itemEnclosureURL[q_id] + " | " + itemEnclosureLength[q_id] + " | " + itemEnclosureType[q_id];
    itemGuidTB.htmlText = itemGuid[q_id];
    itemPubDateTB.htmlText = itemPubDate[q_id];
    itemDurationTB.htmlText = itemDuration[q_id];
    itemKeywordsTB.htmlText = itemKeywords[q_id];
   
    nextBtn.visible = true;
   
    // specific for changing the audio reference (not needed if you decide to not play the audio)
    changeAudio();
   
    if(q_id == 0)
    {
        q_id = 0;
        prevBtn.visible = false;
    }
}

code for audio player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPECIFIC CODE FOR PLAYING THE AUDIO FROM THE ITUNES FILE

// check to see if this is the first launch of the mp3 Player
var firstLaunch:Boolean = true;

// reset the audio player for next song
function changeAudio()
{
    if(theAudioPlayer.visible == true && firstLaunch == false)
    {
        with(theAudioPlayer)
        {
            soundChannel.stop(); //stop audio
            songPlaying = false;
            isPlaying = false;
            play_btn.visible = true;
            pause_btn.visible = false;
        }
    }
   
    theAudioPlayer.visible = false;
    playAudioBtn.visible = true;
}

// handler for play audio btn
playAudioBtn.label = "Play Audio";
theAudioPlayer.visible = false;
playAudioBtn.addEventListener(MouseEvent.CLICK, playTheAudio);
function playTheAudio(e:MouseEvent):void
{
    theAudioPlayer.visible = true;
    playAudioBtn.visible = false;
   
    with(theAudioPlayer)
    {
        // get the correct song path from the itunes file
        var thisReq:URLRequest = new URLRequest(theXMLData.channel.item.enclosure.@url[q_id]);
        var thisTitle:Sound = new Sound(thisReq);
       
        // if this is the players first launch, this handle the initial loading of the mp3 files
        if(firstLaunch == true)
        {
            q_id = 0;
            music.load(thisReq);//load music
            if(songPlaying == false)
            {
                soundChannel = thisTitle.play();
                songPlaying = true;
                isPlaying = true;
            }
        }
        else
        {
            // if this is not the initial load of the mp3 files this is reset the player and play the next song
            currentSound = thisTitle;
       
            soundChannel.stop();   
            songPlaying = false;
            isPlaying = false;
            tBar.tBarKnob.x = 1;
            pos = 0;
            displayTime.text = "00:00/00:00";
            isPlaying = true;
            soundChannel = thisTitle.play();
            songPlaying = true;
        }
       
        firstLaunch = false; // after player has been launched this will prevent the initial launch code from running again
        theAudioPlayer.addEventListener(Event.ENTER_FRAME, OnEnterFrame); // starts the sound tracking (seekbar and time)
    }
}
GD Star Rating
loading...
How To Read iTunes and Other Podcasts with Action Script 3.0, 4.0 out of 5 based on 2 ratings


Comments are closed.



Content

Need to pay for services?


Banner    

Contact me


  

Socially you can find me here


                 

        


Need to pick up a few things before you leave?


Grasshopper - The Entrepreneur's Phone System   Affordable Premium Custom Flash Photo and Video Gallery Templates
2010 Copyright © Todd Vaughn. All rights reserved. Register