Adolfram Posted April 7, 2020 Share Posted April 7, 2020 So I made a converter(youtube to mp3) app using node.js and hosted in an external url. But the problem is, playSound does not wait for my app to respond and naturally, it does not play the audio. What might be the cause of this problem? Link to comment
Moderators IIYAMA Posted April 7, 2020 Moderators Share Posted April 7, 2020 5 minutes ago, Adolfram said: But the problem is, playSound does not wait for my app to respond and naturally, it does not play the audio. What might be the cause of this problem? Good question. How did you program it so that it waits until it is finished downloading? And did you make sure the file is created after the response and not before? Link to comment
Adolfram Posted April 7, 2020 Author Share Posted April 7, 2020 1 minute ago, IIYAMA said: Good question. How did you program it so that it waits until it is finished downloading? And did you make sure the file is created after the response and not before? Hey man sorry for creating a topic before trying long enough. I have found a solution by using another npm package which serves as a streamer (https://www.npmjs.com/package/youtube-audio-stream for the reference). But yeah, I made sure I returned a response after the file was created and made sure of that. I needed to fire playSound twice for it to work. It is weird but I'm glad I didn't solve it in that way. Link to comment
Moderators IIYAMA Posted April 7, 2020 Moderators Share Posted April 7, 2020 (edited) 6 minutes ago, Adolfram said: Hey man sorry for creating a topic before trying long enough. I have found a solution by using another npm package which serves as a streamer (https://www.npmjs.com/package/youtube-audio-stream for the reference). But yeah, I made sure I returned a response after the file was created and made sure of that. I needed to fire playSound twice for it to work. It is weird but I'm glad I didn't solve it in that way. Did you flush the file write? https://wiki.multitheftauto.com/wiki/FileFlush Because it possible that the file hasn't been fully written and that a piece of it is still in your memory. Or are you fully streaming? Edited April 7, 2020 by IIYAMA Link to comment
Adolfram Posted April 7, 2020 Author Share Posted April 7, 2020 2 minutes ago, IIYAMA said: Did you flush the file write? https://wiki.multitheftauto.com/wiki/FileFlush Because it possible that the file hasn't been fully written and that a piece of it is still in your memory. Or are you fully streaming? I am sure I'm fully streaming. The response is ended after the download is complete. Do you want to see the node.js code? Link to comment
Moderators IIYAMA Posted April 7, 2020 Moderators Share Posted April 7, 2020 1 minute ago, Adolfram said: I am sure I'm fully streaming. The response is ended after the download is complete. Do you want to see the node.js code? Yup, I just failed to understand that it was for streaming and not fetching on a stream. In the past I created a custom html player, which sometimes failed because the meta data (...length of the audio...) wasn't sent quick enough. Which is not the same as your problem, but it could be related. Maybe a little delay could do the trick? Link to comment
Adolfram Posted April 7, 2020 Author Share Posted April 7, 2020 2 minutes ago, IIYAMA said: Yup, I just failed to understand that it was for streaming and not fetching on a stream. In the past I created a custom html player, which sometimes failed because the meta data (...length of the audio...) wasn't sent quick enough. Which is not the same as your problem, but it could be related. Maybe a little delay could do the trick? Where should I put the delay? Node.js or MTA?. How can I put delay on playSound? I didn't understand your suggestion sadly Link to comment
Moderators IIYAMA Posted April 7, 2020 Moderators Share Posted April 7, 2020 1 minute ago, Adolfram said: Where should I put the delay? Node.js or MTA?. How can I put delay on playSound? I didn't understand your suggestion sadly You can put the delay on either Node.js or MTA. Not sure what the best solution is. For MTA: setTimer(function () sound = playSound ( url, false, true ) end, 100, 1) Link to comment
Adolfram Posted April 7, 2020 Author Share Posted April 7, 2020 (edited) I mean I can try that but I doubt this will do any help because the converter starts after the request which happens to be the playSound function. Here is my node.js code to help better understanding: var http = require('http'), fileSystem = require('fs'), path = require('path'); const url = require('url'); var Downloader = require("./downloader"); var dl = new Downloader(); //Configure YoutubeMp3Downloader with your settings http.createServer(function (request, response) { const queryObject = url.parse(request.url, true).query; if (!queryObject['ytId']) { response.writeHead(404); response.end("File not found."); return; } var ytId = queryObject['ytId']; var filePath; if (fileSystem.existsSync(path.join("/opt/ytdl/mp3", `${ytId}.mp3`))) { console.log(`${ytId} already exists. not downloading`); filePath = path.join("/opt/ytdl/mp3", `${ytId}.mp3`); var stat = fileSystem.statSync(filePath); response.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Content-Length': stat.size }); var readStream = fileSystem.createReadStream(filePath); // We replaced all the event handlers with a simple call to readStream.pipe() readStream.once('end', function () { console.log("readstream ended"); fileSystem.unlink(filePath, function () { console.log(filePath + "downloaded and deleted"); }); }); readStream.pipe(response, {end: true}); } else { console.log(`download for ${ytId} begins`); dl.getMP3({ videoId: ytId, name: `${ytId}.mp3` }, function (err, res) { if (err) { response.writeHead(400); response.end(err); } else { console.log("Song was downloaded: " + res.file); filePath = (res.file); var stat = fileSystem.statSync(filePath); response.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Content-Length': stat.size }); var readStream = fileSystem.createReadStream(filePath); // We replaced all the event handlers with a simple call to readStream.pipe() readStream.once('end', function () { console.log("readstream ended"); fileSystem.unlink(filePath, function () { console.log(filePath + "downloaded and deleted"); }); }); readStream.pipe(response, {end: true}); } }); } }) .listen(2000); console.log("Yt downloder listening on :2000") Sorry for the noob code, I'm a .NET geek. Edited April 7, 2020 by Adolfram Link to comment
Moderators IIYAMA Posted April 7, 2020 Moderators Share Posted April 7, 2020 (edited) @Adolfram See event open in: https://nodejs.org/en/knowledge/advanced/streams/how-to-use-fs-create-read-stream/ In that example they pipe it when the readable stream is ready. Edited April 7, 2020 by IIYAMA Link to comment
Adolfram Posted April 7, 2020 Author Share Posted April 7, 2020 28 minutes ago, IIYAMA said: @Adolfram See event open in: https://nodejs.org/en/knowledge/advanced/streams/how-to-use-fs-create-read-stream/ In that example they pipe it when the readable stream is ready. let me see that. thanks. Link to comment
Adolfram Posted April 7, 2020 Author Share Posted April 7, 2020 I have burnt the last 3 brain cells I have left and am probably doing something terribly wrong. I will come back to this tomorrow. Thanks for your help @IIYAMA. 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now