最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript FileReader using a lot of memory - Stack Overflow

programmeradmin0浏览0评论

I have a problem with my little project. Every time the music player is loading new songs into playlist or you are pressing a song on the list to get it playing, it's using a lot of memory, and it stays high until you shut it down. I think its every time I'm using the filereader API that it uses memory, but I'm also loading ID3 information with the jDataView.js script which I also think is taking a lot of memory.

Do you guys have any suggestion, to load,store and play songs with the FileReader, without taking up memory? I've tried to see if it was possible to clear the fileReader after using, but I couldn't find anything. I've only tested in Chrome.

UPDATE: I have tested my project,and found out, that its when im trying to load the datastring it takes up memory.

reader.onloadend = function(evt) {
    if(typeof(e) != "undefined"){
        e.pause();
    }
    e = new Audio();
    e.src = evt.target.result; // evt.target.result call takes the memory
    e.setAttribute("type", songs[index]["file"].type);
    e.play();
    e.addEventListener("ended", function() { LoadAudioFile(index + 1) }, false);
};

Is there another way to load the data into the audio element?

I have a problem with my little project. Every time the music player is loading new songs into playlist or you are pressing a song on the list to get it playing, it's using a lot of memory, and it stays high until you shut it down. I think its every time I'm using the filereader API that it uses memory, but I'm also loading ID3 information with the jDataView.js script which I also think is taking a lot of memory.

Do you guys have any suggestion, to load,store and play songs with the FileReader, without taking up memory? I've tried to see if it was possible to clear the fileReader after using, but I couldn't find anything. I've only tested in Chrome.

UPDATE: I have tested my project,and found out, that its when im trying to load the datastring it takes up memory.

reader.onloadend = function(evt) {
    if(typeof(e) != "undefined"){
        e.pause();
    }
    e = new Audio();
    e.src = evt.target.result; // evt.target.result call takes the memory
    e.setAttribute("type", songs[index]["file"].type);
    e.play();
    e.addEventListener("ended", function() { LoadAudioFile(index + 1) }, false);
};

Is there another way to load the data into the audio element?

Share edited May 5, 2013 at 11:20 charmixer asked May 2, 2012 at 12:15 charmixercharmixer 6512 gold badges8 silver badges24 bronze badges 1
  • When you have such a problem, the first thing to do is to see where the memory is used. Have a look at Chrome's memory profiler : developers.google./chrome-developer-tools/docs/… – Denys Séguret Commented May 2, 2012 at 12:23
Add a ment  | 

1 Answer 1

Reset to default 11

This is not because of FileReader but because you are making the src attribute of audio element a 1.33 * mp3filesize string. So instead of the src attribute being a nice short url pointing to a mp3 resource, it's the whole mp3 file in base64 encoding. It's a wonder your browser didn't crash.

You should not read the file with FileReader at all, but create a blob URL from the file and use that as src.

var url = window.URL || window.webkitURL;

//Src will be like "blob:http%3A//stackoverflow./d13eb575-4863-4f86-8727-6400119f4afc"
//A very short string that is pointing to the original resource in hard drive

var src = url.createObjectURL( mp3filereference );

audioElement.src = src;
发布评论

评论列表(0)

  1. 暂无评论