How do you create a javascript function to filter audio rss(podcast) for a project?

Asked

Viewed 279 times

1

I have a question regarding how to create a function where I can filter the audio(.mp3),with a maximum amount of 5 audios from the feed.

E titulo do áudio do feed RSS da parte do podcast da CBN: http://imagens.globoradio.globo.com/cbn/podcast/cardapios/tecnologia.xml

Yeah, in this case I was able to put the href, but I have to put it one by one, and then put the title of the podcast one by one that’s pulling. I am creating an accessibility website project for visually impaired with news feature via audio via podcast.

use an audio framework with playlist called: audiojs(Kolber.github.io/audiojs)imagem do projeto)

  • could simplify the explanation, not understood very well, you want to filter/download 5 audios from the CBN podcast ?

  • Exactly,Thiago. I want to filter the audio from the cbn podcast. What should I do ? Thank you. Doubt ,look at the audio framework I put in the question above. in the playlist part.

  • I got it Carlos, I’ve never really done it, but I’ll see if I can get something.

  • thanks,Thiago. Look I found a feature that I am currently trying to work to create this filter would google feed API. This already thank you.

  • really from what I saw I think this would be the best recuros Carlos

  • Okay, Thiago. I see it,.

  • Good evening, I wonder if the answer helped you? If not, inform might have had some doubt in the use of it.

  • Good evening,helped a lot.But I have a new challenge regarding the podcast how can I play the xml file, in a database (remembering that the function that pulls Feed is Ajax) to create a history of these news, and the player access this database, is plays the news.

Show 3 more comments

1 answer

1

The domain of .globo.com supports Cross-Origin, according to the header’s response when accessing the address http://imagens.globoradio.globo.com/cbn/podcast/cardapios/tecnologia.xml:

Access-Control-Allow-Headers:origin, x-requested-with, content-type
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*

Note the Access-Control-Allow-Origin:*, this means that any domain other than imagens.globoradio.globo.com you can use xml, so you can use ajax for this.

CORS is a requirement to access pages of different domains with ajax for example

In the example on the page http://kolber.github.io/audiojs/demos/test6.html it is necessary to create a playlist with to create the playlist, based on this I created a code in pure javascript to use with audio.js:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="audiojs/audio.min.js"></script>
    <style type="text/css">
    audio.hide {/*oculta o player padrao do browser*/
        display: none;
    }
    </style>
</head>
<body>

<audio class="hide" controls="controls"></audio>
<ol id="playlistitems"></ol>

<script type="text/javascript">
    function retrieveData(rss, maxItens) {/*extrai dados do feed*/
        var items = rss.getElementsByTagName("item");
        var current, title;
        var playList = [];
        var j = items.length;

        if (maxItens > 0 && maxItens < j) {
            j = maxItens;
        }

        for (var i = 0; i < j; i++) {
            current = items[i].getElementsByTagName("enclosure");
            title   = items[i].getElementsByTagName("title");

            if (current.length > 0) {
                playList.push({
                    "url": current[0].getAttribute("url"),
                    "title": title[0].textContent
                });
            }
        }

        return playList;
    }

    function audioJS(items, total) {/*cria o player com audio.js*/
        var allAudios, current = 0, audio;
        allAudios = audiojs.createAll({
            "trackEnded": function() {
                current++;
                if (current < total) {
                    audio.load(items[current].getAttribute("data-href"));
                    audio.play();
                }
            }
        });

        audio = allAudios[0];
        audio.load(items[0].getAttribute("data-href"));
        audio.play();

        for (var i = 0, j = items.length; i < j; i++) {
            items[i].onclick = function() {
                audio.load(this.getAttribute("data-href"));
                audio.play();
                return false;
            };
        }
    }

    function init(data, maxItens) {/*cria os links*/
        var playList = retrieveData(data, maxItens);
        var el, li, playListItems = document.getElementById("playlistitems");

        if (playList.length > 0) {
            for (var i = 0, j = playList.length; i < j; i++) {
                li = document.createElement("li");
                el = document.createElement("a");
                el.setAttribute("href", "#");
                el.setAttribute("data-href", playList[i].url);
                el.innerHTML = playList[i].title;

                li.appendChild(el);
                playListItems.appendChild(li);
            }
        }

        if (j > 0) {
            audioJS(playListItems.getElementsByTagName("a"), j);
        }
    }

    var ajax = new XMLHttpRequest();
    var url = "http://imagens.globoradio.globo.com/cbn/podcast/cardapios/tecnologia.xml";

    ajax.open("GET", url, true);
    ajax.onreadystatechange = function () {
        if (ajax.readyState === 4) {
            if (ajax.status === 200) {/*se tudo ok envia o feed pro evento init*/
                init(ajax.responseXML, 5);//O segundo parametro limita pra 5 itens no máximo
            } else {/*se falhar ao baixar o xml entao mostra o codigo do erro*/
                alert("Erro: " + ajax.status);
            }
        }
    };
    ajax.send(null);
</script>
</body>
</html>

This is just a simple example, if you want to customize more use the example of the link I mentioned (which uses jquery).

To change the maximum limit of items to be displayed to 3 for example, go inside ajax, change the second argument of the function init:

init(ajax.responseXML, 3);

To leave no limit remove the last parameter:

init(ajax.responseXML);
  • William, thank you. I checked your code, and yes this pulling the xml,but when I run it ,not going to the next track and I did not find the part where I can reduce the size of the playlist (limit-Ló for example:pull only 3 news), because the code it pulls all the xml from the page. This already Thank you, Thank you.

  • @Carloshenrique I’m sorry I forgot this, I edited the answer, see if it helps.

  • Now I understand that the Ajax function manages the filtering of the Feed,Thank you. But in case the player goes to the next audio of the Feed(next function), in the case of audiojs have a variable next will be that is what is missing in the code. For when the player finishes, it does not go to the next audio , returns to the same, as if it were a loop.

  • @Carloshenrique you mean in the last audio?

  • It rotates,when the audio is running out,it returns in the same.

  • @Carloshenrique Fixed the code :)

  • @Carloshenrique is missing some functions in your code. You mixed up two things and you included some things manually, I didn’t quite get where you’re going with this.

  • this is the html structure of the:http://bit.ly/1NG4nJy. project so I would insert the base of this code into the div of each subject. Thanks for that. Anything, put in doubt,.

  • Before you answer this question in the stack, I was manually putting the feed in the player div so that the same pulls up to touch. But now through his help, to be able to automate that.

  • You saw the structure, in the part of the player div(<div id="wrapper">), just below appear the audio tag Html5 with Preload="auto", in case I can keep it . to fully automated my player as it ended that news, goes to the next.so successively.

  • @Carloshenrique you saw that I fixed the code, it was a trackEnd problem, it was like this: "trackEnded": function() {&#xA;current++;&#xA;if (current < total) {&#xA;audio.load(items[0].getAttribute("data-href"));&#xA;audio.play();&#xA;}&#xA;}, but what was right was this: "trackEnded": function() {&#xA;current++;&#xA;if (current < total) {&#xA;audio.load(items[current].getAttribute("data-href"));&#xA;audio.play();&#xA;}&#xA;}, i tested and it worked, as soon as an audio track ends it goes to next item.

  • I made the Feed Changes ,and the CSS to make it responsive. (https://jsfiddle.net/6xops9cu)

  • @Carloshenrique I’m sorry, I don’t understand, I thought your problem was with linking the RSS with the <audio> and audio.js, I don’t understand what has responsiveness or css to do with the problem, I actually believe this is more a matter of "aesthetics" and not the problem. Could you explain to me exactly what’s missing for you?

  • First, I’m sorry for the confusion. And yes the problem of linking RSS with audiojs ,I made the appropriate changes,It worked. This already Thank you, Thank you.

  • @Carloshenrique has no problem, I just want to know if this worked for you. If something is missing let me know. See you.

  • It worked.Thank you.

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.