java - Need Help Accessing AudioInputStream Outside of For Loop -
how declare/access appendedfiles audioinputstream data write disk outside of loop? when place disk writing command in loop, works intended, outside doesn't recognize appendedfiles.
import java.io.file; import java.io.ioexception; import java.io.sequenceinputstream; import javax.sound.sampled.audiofileformat; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.unsupportedaudiofileexception; public class wavefilemerge { public void wavefilemerge(string[] filelist) throws unsupportedaudiofileexception, ioexception { for(int x=0;x<filelist.length;x++){ if(filelist[x] != null){ audioinputstream currentclip = audiosystem.getaudioinputstream(new file(filelist[x])); audioinputstream blankaudio = audiosystem.getaudioinputstream(new file("c:\\test\\dummy.wav")); audioinputstream appendedfiles = new audioinputstream( new sequenceinputstream(currentclip, blankaudio), blankaudio.getformat(), blankaudio.getframelength() + currentclip.getframelength()); } } audiosystem.write(appendedfiles,audiofileformat.type.wave, new file("c:\\test\\final.wav")); } }
your problem 1 of variable scope. variables declared between pair of curly braces ({ , }) cannot accessed outside braces. if want access appendedfiles outside for loop, have declare outside of loop. don't, however, have use variable declare it. can set null when initialize , use later. code, like:
audioinputstream appendedfiles = null; //declare variable outside loop. for(int x=0;x<filelist.length;x++){ if(filelist[x] != null){ audioinputstream currentclip = audiosystem.getaudioinputstream(new file(filelist[x])); audioinputstream blankaudio = audiosystem.getaudioinputstream(new file("c:\\test\\dummy.wav")); appendedfiles = new audioinputstream( new sequenceinputstream(currentclip, blankaudio), blankaudio.getformat(), blankaudio.getframelength() + currentclip.getframelength()); //set within loop } } audiosystem.write(appendedfiles,audiofileformat.type.wave, new file("c:\\test\\final.wav")); //you can still reference outside loop. warning
you should careful, however, writing file outside loop intend. if write outside loop, write whatever last value of appendedfiles loop. not want (otherwise, why loop , create other objects?).
what want
if rather use 1 loop create input streams, , write them all, need loop. in case, code create array or list of audioinputstreams, append in first loop, , write them out in separate loop afterwards. guess want. following code:
sequenceinputstream streamforappendedfiles = null; audioinputstream blankaudio = null; long totalframelength = 0l; //loop through files, adding input streams them go. for(int x=0;x<filelist.length;x++){ if(filelist[x] != null){ audioinputstream currentclip = audiosystem.getaudioinputstream(new file(filelist[x])); blankaudio = audiosystem.getaudioinputstream(new file("c:\\file\\with\\no\\audio.wav")); totalframelength+= currentclip.getframelength() + blankaudio.getframelength(); sequenceinputstream currentclipplusblank = new sequenceinputstream(currentclip,blankaudio); if(streamforappendedfiles==null){ //first time through loop, stream current clip streamforappendedfiles = currentclipplusblank; } else { //for subsequent loops, add current stream running total. think of +=. streamforappendedfiles = new sequenceinputstream(streamforappendedfiles,currentclipplusblank); } } } //convert stream audio stream audioinputstream audiostreamappendedfiles = new audioinputstream( streamforappendedfiles, blankaudio.getformat(), totalframelength); //write file. file finalfile = new file("c:\\test\\final.wav"); audiosystem.write(audiostreamappendedfiles,audiofileformat.type.wave,finalfile);
Comments
Post a Comment