c# - Using new Unity VideoPlayer and VideoClip API to play video -
movietexture deprecated after unity 5.6.0b1 release , new api plays video on both desktop , mobile devices released.
videoplayer , videoclip can used play video , retrieve texture each frame if needed.
i've managed video working coduldn't audio play as-well editor on windows 10. know why audio not playing?
//raw image show video images [assign editor] public rawimage image; //video play [assign editor] public videoclip videotoplay; private videoplayer videoplayer; private videosource videosource; //audio private audiosource audiosource; // use initialization void start() { application.runinbackground = true; startcoroutine(playvideo()); } ienumerator playvideo() { //add videoplayer gameobject videoplayer = gameobject.addcomponent<videoplayer>(); //add audiosource audiosource = gameobject.addcomponent<audiosource>(); //disable play on awake both video , audio videoplayer.playonawake = false; audiosource.playonawake = false; //we want play video clip not url videoplayer.source = videosource.videoclip; //set video play prepare audio prevent buffering videoplayer.clip = videotoplay; videoplayer.prepare(); //wait until video prepared while (!videoplayer.isprepared) { debug.log("preparing video"); yield return null; } debug.log("done preparing video"); //set audio output audiosource videoplayer.audiooutputmode = videoaudiooutputmode.audiosource; //assign audio video audiosource played videoplayer.enableaudiotrack(0, true); videoplayer.settargetaudiosource(0, audiosource); //assign texture video rawimage displayed image.texture = videoplayer.texture; //play video videoplayer.play(); //play sound audiosource.play(); debug.log("playing video"); while (videoplayer.isplaying) { debug.logwarning("video time: " + mathf.floortoint((float)videoplayer.time)); yield return null; } debug.log("done playing video"); }
found problem. below fixed code plays video , audio:
//raw image show video images [assign editor] public rawimage image; //video play [assign editor] public videoclip videotoplay; private videoplayer videoplayer; private videosource videosource; //audio private audiosource audiosource; // use initialization void start() { application.runinbackground = true; startcoroutine(playvideo()); } ienumerator playvideo() { //add videoplayer gameobject videoplayer = gameobject.addcomponent<videoplayer>(); //add audiosource audiosource = gameobject.addcomponent<audiosource>(); //disable play on awake both video , audio videoplayer.playonawake = false; audiosource.playonawake = false; //we want play video clip not url videoplayer.source = videosource.videoclip; //set audio output audiosource videoplayer.audiooutputmode = videoaudiooutputmode.audiosource; //assign audio video audiosource played videoplayer.enableaudiotrack(0, true); videoplayer.settargetaudiosource(0, audiosource); //set video play prepare audio prevent buffering videoplayer.clip = videotoplay; videoplayer.prepare(); //wait until video prepared while (!videoplayer.isprepared) { debug.log("preparing video"); yield return null; } debug.log("done preparing video"); //assign texture video rawimage displayed image.texture = videoplayer.texture; //play video videoplayer.play(); //play sound audiosource.play(); debug.log("playing video"); while (videoplayer.isplaying) { debug.logwarning("video time: " + mathf.floortoint((float)videoplayer.time)); yield return null; } debug.log("done playing video"); } why audio not playing:
//set audio output audiosource videoplayer.audiooutputmode = videoaudiooutputmode.audiosource; //assign audio video audiosource played videoplayer.enableaudiotrack(0, true); videoplayer.settargetaudiosource(0, audiosource); must called before videoplayer.prepare(); not after it. took hours of experiment find this problem having.
stuck @ "preparing video"?
wait 5 seconds after videoplayer.prepare(); called exit while loop.
replace:
while (!videoplayer.isprepared) { debug.log("preparing video"); yield return null; } with:
//wait until video prepared waitforseconds waittime = new waitforseconds(5); while (!videoplayer.isprepared) { debug.log("preparing video"); //prepare/wait 5 sceonds yield return waittime; //break out of while loop after 5 seconds wait break; } this should work may experience buffering when video starts playing. while using temporary fix, suggestion file bug title of "videoplayer.isprepared true" because bug.
some people fixed changing:
videoplayer.playonawake = false; audiosource.playonawake = false; to
videoplayer.playonawake = true; audiosource.playonawake = true; play video url:
replace:
//we want play video clip not url videoplayer.source = videosource.videoclip; with:
//we want play url videoplayer.source = videosource.url; videoplayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"; then remove:
public videoclip videotoplay; , videoplayer.clip = videotoplay; these not needed anymore.
all supported video formats:
- ogv
- vp8
- webm
- mov
- dv
- mp4
- m4v
- mpg
- mpeg
extra supported video formats on windows:
- avi
- asf
- wmf
some of these formats don't work on platforms. see this post more information on supported video formats.
Comments
Post a Comment