Languages

Menu
Sites
Language
How to get audio files from the device for audio player

Hi,

     Through Content API i am not able to get audio files directly, in that we need to select folders and then to AUDIO files .

  Any one help me to get audio files to player and create songs list.

 

thank you in advance.

 

Responses

6 Replies
Palitsyna

Hello,

try to use this code:

    // Define the error callback for all the asynchronous calls
    function onError(response) {
        console.log( "The following error occurred: " + response.name);
    }
     
    // List of audio items on the phone.
    function mediaItemArray (contents) {
        console.log("Successfully retrieved the list of Audio items");
        for (var i=0; i < contents.length; i++) {
            console.log(i + ":" + contents[i].type + ":" + contents[i].title + ":" +contents[i].mimeType);
            console.log("album:" + contents[i].album);
            console.log("artists:" + contents[i].artists[0]);
            console.log("duration:" + contents[i].duration);
            console.log("playedTime :" + contents[i].playedTime);
            console.log("playCount :" + contents[i].playCount);
            console.log("Item copyright: " + contents[i].copyright);
            console.log("Item bitrate: " + contents[i].bitrate);
            console.log("Item trackNumber: " + contents[i].trackNumber);
            console.log("Item size: " + contents[i].size);
        }
    }
     
    // Retrieve the ContentManager interface instance using the tizen global object
    var manager = tizen.content;
     
    // Let's try to retrieve the Audio list.
    var contentType = "AUDIO";
    var filter = new tizen.AttributeFilter("type", "EXACTLY", contentType);
    console.log("Getting the list of audio files on the phone");
    manager.find(mediaItemArray, onError, null, filter);

 

Best Regards,

Svetlana Palitsyna

Supreeth Kumar

Hi,

thanks for your reply i used above code and its working but i have some issue with playing audio, i have created list of artist, title and albums whenever i click on item list URL is not loading..

 

       var itemsList = document.getElementById('playlist');
      var albumList = document.getElementById('albumlist');
      var artistList = document.getElementById('artistlist');
        
        console.log("Successfully retrieved the list of Audio items");
        for (var i=0; i < contents.length; i++) {
            console.log(i + ":" + contents[i].type + ":" + contents[i].title + ":" +contents[i].mimeType);
            console.log("album:" + contents[i].album);
            console.log("artists:" + contents[i].artists[0]);
            console.log("duration:" + contents[i].duration);
            console.log("playedTime :" + contents[i].playedTime);
            console.log("playCount :" + contents[i].playCount);
            console.log("Item copyright: " + contents[i].copyright);
            console.log("Item bitrate: " + contents[i].bitrate);
            console.log("Item trackNumber: " + contents[i].trackNumber);
            console.log("Item size: " + contents[i].size); 
          
          
            
            var listItem = document.createElement("li");   //to create title list
            var listalbum = document.createElement("li");   //to create albumlist
            var listartist = document.createElement("li");     //to create artist list

            // add the item text
            listItem.innerHTML = contents[i].title;
            listalbum.innerHTML = contents[i].album;
            listartist.innerHTML =  contents[i].artists[0];
           
           
            // add listItem to the listElement
            itemsList.appendChild(listItem);
            albumList.appendChild(listalbum);
            artistList.appendChild(listartist);
        } 
    }
  
    
    function getAudio(){
        
      
    // Retrieve the ContentManager interface instance using the tizen global object
    var manager = tizen.content;
     
    // Let's try to retrieve the Audio list.
    var contentType = "AUDIO";
    var filter = new tizen.AttributeFilter("type", "EXACTLY", contentType);
    console.log("Getting the list of audio files on the phone");
    manager.find(mediaItemArray, onError, null, filter);
    
    };

//initialising audio file

 function initialize(c) {
        alert(c.contentURL);
        var e = c.contentURI;
        var d = c.text();
        var b = c.attr("cover");
        var a = c.attr("artist");
        $(" .title").text(d);
        $(" .artist").text(a);
        $(" .cover").css("background-image", "url(data/" + b + ")");
        E = new Audio(e);
        E.addEventListener("timeupdate", function() {
            var f = parseInt(E.currentTime, 10);
            M.slider("value", f);
        });
        $("#playlist li").removeClass("active");
        c.addClass("active");
    }

//selecting list item 

    $("#playlist li").click(function() {
        PauseAudio();
        
        initialize($(this));
      
        PlayAudio();
    });

 

 

Please help me to resolve this problem

 

Palitsyna

Hello,

this is because you don't save URL, you save only information about title, artist and album. 

// Retrieve the ContentManager interface instance using the tizen global object
var manager = tizen.content;
var audio = document.getElementById("audio");

// Let's try to retrieve the Audio list.
var contentType = "AUDIO";
var filter = new tizen.AttributeFilter("type", "EXACTLY", contentType);
console.log("Getting the list of audio files on the phone");
manager.find(mediaItemArray, onError, null, filter);


function onError(response) {
    console.log( "The following error occurred: " + response.name);
}

var onclickFunction = function() {
	alert("clicked" + this.innerHTML);
	alert("URL= " + this.contentURL);
	
	audio.src = this.contentURL;
};

function mediaItemArray (contents) {
    var itemsList = document.getElementById('playlist');
    console.log("Successfully retrieved the list of Audio items");

    for (var i=0; i < contents.length; i++) {
        console.log(i + ":" + contents[i].type + ":" + contents[i].title + ":" +contents[i].mimeType);
        console.log("album:" + contents[i].album);
        console.log("artists:" + contents[i].artists[0]);
        console.log("duration:" + contents[i].duration);
        console.log("Item bitrate: " + contents[i].bitrate);
        console.log("Item trackNumber: " + contents[i].trackNumber);
        console.log("Item size: " + contents[i].size); 

        var listItem = document.createElement("li");   //to create title list
        listItem.innerHTML = contents[i].title;
        listItem.contentURL = contents[i].contentURI;
        listItem.onclick = onclickFunction;

        itemsList.appendChild(listItem);
    }
}

 

"audio" in this example is:

<audio id="audio" controls src="" autoplay></audio>

 

Regards,

Svetlana Palitsyna

Supreeth Kumar

Thank you.

i'll chek it out..

Vikram

Hi,

I write the below source code to play one audio file which I selected.

function audio(){
    var mimeType = "audio/mpeg";
	var uri = "file:///opt/usr/media/Sounds/Over the Horizon.mp3";	 
	var appControl = new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/view", uri, mimeType);    	
	tizen.application.launchAppControl(appControl, "tizen.musicplayer", function() {
	        console.log("launch appControl succeeded");
	    }, function(e) {
	        /* Error handling */
	    }, null);
}

 

Alex Dem

Hi,
fyi: In some cases pick operation could be useful:
https://developer.tizen.org/development/guides/web-application/tizen-features/application/application-0#file
with "audio/*" mime type.

But looks like you should use Content API as suggested by Svetlana:
https://developer.tizen.org/development/tutorials/web-application/tizen-features/content/content

Alexey.