jmf - How to play mov and wmv video format file in Java Swing? -
i using jmf jar file, able listen sound cant view movie.
unable handle format: rle , 800x600, framerate=14.6, length=31280 failed realize: com.sun.media.playbackengine@17590db error: unable realize com.sun.media.playbackengine@17590db
some error got when trying play video.
jmf basically dead
you can use xuggler play wmv files in java swing.
package com.xuggle.xuggler.demos; import com.xuggle.xuggler.demos.videoimage; import javax.sound.sampled.audioformat; import javax.sound.sampled.audiosystem; import javax.sound.sampled.dataline; import javax.sound.sampled.lineunavailableexception; import javax.sound.sampled.sourcedataline; import com.xuggle.xuggler.iaudiosamples; import com.xuggle.xuggler.icontainer; import com.xuggle.xuggler.ipacket; import com.xuggle.xuggler.istream; import com.xuggle.xuggler.istreamcoder; import com.xuggle.xuggler.icodec; public class decodeandplayaudio { private static sourcedataline mline; /** * takes media container (file) first argument, opens it, * opens default audio device on system, , plays audio. * * @param args must contain 1 string represents filename */ public static void main(string[] args) { if (args.length <= 0) throw new illegalargumentexception("must pass in filename first argument"); string filename = args[0]; // create xuggler container object icontainer container = icontainer.make(); // open container if (container.open(filename, icontainer.type.read, null) < 0) throw new illegalargumentexception("could not open file: " + filename); // query how many streams call open found int numstreams = container.getnumstreams(); // , iterate through streams find first audio stream int audiostreamid = -1; istreamcoder audiocoder = null; for(int = 0; < numstreams; i++) { // find stream object istream stream = container.getstream(i); // pre-configured decoder can decode stream; istreamcoder coder = stream.getstreamcoder(); if (coder.getcodectype() == icodec.type.codec_type_audio) { audiostreamid = i; audiocoder = coder; break; } } if (audiostreamid == -1) throw new runtimeexception("could not find audio stream in container: "+filename); /* * have found audio stream in file. let's open our decoder can * work. */ if (audiocoder.open() < 0) throw new runtimeexception("could not open audio decoder container: "+filename); /* * , once have that, ask java sound system ready. */ openjavasound(audiocoder); /* * now, start walking through container looking @ each packet. */ ipacket packet = ipacket.make(); while(container.readnextpacket(packet) >= 0) { /* * have packet, let's see if belongs our audio stream */ if (packet.getstreamindex() == audiostreamid) { /* * allocate set of samples same number of channels * coder tells in buffer. * * pass in buffer size (1024 in our example), although xuggler * allocate more space 1024 (it's not important why). */ iaudiosamples samples = iaudiosamples.make(1024, audiocoder.getchannels()); /* * packet can contain multiple sets of samples (or frames of samples * in audio-decoding speak). so, may need call decode audio multiple * times @ different offsets in packet's data. capture here. */ int offset = 0; /* * keep going until we've processed data */ while(offset < packet.getsize()) { int bytesdecoded = audiocoder.decodeaudio(samples, packet, offset); if (bytesdecoded < 0) throw new runtimeexception("got error decoding audio in: " + filename); offset += bytesdecoded; /* * decoder consume data in packet, not able construct * full set of samples yet. therefore should check if * got complete set of samples decoder */ if (samples.iscomplete()) { playjavasound(samples); } } } else { /* * packet isn't part of our audio stream, silently drop it. */ {} while(false); } } /* * technically since we're exiting anyway, these cleaned * garbage collector... because we're nice people , want * invited places christmas, we're going show how clean up. */ closejavasound(); if (audiocoder != null) { audiocoder.close(); audiocoder = null; } if (container !=null) { container.close(); container = null; } } private static void openjavasound(istreamcoder aaudiocoder) { audioformat audioformat = new audioformat(aaudiocoder.getsamplerate(), (int)iaudiosamples.findsamplebitdepth(aaudiocoder.getsampleformat()), aaudiocoder.getchannels(), true, /* xuggler defaults signed 16 bit samples */ false); dataline.info info = new dataline.info(sourcedataline.class, audioformat); try { mline = (sourcedataline) audiosystem.getline(info); /** * if succeeded, try opening line. */ mline.open(audioformat); /** * , if succeed, start line. */ mline.start(); } catch (lineunavailableexception e) { throw new runtimeexception("could not open audio line"); }
}
private static void playjavasound(iaudiosamples asamples) { /** * we're going dump samples line. */ byte[] rawbytes = asamples.getdata().getbytearray(0, asamples.getsize()); mline.write(rawbytes, 0, asamples.getsize()); } private static void closejavasound() { if (mline != null) { /* * wait line finish playing */ mline.drain(); /* * close line. */ mline.close(); mline=null; } } }
Comments
Post a Comment