MediaPlayerCtrl.cs 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine.UI;
  8. #if !UNITY_WEBPLAYER && !UNITY_WEBGL && !UNITY_WP8 && !UNITY_WP8_1
  9. using FFmpeg.AutoGen;
  10. using System.Threading;
  11. #endif
  12. [Serializable]
  13. public class VideoDate
  14. {
  15. public string Url;
  16. public int TimeSec;
  17. }
  18. #if UNITY_EDITOR || UNITY_STANDALONE
  19. public unsafe class MediaPlayerCtrl : MonoBehaviour
  20. {
  21. #else
  22. public class MediaPlayerCtrl : MonoBehaviour
  23. {
  24. #endif
  25. public static MediaPlayerCtrl Instance;
  26. public MeshRenderer rend;
  27. public VideoDate CurVideo;
  28. public float CurTimeVideo;
  29. public string m_strFileName;
  30. public GameObject[] m_TargetMaterial = null;
  31. private Texture2D m_VideoTexture = null;
  32. private Texture2D m_VideoTextureDummy = null;
  33. private Texture2D m_VideoTextureY;
  34. private Texture2D m_VideoTextureU;
  35. private Texture2D m_VideoTextureV;
  36. private Texture2D m_VideoTextureDummyY;
  37. private Texture2D m_VideoTextureDummyU;
  38. private Texture2D m_VideoTextureDummyV;
  39. private MEDIAPLAYER_STATE m_CurrentState;
  40. private int m_iCurrentSeekPosition;
  41. private int m_iWidth;
  42. private int m_iHeight;
  43. private float m_fVolume = 1.0f;
  44. public float m_fSpeed = 1.0f;
  45. public List<VideoDate> CashUrlVideoAdDurex = new List<VideoDate>();
  46. public List<VideoDate> CashUrlVideoAdLifja = new List<VideoDate>();
  47. public int CurIndexLifja = 0;
  48. public int CurIndexDurex = 0;
  49. public int CurCashIndexLifja = 0;
  50. public int CurCashIndexDurex = 0;
  51. public List<VideoDate> VideoDataDurex;
  52. public List<string> VideoUrls;
  53. public List<VideoDate> VideoDataDurexDenmark;
  54. public List<VideoDate> VideoDataLifja;
  55. public List<VideoDate> VideoDataLifjaDenmark;
  56. public bool m_bFullScreen = false;//Please use only in FullScreen prefab.
  57. public bool m_bSupportRockchip = true; //Using a device support Rochchip or Low-end devices
  58. //(Reason 1 : Not directly play in StreamingAssets)
  59. //(Reason 2 : Video buffer is RGB565 only supported)
  60. public bool m_bPC_FastMode = false;
  61. public delegate void VideoEnd();
  62. public delegate void VideoReady();
  63. public delegate void VideoError(MEDIAPLAYER_ERROR errorCode, MEDIAPLAYER_ERROR errorCodeExtra);
  64. public delegate void VideoFirstFrameReady();
  65. public delegate void VideoResize();
  66. public VideoResize OnResize;
  67. public VideoReady OnReady;
  68. public VideoEnd OnEnd;
  69. public VideoError OnVideoError;
  70. public VideoFirstFrameReady OnVideoFirstFrameReady;
  71. private IntPtr m_texPtr;
  72. private IntPtr m_texPtrY;
  73. private IntPtr m_texPtrU;
  74. private IntPtr m_texPtrV;
  75. #if UNITY_IPHONE || UNITY_TVOS
  76. private int m_iPauseFrame;
  77. #endif
  78. #if UNITY_ANDROID && !UNITY_EDITOR && UNITY_5
  79. [DllImport ("BlueDoveMediaRender")]
  80. private static extern void InitNDK();
  81. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  82. [DllImport ("BlueDoveMediaRender")]
  83. private static extern IntPtr EasyMovieTextureRender();
  84. #endif
  85. #endif
  86. #if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN)
  87. [DllImport("EasyMovieTexture")]
  88. private static extern int SetTexture();
  89. [DllImport("EasyMovieTexture")]
  90. private static extern void ReleaseTexture(int iID);
  91. [DllImport("EasyMovieTexture")]
  92. private static extern void SetTextureFromUnity(int iID, System.IntPtr texture, System.IntPtr textureY, System.IntPtr textureU, System.IntPtr textureV, int w, int h, byte[] data);
  93. [DllImport("EasyMovieTexture")]
  94. private static extern IntPtr GetRenderEventFunc();
  95. #endif
  96. #if (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX)
  97. [DllImport ("EasyMovieTextureRender")]
  98. private static extern int SetTexture();
  99. [DllImport ("EasyMovieTextureRender")]
  100. private static extern void ReleaseTexture(int iID);
  101. [DllImport ("EasyMovieTextureRender")]
  102. private static extern void SetTextureFromUnity(int iID,System.IntPtr texture,System.IntPtr textureY,System.IntPtr textureU,System.IntPtr textureV, int w, int h,byte[] data);
  103. [DllImport("EasyMovieTextureRender")]
  104. private static extern IntPtr GetRenderEventFunc();
  105. #endif
  106. private int m_iAndroidMgrID;
  107. private bool m_bIsFirstFrameReady;
  108. public enum MEDIAPLAYER_ERROR
  109. {
  110. MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
  111. MEDIA_ERROR_IO = -1004,
  112. MEDIA_ERROR_MALFORMED = -1007,
  113. MEDIA_ERROR_TIMED_OUT = -110,
  114. MEDIA_ERROR_UNSUPPORTED = -1010,
  115. MEDIA_ERROR_SERVER_DIED = 100,
  116. MEDIA_ERROR_UNKNOWN = 1
  117. }
  118. public enum MEDIAPLAYER_STATE
  119. {
  120. NOT_READY = 0,
  121. READY = 1,
  122. END = 2,
  123. PLAYING = 3,
  124. PAUSED = 4,
  125. STOPPED = 5,
  126. ERROR = 6
  127. }
  128. public enum MEDIA_SCALE
  129. {
  130. SCALE_X_TO_Y = 0,
  131. SCALE_X_TO_Z = 1,
  132. SCALE_Y_TO_X = 2,
  133. SCALE_Y_TO_Z = 3,
  134. SCALE_Z_TO_X = 4,
  135. SCALE_Z_TO_Y = 5,
  136. SCALE_X_TO_Y_2 = 6,
  137. }
  138. public bool m_bFirst = false;
  139. public MEDIA_SCALE m_ScaleValue;
  140. public GameObject[] m_objResize = null;
  141. public bool m_bLoop = false;
  142. public bool m_bAutoPlay = true;
  143. private bool m_bStop = false;
  144. private bool m_bInit = false;
  145. #if !UNITY_WEBPLAYER && !UNITY_WEBGL && !UNITY_WP8 && !UNITY_WP8_1
  146. static MediaPlayerCtrl()
  147. {
  148. #if UNITY_EDITOR
  149. String currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
  150. String dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins";
  151. if (currentPath.Contains(dllPath) == false)
  152. {
  153. Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  154. }
  155. dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x64";
  156. if (currentPath.Contains(dllPath) == false)
  157. {
  158. Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  159. }
  160. dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x86";
  161. if (currentPath.Contains(dllPath) == false)
  162. {
  163. Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  164. }
  165. #endif
  166. }
  167. void Awake()
  168. {
  169. Instance = this;
  170. #if UNITY_5 && UNITY_ANDROID
  171. #endif
  172. if (PlayerPrefs.GetInt("Lifja") == 0)
  173. {
  174. PlayerPrefs.SetInt("Lifja", 1);
  175. random++;
  176. }
  177. else
  178. {
  179. PlayerPrefs.SetInt("Lifja", 0);
  180. }
  181. if (SystemInfo.deviceModel.Contains("rockchip"))
  182. {
  183. m_bSupportRockchip = true;
  184. }
  185. else
  186. {
  187. m_bSupportRockchip = false;
  188. }
  189. #if UNITY_STANDALONE
  190. String currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
  191. String dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins";
  192. if(currentPath.Contains(dllPath) == false)
  193. {
  194. Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  195. }
  196. #endif
  197. if (SystemInfo.deviceModel.Contains("rockchip"))
  198. {
  199. m_bSupportRockchip = true;
  200. }
  201. else
  202. {
  203. m_bSupportRockchip = false;
  204. }
  205. #if UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR || UNITY_STANDALONE
  206. if (m_TargetMaterial != null)
  207. {
  208. for (int iIndex = 0; iIndex < m_TargetMaterial.Length; iIndex++)
  209. {
  210. if (m_TargetMaterial[iIndex] != null)
  211. {
  212. if (m_TargetMaterial[iIndex].GetComponent<MeshFilter>() != null)
  213. {
  214. Vector2[] vec2UVs = m_TargetMaterial[iIndex].GetComponent<MeshFilter>().mesh.uv;
  215. for (int i = 0; i < vec2UVs.Length; i++)
  216. {
  217. vec2UVs[i] = new Vector2(vec2UVs[i].x, 1.0f - vec2UVs[i].y);
  218. }
  219. m_TargetMaterial[iIndex].GetComponent<MeshFilter>().mesh.uv = vec2UVs;
  220. }
  221. if (m_TargetMaterial[iIndex].GetComponent<RawImage>() != null)
  222. {
  223. m_TargetMaterial[iIndex].GetComponent<RawImage>().uvRect = new Rect(0, 1, 1, -1);
  224. }
  225. }
  226. }
  227. }
  228. #endif
  229. #if UNITY_STANDALONE || UNITY_EDITOR
  230. //RegisterDebugCallback(new DebugCallback(DebugMethod));
  231. //threadVideo = new Thread(ThreadUpdate);
  232. //threadVideo.Start();
  233. #endif
  234. #if UNITY_ANDROID && !UNITY_EDITOR
  235. #if UNITY_5
  236. if( SystemInfo.graphicsMultiThreaded == true)
  237. InitNDK();
  238. #endif
  239. m_iAndroidMgrID = Call_InitNDK();
  240. #endif
  241. Call_SetUnityActivity();
  242. }
  243. //private void DelegateCallBack(int num)
  244. //{
  245. // Debug.Log(num);
  246. //}
  247. // Use this for initialization
  248. // Use this for initialization
  249. void Start()
  250. {
  251. #if UNITY_ANDROID
  252. if (Application.dataPath.Contains(".obb"))
  253. {
  254. Call_SetSplitOBB(true, Application.dataPath);
  255. }
  256. else
  257. {
  258. Call_SetSplitOBB(false, null);
  259. }
  260. #endif
  261. m_bInit = true;
  262. }
  263. void OnApplicationQuit()
  264. {
  265. if( System.IO.Directory.Exists( Application.persistentDataPath + "/Data" ) == true)
  266. System.IO.Directory.Delete( Application.persistentDataPath + "/Data", true);
  267. }
  268. bool m_bCheckFBO = false;
  269. public int random;
  270. void OnDisable()
  271. {
  272. if (GetCurrentState() == MEDIAPLAYER_STATE.PLAYING)
  273. {
  274. Pause();
  275. }
  276. }
  277. void OnEnable()
  278. {
  279. if (GetCurrentState() == MEDIAPLAYER_STATE.PAUSED)
  280. {
  281. Play();
  282. }
  283. }
  284. void Update()
  285. {
  286. if (m_bFirst == false)
  287. {
  288. #if UNITY_IPHONE
  289. if (CurIndexDurex > VideoDataDurex.Count - 1)
  290. {
  291. CurIndexDurex = 0;
  292. }
  293. Debug.Log("CurIndexDurex " + CurIndexDurex);
  294. m_strFileName = VideoDataDurex[CurIndexDurex].Url;
  295. MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo = VideoDataDurex[CurIndexDurex];
  296. if (CurIndexDurex > VideoDataDurex.Count - 1)
  297. {
  298. Debug.Log("(CurIndexDurex > VideoDataDurex.Count ");
  299. CurIndexDurex = 0;
  300. }
  301. else
  302. {
  303. CurIndexDurex++;
  304. Debug.Log("CurIndexDurex <= VideoDataDurex.Count" + CurIndexDurex);
  305. }
  306. Debug.Log("Play " + m_strFileName);
  307. string strName = m_strFileName.Trim();
  308. if (strName.StartsWith("http",StringComparison.OrdinalIgnoreCase))
  309. {
  310. StartCoroutine( DownloadStreamingVideoAndLoad(strName, MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo.TimeSec ));
  311. }
  312. else
  313. {
  314. Call_Load(strName,0);
  315. }
  316. #elif UNITY_ANDROID
  317. {
  318. // random++;
  319. //if (random % 2 == 0)
  320. {
  321. //var rand = UnityEngine.Random.Range(0, VideoDataDurex.Count-1);
  322. if (CurIndexDurex > VideoDataDurex.Count - 1)
  323. {
  324. CurIndexDurex = 0;
  325. }
  326. Debug.Log("CurIndexDurex " + CurIndexDurex);
  327. m_strFileName = VideoDataDurex[CurIndexDurex].Url;
  328. MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo = VideoDataDurex[CurIndexDurex];
  329. if (CurIndexDurex > VideoDataDurex.Count - 1)
  330. {
  331. Debug.Log("(CurIndexDurex > VideoDataDurex.Count ");
  332. CurIndexDurex = 0;
  333. }
  334. else
  335. {
  336. CurIndexDurex++;
  337. Debug.Log("CurIndexDurex <= VideoDataDurex.Count" + CurIndexDurex);
  338. }
  339. //Debug.Log ("random%2");
  340. }
  341. //else
  342. //{
  343. // if (CurIndexLifja > VideoDataLifjaDenmark.Count - 1)
  344. // {
  345. // CurIndexLifja = 0;
  346. // }
  347. // //Debug.Log("CurIndexLifja " + CurIndexLifja);
  348. // //var rand = UnityEngine.Random.Range(0, VideoDataLifja.Count-1);
  349. // m_strFileName = VideoDataLifjaDenmark[CurIndexLifja].Url;
  350. // MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo = VideoDataLifjaDenmark[CurIndexLifja];
  351. // if (CurIndexLifja > VideoDataLifjaDenmark.Count - 1)
  352. // {
  353. // Debug.Log("(CurIndexLifja > VideoDataLifja.Count - 1 ");
  354. // CurIndexLifja = 0;
  355. // }
  356. // else
  357. // {
  358. // Debug.Log("CurIndexLifja <= VideoDataLifja.Count" + CurIndexLifja);
  359. // CurIndexLifja++;
  360. // }
  361. // //Debug.Log ("random%1");
  362. //}
  363. }
  364. Debug.Log("Play " + m_strFileName);
  365. string strName = m_strFileName.Trim();
  366. //if (strName.StartsWith("http",StringComparison.OrdinalIgnoreCase))
  367. //{
  368. StartCoroutine(DownloadStreamingVideoAndLoad(strName, MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo.TimeSec));
  369. //}
  370. //else
  371. //{
  372. // Call_Load(strName,0);
  373. //}
  374. /*if (m_bSupportRockchip)
  375. {
  376. Call_SetRockchip(m_bSupportRockchip);
  377. if(strName.Contains("://"))
  378. {
  379. Call_Load(strName,0);
  380. }
  381. else
  382. {
  383. Call_Load(strName,0);
  384. StartCoroutine( CopyStreamingAssetVideoAndLoad(strName));
  385. }
  386. }
  387. else
  388. {
  389. Call_Load(strName,0);
  390. }*/
  391. #elif UNITY_STANDALONE
  392. Call_Load(strName,0);
  393. #endif
  394. //Call_SetLooping(m_bLoop);
  395. m_bFirst = true;
  396. }
  397. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  398. {
  399. if (m_bCheckFBO == false)
  400. {
  401. if (Call_GetVideoWidth() <= 0 || Call_GetVideoHeight() <= 0)
  402. {
  403. return;
  404. }
  405. m_iWidth = Call_GetVideoWidth();
  406. m_iHeight = Call_GetVideoHeight();
  407. Resize();
  408. if (m_VideoTexture != null)
  409. {
  410. //Destroy(m_VideoTexture);
  411. if (m_VideoTextureDummy != null)
  412. {
  413. Destroy(m_VideoTextureDummy);
  414. m_VideoTextureDummy = null;
  415. }
  416. m_VideoTextureDummy = m_VideoTexture;
  417. m_VideoTexture = null;
  418. }
  419. if (m_VideoTextureY != null)
  420. {
  421. if (m_VideoTextureDummyY != null)
  422. {
  423. Destroy(m_VideoTextureDummyY);
  424. m_VideoTextureDummyY = null;
  425. }
  426. m_VideoTextureDummyY = m_VideoTextureY;
  427. m_VideoTextureY = null;
  428. }
  429. if (m_VideoTextureU != null)
  430. {
  431. if (m_VideoTextureDummyU != null)
  432. {
  433. Destroy(m_VideoTextureDummyU);
  434. m_VideoTextureDummyU = null;
  435. }
  436. m_VideoTextureDummyU = m_VideoTextureU;
  437. m_VideoTextureU = null;
  438. }
  439. if (m_VideoTextureV != null)
  440. {
  441. if (m_VideoTextureDummyV != null)
  442. {
  443. Destroy(m_VideoTextureDummyV);
  444. m_VideoTextureDummyV = null;
  445. }
  446. m_VideoTextureDummyV = m_VideoTextureV;
  447. m_VideoTextureV = null;
  448. }
  449. #if UNITY_ANDROID || UNITY_EDITOR || UNITY_STANDALONE
  450. #if UNITY_EDITOR || UNITY_STANDALONE
  451. if (m_bPC_FastMode == true)
  452. {
  453. m_VideoTextureY = new Texture2D(Call_GetVideoWidth(), Call_GetVideoHeight(), TextureFormat.Alpha8, false);
  454. m_VideoTextureU = new Texture2D(Call_GetVideoWidth() / 2, Call_GetVideoHeight() / 2, TextureFormat.Alpha8, false);
  455. m_VideoTextureV = new Texture2D(Call_GetVideoWidth() / 2, Call_GetVideoHeight() / 2, TextureFormat.Alpha8, false);
  456. m_VideoTextureY.filterMode = FilterMode.Bilinear;
  457. m_VideoTextureY.wrapMode = TextureWrapMode.Clamp;
  458. m_VideoTextureU.filterMode = FilterMode.Bilinear;
  459. m_VideoTextureU.wrapMode = TextureWrapMode.Clamp;
  460. m_VideoTextureV.filterMode = FilterMode.Bilinear;
  461. m_VideoTextureV.wrapMode = TextureWrapMode.Clamp;
  462. }
  463. else
  464. {
  465. if (m_bSupportRockchip)
  466. {
  467. m_VideoTexture = new Texture2D(Call_GetVideoWidth(), Call_GetVideoHeight(), TextureFormat.RGB565, false);
  468. }
  469. else
  470. {
  471. m_VideoTexture = new Texture2D(Call_GetVideoWidth(), Call_GetVideoHeight(), TextureFormat.RGBA32, false);
  472. }
  473. m_VideoTexture.filterMode = FilterMode.Bilinear;
  474. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  475. }
  476. if (m_bPC_FastMode == true)
  477. {
  478. m_texPtrY = m_VideoTextureY.GetNativeTexturePtr();
  479. m_texPtrU = m_VideoTextureU.GetNativeTexturePtr();
  480. m_texPtrV = m_VideoTextureV.GetNativeTexturePtr();
  481. }
  482. else
  483. {
  484. m_texPtr = m_VideoTexture.GetNativeTexturePtr();
  485. }
  486. #else
  487. if (m_bSupportRockchip) {
  488. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
  489. } else {
  490. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  491. }
  492. m_VideoTexture.filterMode = FilterMode.Bilinear;
  493. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  494. m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
  495. #endif
  496. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  497. Call_SetUnityTexture((int)m_texPtr);
  498. #else
  499. Call_SetUnityTexture (m_VideoTexture.GetNativeTextureID ());
  500. #endif
  501. #endif
  502. Call_SetWindowSize();
  503. m_bCheckFBO = true;
  504. if (OnResize != null)
  505. OnResize();
  506. }
  507. else
  508. {
  509. if (Call_GetVideoWidth() != m_iWidth || Call_GetVideoHeight() != m_iHeight)
  510. {
  511. m_iWidth = Call_GetVideoWidth();
  512. m_iHeight = Call_GetVideoHeight();
  513. if (OnResize != null)
  514. OnResize();
  515. ResizeTexture();
  516. }
  517. }
  518. Call_UpdateVideoTexture();
  519. m_iCurrentSeekPosition = Call_GetSeekPosition();
  520. }
  521. if (m_CurrentState != Call_GetStatus())
  522. {
  523. m_CurrentState = Call_GetStatus();
  524. if (m_CurrentState == MEDIAPLAYER_STATE.READY)
  525. {
  526. if (OnReady != null)
  527. OnReady();
  528. if (m_bAutoPlay)
  529. Call_Play(0);
  530. if (m_bReadyPlay)
  531. {
  532. Call_Play(0);
  533. m_bReadyPlay = false;
  534. }
  535. SetVolume(m_fVolume);
  536. }
  537. else if (m_CurrentState == MEDIAPLAYER_STATE.END)
  538. {
  539. if (OnEnd != null)
  540. OnEnd();
  541. if (m_bLoop == true)
  542. {
  543. Call_Play(0);
  544. }
  545. }
  546. else if (m_CurrentState == MEDIAPLAYER_STATE.ERROR)
  547. {
  548. OnError((MEDIAPLAYER_ERROR)Call_GetError(), (MEDIAPLAYER_ERROR)Call_GetErrorExtra());
  549. }
  550. }
  551. GL.InvalidateState();
  552. }
  553. public void DeleteVideoTexture()
  554. {
  555. if (m_VideoTextureDummy != null)
  556. {
  557. Destroy(m_VideoTextureDummy);
  558. m_VideoTextureDummy = null;
  559. }
  560. if (m_VideoTexture != null)
  561. {
  562. Destroy(m_VideoTexture);
  563. m_VideoTexture = null;
  564. }
  565. if (m_VideoTextureDummyY != null)
  566. {
  567. Destroy(m_VideoTextureDummyY);
  568. m_VideoTextureDummyY = null;
  569. }
  570. if (m_VideoTextureY != null)
  571. {
  572. Destroy(m_VideoTextureY);
  573. m_VideoTextureY = null;
  574. }
  575. if (m_VideoTextureDummyU != null)
  576. {
  577. Destroy(m_VideoTextureDummyU);
  578. m_VideoTextureDummyU = null;
  579. }
  580. if (m_VideoTextureU != null)
  581. {
  582. Destroy(m_VideoTextureU);
  583. m_VideoTextureU = null;
  584. }
  585. if (m_VideoTextureDummyV != null)
  586. {
  587. Destroy(m_VideoTextureDummyV);
  588. m_VideoTextureDummyV = null;
  589. }
  590. if (m_VideoTextureV != null)
  591. {
  592. Destroy(m_VideoTextureV);
  593. m_VideoTextureV = null;
  594. }
  595. }
  596. public void ResizeTexture()
  597. {
  598. Debug.Log("ResizeTexture " + m_iWidth + " " + m_iHeight);
  599. if (m_iWidth == 0 || m_iHeight == 0)
  600. return;
  601. if (m_VideoTexture != null)
  602. {
  603. //Destroy(m_VideoTexture);
  604. if (m_VideoTextureDummy != null)
  605. {
  606. Destroy(m_VideoTextureDummy);
  607. m_VideoTextureDummy = null;
  608. }
  609. m_VideoTextureDummy = m_VideoTexture;
  610. m_VideoTexture = null;
  611. }
  612. if (m_VideoTextureY != null)
  613. {
  614. if (m_VideoTextureDummyY != null)
  615. {
  616. Destroy(m_VideoTextureDummyY);
  617. m_VideoTextureDummyY = null;
  618. }
  619. m_VideoTextureDummyY = m_VideoTextureY;
  620. m_VideoTextureY = null;
  621. }
  622. if (m_VideoTextureU != null)
  623. {
  624. if (m_VideoTextureDummyU != null)
  625. {
  626. Destroy(m_VideoTextureDummyU);
  627. m_VideoTextureDummyU = null;
  628. }
  629. m_VideoTextureDummyU = m_VideoTextureU;
  630. m_VideoTextureU = null;
  631. }
  632. if (m_VideoTextureV != null)
  633. {
  634. if (m_VideoTextureDummyV != null)
  635. {
  636. Destroy(m_VideoTextureDummyV);
  637. m_VideoTextureDummyV = null;
  638. }
  639. m_VideoTextureDummyV = m_VideoTextureV;
  640. m_VideoTextureV = null;
  641. }
  642. #if UNITY_IPHONE && !UNITY_EDITOR
  643. _videoTexture =null;
  644. bFirstIOS = true;
  645. #endif
  646. #if UNITY_ANDROID || UNITY_EDITOR || UNITY_STANDALONE
  647. #if UNITY_EDITOR || UNITY_STANDALONE
  648. if (m_bPC_FastMode == true)
  649. {
  650. m_VideoTextureY = new Texture2D(Call_GetVideoWidth(), Call_GetVideoHeight(), TextureFormat.Alpha8, false);
  651. m_VideoTextureU = new Texture2D(Call_GetVideoWidth() / 2, Call_GetVideoHeight() / 2, TextureFormat.Alpha8, false);
  652. m_VideoTextureV = new Texture2D(Call_GetVideoWidth() / 2, Call_GetVideoHeight() / 2, TextureFormat.Alpha8, false);
  653. }
  654. else
  655. {
  656. if (m_bSupportRockchip)
  657. {
  658. m_VideoTexture = new Texture2D(Call_GetVideoWidth(), Call_GetVideoHeight(), TextureFormat.RGB565, false);
  659. }
  660. else
  661. {
  662. m_VideoTexture = new Texture2D(Call_GetVideoWidth(), Call_GetVideoHeight(), TextureFormat.RGBA32, false);
  663. }
  664. }
  665. m_VideoTexture.filterMode = FilterMode.Bilinear;
  666. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  667. if (m_bPC_FastMode == true)
  668. {
  669. m_texPtrY = m_VideoTextureY.GetNativeTexturePtr();
  670. m_texPtrU = m_VideoTextureU.GetNativeTexturePtr();
  671. m_texPtrV = m_VideoTextureV.GetNativeTexturePtr();
  672. }
  673. else
  674. {
  675. m_texPtr = m_VideoTexture.GetNativeTexturePtr();
  676. }
  677. #else
  678. if (m_bSupportRockchip) {
  679. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
  680. } else {
  681. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  682. }
  683. m_VideoTexture.filterMode = FilterMode.Bilinear;
  684. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  685. m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
  686. #endif
  687. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  688. Call_SetUnityTexture((int)m_texPtr);
  689. #else
  690. Call_SetUnityTexture (m_VideoTexture.GetNativeTextureID ());
  691. #endif
  692. #endif
  693. Call_SetWindowSize();
  694. }
  695. public void Resize()
  696. {
  697. if (m_CurrentState != MEDIAPLAYER_STATE.PLAYING)
  698. return;
  699. if (Call_GetVideoWidth() <= 0 || Call_GetVideoHeight() <= 0)
  700. {
  701. return;
  702. }
  703. if (m_objResize != null)
  704. {
  705. int iScreenWidth = Screen.width;
  706. int iScreenHeight = Screen.height;
  707. float fRatioScreen = (float)iScreenHeight / (float)iScreenWidth;
  708. int iWidth = Call_GetVideoWidth();
  709. int iHeight = Call_GetVideoHeight();
  710. float fRatio = (float)iHeight / (float)iWidth;
  711. float fRatioResult = fRatioScreen / fRatio;
  712. for (int i = 0; i < m_objResize.Length; i++)
  713. {
  714. if (m_objResize[i] == null)
  715. continue;
  716. if (m_bFullScreen)
  717. {
  718. m_objResize[i].transform.localScale = new Vector3(20.0f / fRatioScreen, 20.0f / fRatioScreen, 1.0f);
  719. if (fRatio < 1.0f)
  720. {
  721. if (fRatioScreen < 1.0f)
  722. {
  723. if (fRatio > fRatioScreen)
  724. {
  725. m_objResize[i].transform.localScale *= fRatioResult;
  726. }
  727. }
  728. m_ScaleValue = MEDIA_SCALE.SCALE_X_TO_Y;
  729. }
  730. else
  731. {
  732. if (fRatioScreen > 1.0f)
  733. {
  734. if (fRatio >= fRatioScreen)
  735. {
  736. m_objResize[i].transform.localScale *= fRatioResult;
  737. }
  738. }
  739. else
  740. {
  741. m_objResize[i].transform.localScale *= fRatioResult;
  742. }
  743. m_ScaleValue = MEDIA_SCALE.SCALE_X_TO_Y;
  744. }
  745. }
  746. if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Y)
  747. {
  748. m_objResize[i].transform.localScale
  749. = new Vector3(m_objResize[i].transform.localScale.x
  750. , m_objResize[i].transform.localScale.x * fRatio
  751. , m_objResize[i].transform.localScale.z);
  752. }
  753. else if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Y_2)
  754. {
  755. m_objResize[i].transform.localScale
  756. = new Vector3(m_objResize[i].transform.localScale.x
  757. , m_objResize[i].transform.localScale.x * fRatio / 2.0f
  758. , m_objResize[i].transform.localScale.z);
  759. }
  760. else if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Z)
  761. {
  762. m_objResize[i].transform.localScale
  763. = new Vector3(m_objResize[i].transform.localScale.x
  764. , m_objResize[i].transform.localScale.y
  765. , m_objResize[i].transform.localScale.x * fRatio);
  766. }
  767. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Y_TO_X)
  768. {
  769. m_objResize[i].transform.localScale
  770. = new Vector3(m_objResize[i].transform.localScale.y / fRatio
  771. , m_objResize[i].transform.localScale.y
  772. , m_objResize[i].transform.localScale.z);
  773. }
  774. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Y_TO_Z)
  775. {
  776. m_objResize[i].transform.localScale
  777. = new Vector3(m_objResize[i].transform.localScale.x
  778. , m_objResize[i].transform.localScale.y
  779. , m_objResize[i].transform.localScale.y / fRatio);
  780. }
  781. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Z_TO_X)
  782. {
  783. m_objResize[i].transform.localScale
  784. = new Vector3(m_objResize[i].transform.localScale.z * fRatio
  785. , m_objResize[i].transform.localScale.y
  786. , m_objResize[i].transform.localScale.z);
  787. }
  788. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Z_TO_Y)
  789. {
  790. m_objResize[i].transform.localScale
  791. = new Vector3(m_objResize[i].transform.localScale.x
  792. , m_objResize[i].transform.localScale.z * fRatio
  793. , m_objResize[i].transform.localScale.z);
  794. }
  795. else
  796. {
  797. m_objResize[i].transform.localScale
  798. = new Vector3(m_objResize[i].transform.localScale.x, m_objResize[i].transform.localScale.y, m_objResize[i].transform.localScale.z);
  799. }
  800. }
  801. }
  802. }
  803. //The error code is the following sites related documents.
  804. //http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html
  805. void OnError(MEDIAPLAYER_ERROR iCode, MEDIAPLAYER_ERROR iCodeExtra)
  806. {
  807. string strError = "";
  808. switch (iCode)
  809. {
  810. case MEDIAPLAYER_ERROR.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
  811. strError = "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK";
  812. break;
  813. case MEDIAPLAYER_ERROR.MEDIA_ERROR_SERVER_DIED:
  814. strError = "MEDIA_ERROR_SERVER_DIED";
  815. break;
  816. case MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN:
  817. strError = "MEDIA_ERROR_UNKNOWN";
  818. break;
  819. default:
  820. strError = "Unknown error " + iCode;
  821. break;
  822. }
  823. strError += " ";
  824. switch (iCodeExtra)
  825. {
  826. case MEDIAPLAYER_ERROR.MEDIA_ERROR_IO:
  827. strError += "MEDIA_ERROR_IO";
  828. break;
  829. case MEDIAPLAYER_ERROR.MEDIA_ERROR_MALFORMED:
  830. strError += "MEDIA_ERROR_MALFORMED";
  831. break;
  832. case MEDIAPLAYER_ERROR.MEDIA_ERROR_TIMED_OUT:
  833. strError += "MEDIA_ERROR_TIMED_OUT";
  834. break;
  835. case MEDIAPLAYER_ERROR.MEDIA_ERROR_UNSUPPORTED:
  836. strError += "MEDIA_ERROR_UNSUPPORTED";
  837. break;
  838. default:
  839. strError = "Unknown error " + iCode;
  840. break;
  841. }
  842. Debug.LogError(strError);
  843. if (OnVideoError != null)
  844. {
  845. OnVideoError(iCode, iCodeExtra);
  846. }
  847. }
  848. void OnDestroy()
  849. {
  850. #if UNITY_ANDROID
  851. //Call_Reset();
  852. #endif
  853. Call_UnLoad();
  854. DeleteVideoTexture();
  855. Call_Destroy();
  856. }
  857. bool m_bPause = false;
  858. void OnApplicationPause(bool bPause)
  859. {
  860. //Debug.Log("ApplicationPause : " + bPause);
  861. if (bPause == true)
  862. {
  863. if (m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  864. {
  865. m_bPause = true;
  866. }
  867. #if (UNITY_IPHONE || UNITY_TVOS) && !UNITY_EDITOR
  868. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  869. {
  870. m_iPauseFrame = m_iCurrentSeekPosition;
  871. Stop();
  872. }
  873. else
  874. {
  875. m_iPauseFrame = 0;
  876. }
  877. #else
  878. Call_Pause();
  879. #endif
  880. }
  881. else
  882. {
  883. #if ( UNITY_IPHONE || UNITY_TVOS )&& !UNITY_EDITOR
  884. if (m_iPauseFrame != 0)
  885. {
  886. m_bStop = false;
  887. Call_Play(m_iPauseFrame);
  888. }
  889. #else
  890. Call_RePlay();
  891. #endif
  892. if (m_bPause == true)
  893. {
  894. Call_Pause();
  895. m_bPause = false;
  896. }
  897. }
  898. }
  899. public MEDIAPLAYER_STATE GetCurrentState()
  900. {
  901. return m_CurrentState;
  902. }
  903. public Texture2D GetVideoTexture()
  904. {
  905. return m_VideoTexture;
  906. }
  907. bool m_bReadyPlay = false;
  908. public void Play()
  909. {
  910. if (m_bStop == true)
  911. {
  912. SeekTo(0);
  913. Call_Play(0);
  914. m_bStop = false;
  915. }
  916. if (m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  917. {
  918. Call_RePlay();
  919. }
  920. else if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED || m_CurrentState == MEDIAPLAYER_STATE.END)
  921. {
  922. Call_Play(0);
  923. }
  924. else if (m_CurrentState == MEDIAPLAYER_STATE.NOT_READY)
  925. {
  926. m_bReadyPlay = true;
  927. }
  928. }
  929. public void Stop()
  930. {
  931. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  932. Call_Pause();
  933. m_bStop = true;
  934. m_CurrentState = MEDIAPLAYER_STATE.STOPPED;
  935. m_iCurrentSeekPosition = 0;
  936. }
  937. public void Pause()
  938. {
  939. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  940. {
  941. Call_Pause();
  942. m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
  943. }
  944. }
  945. public void Load(string strFileName)
  946. {
  947. if (GetCurrentState() != MEDIAPLAYER_STATE.NOT_READY)
  948. UnLoad();
  949. m_bReadyPlay = false;
  950. m_bIsFirstFrameReady = false;
  951. m_bFirst = false;
  952. m_bCheckFBO = false;
  953. m_strFileName = strFileName;
  954. if (m_bInit == false)
  955. return;
  956. m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
  957. }
  958. public void SetVolume(float fVolume)
  959. {
  960. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  961. {
  962. m_fVolume = fVolume;
  963. Call_SetVolume(fVolume);
  964. }
  965. }
  966. //return milisecond
  967. public int GetSeekPosition()
  968. {
  969. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END)
  970. return m_iCurrentSeekPosition;
  971. else
  972. return 0;
  973. }
  974. public void SeekTo(int iSeek)
  975. {
  976. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  977. Call_SetSeekPosition(iSeek);
  978. }
  979. /// <summary>
  980. /// Sets the speed.
  981. /// Experimental API( PC&iOS support, support from Android version 6.0 or later)
  982. /// </summary>
  983. /// <param name="fSpeed">video playback speed.</param>
  984. public void SetSpeed(float fSpeed)
  985. {
  986. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  987. {
  988. m_fSpeed = fSpeed;
  989. Call_SetSpeed(fSpeed);
  990. }
  991. }
  992. //Gets the duration of the file.
  993. //Returns
  994. //the duration in milliseconds, if no duration is available (for example, if streaming live content), -1 is returned.
  995. public int GetDuration()
  996. {
  997. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  998. return Call_GetDuration();
  999. else
  1000. return 0;
  1001. }
  1002. public float GetSeekBarValue()
  1003. {
  1004. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  1005. {
  1006. if (GetDuration() == 0)
  1007. {
  1008. return 0;
  1009. }
  1010. return (float)GetSeekPosition() / (float)GetDuration();
  1011. }
  1012. else
  1013. return 0;
  1014. }
  1015. public void SetSeekBarValue(float fValue)
  1016. {
  1017. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  1018. {
  1019. if (GetDuration() == 0)
  1020. {
  1021. return;
  1022. }
  1023. SeekTo((int)((float)GetDuration() * fValue));
  1024. }
  1025. else
  1026. return;
  1027. }
  1028. //Only Android support.
  1029. //Get update status in buffering a media stream received through progressive HTTP download.
  1030. //The received buffering percentage indicates how much of the content has been buffered or played.
  1031. //For example a buffering update of 80 percent when half the content has already been played indicates that the next 30 percent of the content to play has been buffered.
  1032. //the percentage (0-100) of the content that has been buffered or played thus far
  1033. public int GetCurrentSeekPercent()
  1034. {
  1035. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY)
  1036. return Call_GetCurrentSeekPercent();
  1037. else
  1038. return 0;
  1039. }
  1040. public int GetVideoWidth()
  1041. {
  1042. return Call_GetVideoWidth();
  1043. }
  1044. public int GetVideoHeight()
  1045. {
  1046. return Call_GetVideoHeight();
  1047. }
  1048. public void UnLoad()
  1049. {
  1050. m_bCheckFBO = false;
  1051. #if UNITY_ANDROID
  1052. //Call_Reset();
  1053. #endif
  1054. Call_UnLoad();
  1055. m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
  1056. }
  1057. #if !UNITY_EDITOR && !UNITY_STANDALONE && !UNITY_WEBGL
  1058. #if UNITY_ANDROID
  1059. private AndroidJavaObject javaObj = null;
  1060. private AndroidJavaObject GetJavaObject()
  1061. {
  1062. if (javaObj == null)
  1063. {
  1064. javaObj = new AndroidJavaObject("com.EasyMovieTexture.EasyMovieTexture");
  1065. }
  1066. return javaObj;
  1067. }
  1068. private void Call_Destroy()
  1069. {
  1070. #if UNITY_5
  1071. if( SystemInfo.graphicsMultiThreaded == true)
  1072. {
  1073. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1074. GL.IssuePluginEvent(EasyMovieTextureRender(), 5 + m_iAndroidMgrID * 10 + 7000);
  1075. #else
  1076. GL.IssuePluginEvent(5 + m_iAndroidMgrID * 10 + 7000);
  1077. #endif
  1078. }
  1079. else
  1080. {
  1081. GetJavaObject().Call("Destroy");
  1082. }
  1083. #else
  1084. GetJavaObject().Call("Destroy");
  1085. #endif
  1086. }
  1087. private void Call_UnLoad()
  1088. {
  1089. #if UNITY_5
  1090. if( SystemInfo.graphicsMultiThreaded == true)
  1091. {
  1092. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1093. GL.IssuePluginEvent(EasyMovieTextureRender(), 4 + m_iAndroidMgrID * 10 + 7000);
  1094. #else
  1095. GL.IssuePluginEvent(4 + m_iAndroidMgrID * 10 + 7000);
  1096. #endif
  1097. }
  1098. else
  1099. {
  1100. GetJavaObject().Call("UnLoad");
  1101. }
  1102. #else
  1103. GetJavaObject().Call("UnLoad");
  1104. #endif
  1105. }
  1106. private bool Call_Load(string strFileName, int iSeek)
  1107. {
  1108. #if UNITY_5
  1109. if( SystemInfo.graphicsMultiThreaded == true)
  1110. {
  1111. GetJavaObject().Call("NDK_SetFileName", strFileName);
  1112. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1113. GL.IssuePluginEvent(EasyMovieTextureRender(), 1 + m_iAndroidMgrID * 10 + 7000);
  1114. #else
  1115. GL.IssuePluginEvent(1+ m_iAndroidMgrID * 10 + 7000);
  1116. #endif
  1117. Call_SetNotReady();
  1118. return true;
  1119. }
  1120. else
  1121. {
  1122. GetJavaObject().Call("NDK_SetFileName", strFileName);
  1123. if (GetJavaObject().Call<bool>("Load"))
  1124. {
  1125. return true;
  1126. }
  1127. else
  1128. {
  1129. OnError(MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN, MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN);
  1130. return false;
  1131. }
  1132. }
  1133. #else
  1134. GetJavaObject().Call("NDK_SetFileName", strFileName);
  1135. if (GetJavaObject().Call<bool>("Load"))
  1136. {
  1137. return true;
  1138. }
  1139. else
  1140. {
  1141. OnError(MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN, MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN);
  1142. return false;
  1143. }
  1144. #endif
  1145. }
  1146. private void Call_UpdateVideoTexture()
  1147. {
  1148. if( Call_IsUpdateFrame() == false)
  1149. return;
  1150. if(m_VideoTextureDummy != null)
  1151. {
  1152. Destroy(m_VideoTextureDummy);
  1153. m_VideoTextureDummy = null;
  1154. }
  1155. for( int i = 0; i < m_TargetMaterial.Length; i++)
  1156. {
  1157. if(m_TargetMaterial[i])
  1158. {
  1159. if(m_TargetMaterial[i].GetComponent<MeshRenderer>()!= null)
  1160. {
  1161. if(m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture != m_VideoTexture)
  1162. {
  1163. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = m_VideoTexture;
  1164. }
  1165. }
  1166. if(m_TargetMaterial[i].GetComponent<RawImage>()!= null)
  1167. {
  1168. if(m_TargetMaterial[i].GetComponent<RawImage>().texture != m_VideoTexture)
  1169. {
  1170. m_TargetMaterial[i].GetComponent<RawImage>().texture = m_VideoTexture;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. #if UNITY_5
  1176. if( SystemInfo.graphicsMultiThreaded == true)
  1177. {
  1178. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1179. GL.IssuePluginEvent(EasyMovieTextureRender(), 3 + m_iAndroidMgrID * 10 + 7000);
  1180. #else
  1181. GL.IssuePluginEvent(3+ m_iAndroidMgrID * 10 + 7000);
  1182. #endif
  1183. }
  1184. else
  1185. {
  1186. GetJavaObject().Call("UpdateVideoTexture");
  1187. }
  1188. #else
  1189. GetJavaObject().Call("UpdateVideoTexture");
  1190. #endif
  1191. if (!m_bIsFirstFrameReady)
  1192. {
  1193. m_bIsFirstFrameReady = true;
  1194. if (OnVideoFirstFrameReady != null)
  1195. {
  1196. OnVideoFirstFrameReady();
  1197. //OnVideoFirstFrameReady = null;
  1198. }
  1199. }
  1200. }
  1201. private void Call_SetVolume(float fVolume)
  1202. {
  1203. GetJavaObject().Call("SetVolume",fVolume);
  1204. }
  1205. private void Call_SetSeekPosition(int iSeek)
  1206. {
  1207. GetJavaObject().Call("SetSeekPosition",iSeek);
  1208. }
  1209. private int Call_GetSeekPosition()
  1210. {
  1211. return GetJavaObject().Call<int>("GetSeekPosition");
  1212. }
  1213. private void Call_Play(int iSeek)
  1214. {
  1215. GetJavaObject().Call("Play",iSeek);
  1216. }
  1217. private void Call_Reset()
  1218. {
  1219. GetJavaObject().Call("Reset");
  1220. }
  1221. private void Call_Stop()
  1222. {
  1223. GetJavaObject().Call("Stop");
  1224. }
  1225. private void Call_RePlay()
  1226. {
  1227. GetJavaObject().Call("RePlay");
  1228. }
  1229. private void Call_Pause()
  1230. {
  1231. GetJavaObject().Call("Pause");
  1232. }
  1233. private int Call_InitNDK()
  1234. {
  1235. return GetJavaObject().Call<int>("InitNative",GetJavaObject());
  1236. }
  1237. private int Call_GetVideoWidth()
  1238. {
  1239. return GetJavaObject().Call<int>("GetVideoWidth");
  1240. }
  1241. private int Call_GetVideoHeight()
  1242. {
  1243. return GetJavaObject().Call<int>("GetVideoHeight");
  1244. }
  1245. private bool Call_IsUpdateFrame()
  1246. {
  1247. return GetJavaObject().Call<bool>("IsUpdateFrame");
  1248. }
  1249. private void Call_SetUnityTexture(int iTextureID)
  1250. {
  1251. GetJavaObject().Call("SetUnityTexture",iTextureID);
  1252. }
  1253. private void Call_SetWindowSize()
  1254. {
  1255. #if UNITY_5
  1256. if( SystemInfo.graphicsMultiThreaded == true)
  1257. {
  1258. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1259. GL.IssuePluginEvent(EasyMovieTextureRender(), 2 + m_iAndroidMgrID * 10 + 7000);
  1260. #else
  1261. GL.IssuePluginEvent(2+ m_iAndroidMgrID * 10 + 7000);
  1262. #endif
  1263. }
  1264. else
  1265. {
  1266. GetJavaObject().Call("SetWindowSize");
  1267. }
  1268. #else
  1269. GetJavaObject().Call("SetWindowSize");
  1270. #endif
  1271. }
  1272. private void Call_SetLooping(bool bLoop)
  1273. {
  1274. GetJavaObject().Call("SetLooping",bLoop);
  1275. }
  1276. private void Call_SetRockchip(bool bValue)
  1277. {
  1278. GetJavaObject().Call("SetRockchip",bValue);
  1279. }
  1280. private int Call_GetDuration()
  1281. {
  1282. return GetJavaObject().Call<int>("GetDuration");
  1283. }
  1284. private int Call_GetCurrentSeekPercent()
  1285. {
  1286. return GetJavaObject().Call<int>("GetCurrentSeekPercent");
  1287. }
  1288. private int Call_GetError()
  1289. {
  1290. return GetJavaObject().Call<int>("GetError");
  1291. }
  1292. private void Call_SetSplitOBB(bool bValue, string strOBBName)
  1293. {
  1294. GetJavaObject().Call("SetSplitOBB",bValue,strOBBName);
  1295. }
  1296. private int Call_GetErrorExtra()
  1297. {
  1298. return GetJavaObject().Call<int>("GetErrorExtra");
  1299. }
  1300. private void Call_SetUnityActivity()
  1301. {
  1302. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  1303. AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
  1304. GetJavaObject().Call("SetUnityActivity", jo);
  1305. #if UNITY_5
  1306. if( SystemInfo.graphicsMultiThreaded == true)
  1307. {
  1308. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1309. GL.IssuePluginEvent(EasyMovieTextureRender(), 0 + m_iAndroidMgrID * 10 + 7000);
  1310. #else
  1311. GL.IssuePluginEvent(0+ m_iAndroidMgrID * 10 + 7000);
  1312. #endif
  1313. }
  1314. else
  1315. {
  1316. Call_InitJniManager();
  1317. }
  1318. #else
  1319. Call_InitJniManager();
  1320. #endif
  1321. }
  1322. private void Call_SetNotReady()
  1323. {
  1324. GetJavaObject().Call("SetNotReady");
  1325. }
  1326. private void Call_InitJniManager()
  1327. {
  1328. GetJavaObject().Call("InitJniManager");
  1329. }
  1330. private void Call_SetSpeed(float fSpeed)
  1331. {
  1332. using (AndroidJavaClass buildVersion = new AndroidJavaClass("android.os.Build$VERSION"))
  1333. {
  1334. int sdkVersion = buildVersion.GetStatic<int>("SDK_INT");
  1335. if(sdkVersion >= 23 ) //Android 6.0
  1336. {
  1337. GetJavaObject().Call("SetSpeed",fSpeed);
  1338. }
  1339. }
  1340. }
  1341. private MEDIAPLAYER_STATE Call_GetStatus()
  1342. {
  1343. return (MEDIAPLAYER_STATE)GetJavaObject().Call<int>("GetStatus");
  1344. }
  1345. #elif UNITY_IPHONE || UNITY_TVOS
  1346. [DllImport("__Internal")]
  1347. private static extern int VideoPlayerPluginCreateInstance();
  1348. [DllImport("__Internal")]
  1349. private static extern void VideoPlayerPluginDestroyInstance(int iID);
  1350. [DllImport("__Internal")]
  1351. private static extern bool VideoPlayerPluginCanOutputToTexture(string videoURL);
  1352. [DllImport("__Internal")]
  1353. private static extern void VideoPlayerPluginSetLoop(int iID, bool bLoop);
  1354. [DllImport("__Internal")]
  1355. private static extern void VideoPlayerPluginSetVolume(int iID, float fVolume);
  1356. [DllImport("__Internal")]
  1357. private static extern bool VideoPlayerPluginPlayerReady(int iID);
  1358. [DllImport("__Internal")]
  1359. private static extern float VideoPlayerPluginDurationSeconds(int iID);
  1360. [DllImport("__Internal")]
  1361. private static extern void VideoPlayerPluginExtents(int iID,ref int width, ref int height);
  1362. [DllImport("__Internal")]
  1363. private static extern IntPtr VideoPlayerPluginCurFrameTexture(int iID);
  1364. [DllImport("__Internal")]
  1365. private static extern void VideoPlayerPluginLoadVideo(int iID,string videoURL);
  1366. [DllImport("__Internal")]
  1367. private static extern void VideoPlayerPluginPlayVideo(int iID);
  1368. [DllImport("__Internal")]
  1369. private static extern void VideoPlayerPluginPauseVideo(int iID);
  1370. [DllImport("__Internal")]
  1371. private static extern void VideoPlayerPluginResumeVideo(int iID);
  1372. [DllImport("__Internal")]
  1373. private static extern void VideoPlayerPluginRewindVideo(int iID);
  1374. [DllImport("__Internal")]
  1375. private static extern void VideoPlayerPluginSeekToVideo(int iID,float time);
  1376. [DllImport("__Internal")]
  1377. private static extern float VideoPlayerPluginCurTimeSeconds(int iID);
  1378. [DllImport("__Internal")]
  1379. private static extern bool VideoPlayerPluginIsPlaying(int iID);
  1380. [DllImport("__Internal")]
  1381. private static extern void VideoPlayerPluginStopVideo(int iID);
  1382. [DllImport("__Internal")]
  1383. private static extern bool VideoPlayerPluginFinish(int iID);
  1384. [DllImport("__Internal")]
  1385. private static extern bool VideoPlayerPluginError(int iID);
  1386. [DllImport("__Internal")]
  1387. private static extern void VideoPlayerPluginSetTexture(int iID,int iTextureID);
  1388. [DllImport("__Internal")]
  1389. private static extern void VideoPlayerPluginSetSpeed(int iID,float fSpeed);
  1390. int m_iID = -1;
  1391. /// <summary>
  1392. /// ???????????????true??????? /// </summary>
  1393. public bool ready
  1394. {
  1395. get
  1396. {
  1397. return VideoPlayerPluginPlayerReady(m_iID);
  1398. }
  1399. }
  1400. /// <summary>
  1401. ///
  1402. /// </summary>
  1403. public float duration
  1404. {
  1405. get
  1406. {
  1407. return VideoPlayerPluginDurationSeconds(m_iID);
  1408. }
  1409. }
  1410. public float currentTime
  1411. {
  1412. get
  1413. {
  1414. return VideoPlayerPluginCurTimeSeconds(m_iID);
  1415. }
  1416. }
  1417. public bool isPlaying
  1418. {
  1419. get
  1420. {
  1421. return VideoPlayerPluginIsPlaying(m_iID);
  1422. }
  1423. }
  1424. public Vector2 videoSize
  1425. {
  1426. get
  1427. {
  1428. int width = 0, height = 0;
  1429. VideoPlayerPluginExtents(m_iID,ref width, ref height);
  1430. return new Vector2(width, height);
  1431. }
  1432. }
  1433. private Texture2D _videoTexture;
  1434. public Texture2D videoTexture
  1435. {
  1436. get
  1437. {
  1438. IntPtr nativeTex = ready ? VideoPlayerPluginCurFrameTexture(m_iID) : (IntPtr)0;
  1439. if (nativeTex != (IntPtr)0)
  1440. {
  1441. if (_videoTexture == null)
  1442. {
  1443. #if UNITY_5
  1444. if(SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
  1445. _videoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  1446. else
  1447. _videoTexture = Texture2D.CreateExternalTexture((int)videoSize.x, (int)videoSize.y, TextureFormat.RGBA32,
  1448. false, false, (IntPtr)nativeTex);
  1449. #else
  1450. _videoTexture = Texture2D.CreateExternalTexture((int)videoSize.x, (int)videoSize.y, TextureFormat.RGBA32,
  1451. false, false, (IntPtr)nativeTex);
  1452. #endif
  1453. _videoTexture.filterMode = FilterMode.Bilinear;
  1454. _videoTexture.wrapMode = TextureWrapMode.Clamp;
  1455. }
  1456. _videoTexture.UpdateExternalTexture((IntPtr)nativeTex);
  1457. }
  1458. /* else
  1459. {
  1460. if(_videoTexture != null)
  1461. {
  1462. Destroy(_videoTexture);
  1463. }
  1464. _videoTexture = null;
  1465. }*/
  1466. return _videoTexture;
  1467. }
  1468. }
  1469. private void Call_Destroy()
  1470. {
  1471. VideoPlayerPluginDestroyInstance(m_iID);
  1472. if(_videoTexture != null)
  1473. Destroy(_videoTexture);
  1474. _videoTexture = null;
  1475. m_iID = -1;
  1476. }
  1477. private void Call_UnLoad()
  1478. {
  1479. VideoPlayerPluginStopVideo(m_iID);
  1480. VideoPlayerPluginDestroyInstance(m_iID);
  1481. }
  1482. private bool Call_Load(string strFileName, int iSeek)
  1483. {
  1484. if( m_iID == -1)
  1485. {
  1486. Call_SetUnityActivity();
  1487. }
  1488. bFirstIOS = true;
  1489. /*if(_videoTexture != null)
  1490. {
  1491. Destroy(_videoTexture);
  1492. }*/
  1493. _videoTexture = null;
  1494. if (VideoPlayerPluginCanOutputToTexture(strFileName))
  1495. {
  1496. VideoPlayerPluginLoadVideo(m_iID,strFileName);
  1497. }
  1498. return true;
  1499. }
  1500. bool bFirstIOS = false;
  1501. private void Call_UpdateVideoTexture()
  1502. {
  1503. if( m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  1504. {
  1505. if( videoTexture == null)
  1506. return;
  1507. if(bFirstIOS == true)
  1508. {
  1509. bFirstIOS = false;
  1510. return;
  1511. }
  1512. if(bFirstIOS == false)
  1513. {
  1514. for( int i = 0; i < m_TargetMaterial.Length; i++)
  1515. {
  1516. if(m_TargetMaterial[i])
  1517. {
  1518. if(m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
  1519. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = _videoTexture;
  1520. }
  1521. if(m_TargetMaterial[i])
  1522. {
  1523. if(m_TargetMaterial[i].GetComponent<RawImage>() != null)
  1524. m_TargetMaterial[i].GetComponent<RawImage>().texture= _videoTexture;
  1525. }
  1526. }
  1527. if(m_VideoTextureDummy != null)
  1528. {
  1529. Destroy(m_VideoTextureDummy);
  1530. m_VideoTextureDummy = null;
  1531. }
  1532. }
  1533. m_VideoTexture = _videoTexture;
  1534. }
  1535. if (!m_bIsFirstFrameReady)
  1536. {
  1537. m_bIsFirstFrameReady = true;
  1538. if (OnVideoFirstFrameReady != null)
  1539. {
  1540. OnVideoFirstFrameReady();
  1541. //OnVideoFirstFrameReady = null;
  1542. }
  1543. }
  1544. }
  1545. private void Call_SetVolume(float fVolume)
  1546. {
  1547. VideoPlayerPluginSetVolume(m_iID, fVolume);
  1548. }
  1549. private void Call_SetSeekPosition(int iSeek)
  1550. {
  1551. float fSeek = (float)iSeek / 1000.0f;
  1552. VideoPlayerPluginSeekToVideo(m_iID,fSeek);
  1553. }
  1554. private int Call_GetSeekPosition()
  1555. {
  1556. float fSeek = VideoPlayerPluginCurTimeSeconds(m_iID);
  1557. return (int)(fSeek * 1000.0f);
  1558. }
  1559. private void Call_Play(int iSeek)
  1560. {
  1561. float fSeek = (float)iSeek / 1000.0f;
  1562. if( isPlaying == true )
  1563. {
  1564. VideoPlayerPluginSeekToVideo(m_iID,fSeek);
  1565. }
  1566. else
  1567. {
  1568. if( m_CurrentState != MEDIAPLAYER_STATE.READY)
  1569. VideoPlayerPluginSeekToVideo(m_iID,fSeek);
  1570. VideoPlayerPluginPlayVideo(m_iID);
  1571. }
  1572. if( m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  1573. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  1574. }
  1575. private void Call_Reset()
  1576. {
  1577. }
  1578. private void Call_Stop()
  1579. {
  1580. VideoPlayerPluginStopVideo(m_iID);
  1581. }
  1582. private void Call_RePlay()
  1583. {
  1584. VideoPlayerPluginResumeVideo(m_iID);
  1585. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  1586. }
  1587. private void Call_Pause()
  1588. {
  1589. m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
  1590. VideoPlayerPluginPauseVideo(m_iID);
  1591. }
  1592. private int Call_GetVideoWidth()
  1593. {
  1594. return (int)videoSize.x;
  1595. }
  1596. private int Call_GetVideoHeight()
  1597. {
  1598. return (int)videoSize.y;
  1599. }
  1600. private void Call_SetUnityTexture(int iTextureID)
  1601. {
  1602. VideoPlayerPluginSetTexture(m_iID,iTextureID);
  1603. }
  1604. private void Call_SetWindowSize()
  1605. {
  1606. }
  1607. private void Call_SetLooping(bool bLoop)
  1608. {
  1609. VideoPlayerPluginSetLoop(m_iID, bLoop);
  1610. }
  1611. public void Call_SetUnityActivity()
  1612. {
  1613. m_iID = VideoPlayerPluginCreateInstance();
  1614. }
  1615. private int Call_GetError()
  1616. {
  1617. return 0;
  1618. }
  1619. private int Call_GetErrorExtra()
  1620. {
  1621. return 0;
  1622. }
  1623. private int Call_GetDuration()
  1624. {
  1625. return (int)(duration * 1000);
  1626. }
  1627. private int Call_GetCurrentSeekPercent()
  1628. {
  1629. return -1;
  1630. }
  1631. private void Call_SetSpeed(float fSpeed)
  1632. {
  1633. VideoPlayerPluginSetSpeed(m_iID,fSpeed);
  1634. }
  1635. private MEDIAPLAYER_STATE Call_GetStatus()
  1636. {
  1637. if( VideoPlayerPluginFinish(m_iID) )
  1638. {
  1639. return MEDIAPLAYER_STATE.END;
  1640. }
  1641. if( VideoPlayerPluginError(m_iID) )
  1642. {
  1643. return MEDIAPLAYER_STATE.ERROR;
  1644. }
  1645. if( m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  1646. {
  1647. return m_CurrentState;
  1648. }
  1649. else if(isPlaying)
  1650. {
  1651. return MEDIAPLAYER_STATE.PLAYING;
  1652. }
  1653. else if(ready)
  1654. {
  1655. return MEDIAPLAYER_STATE.READY;
  1656. }
  1657. return m_CurrentState;
  1658. }
  1659. #endif
  1660. #else
  1661. AVFrame* pConvertedFrame = null;
  1662. sbyte* pConvertedFrameBuffer = null;
  1663. SwsContext* pConvertContext = null;
  1664. AVCodecContext* pCodecContext = null;
  1665. AVCodecContext* pAudioCodecContext = null;
  1666. AVFrame* pDecodedFrame = null;
  1667. AVFrame* pDecodedAudioFrame = null;
  1668. AVFormatContext* pFormatContext = null;
  1669. AVPacket* pPacket = null;
  1670. AVStream* pStream = null;
  1671. AVStream* pStreamAudio = null;
  1672. int iStreamAudioIndex;
  1673. int iStreamIndex;
  1674. AudioClip audioClip;
  1675. AudioSource audioSource;
  1676. int iSoundCount = 0;
  1677. int iInitCount = 0;
  1678. double pts;
  1679. bool bVideoFirstFrameReady = false;
  1680. int m_iID;
  1681. private void Call_Destroy()
  1682. {
  1683. if (loader != null)
  1684. {
  1685. while (loader.IsAlive == true)
  1686. {
  1687. loader.Abort();
  1688. }
  1689. loader = null;
  1690. }
  1691. if (threadVideo != null)
  1692. {
  1693. while (threadVideo.IsAlive == true)
  1694. {
  1695. threadVideo.Abort();
  1696. }
  1697. threadVideo = null;
  1698. }
  1699. ffmpeg.avformat_network_deinit();
  1700. ReleaseTexture(m_iID);
  1701. }
  1702. private void Call_UnLoad()
  1703. {
  1704. m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
  1705. if (loader != null)
  1706. {
  1707. while (loader.IsAlive == true)
  1708. {
  1709. loader.Abort();
  1710. }
  1711. loader = null;
  1712. }
  1713. if (threadVideo != null)
  1714. {
  1715. while (threadVideo.IsAlive == true)
  1716. {
  1717. threadVideo.Abort();
  1718. }
  1719. threadVideo = null;
  1720. }
  1721. if (listAudio != null)
  1722. {
  1723. listAudio.Clear();
  1724. listAudio = null;
  1725. }
  1726. if (listVideo != null)
  1727. {
  1728. listVideo.Clear();
  1729. listVideo = null;
  1730. }
  1731. if (listAudioPts != null)
  1732. {
  1733. listAudioPts.Clear();
  1734. listAudioPts = null;
  1735. }
  1736. if (listAudioPtsTime != null)
  1737. {
  1738. listAudioPtsTime.Clear();
  1739. listAudioPtsTime = null;
  1740. }
  1741. if (listVideoPts != null)
  1742. {
  1743. listVideoPts.Clear();
  1744. listVideoPts = null;
  1745. }
  1746. fCurrentSeekTime = 0.0f;
  1747. fLastFrameTime = 0.0f;
  1748. if (pPacket != null)
  1749. {
  1750. ffmpeg.av_free_packet(pPacket);
  1751. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  1752. pPacket = null;
  1753. }
  1754. if (pConvertedFrame != null)
  1755. {
  1756. ffmpeg.av_free(pConvertedFrame);
  1757. pConvertedFrame = null;
  1758. }
  1759. if (pConvertedFrameBuffer != null)
  1760. {
  1761. ffmpeg.av_free(pConvertedFrameBuffer);
  1762. pConvertedFrameBuffer = null;
  1763. }
  1764. if (pConvertContext != null)
  1765. {
  1766. ffmpeg.sws_freeContext(pConvertContext);
  1767. pConvertContext = null;
  1768. }
  1769. if (pDecodedFrame != null)
  1770. {
  1771. ffmpeg.av_free(pDecodedFrame);
  1772. pDecodedFrame = null;
  1773. }
  1774. if (pDecodedAudioFrame != null)
  1775. ffmpeg.av_free(pDecodedAudioFrame);
  1776. pDecodedAudioFrame = null;
  1777. if (pCodecContext != null)
  1778. ffmpeg.avcodec_close(pCodecContext);
  1779. pCodecContext = null;
  1780. if (pAudioCodecContext != null)
  1781. ffmpeg.avcodec_close(pAudioCodecContext);
  1782. pAudioCodecContext = null;
  1783. if (pFormatContext != null)
  1784. {
  1785. AVFormatContext* ppFormatContext = pFormatContext;
  1786. ffmpeg.avformat_close_input(&ppFormatContext);
  1787. }
  1788. pFormatContext = null;
  1789. if (audioSource != null)
  1790. {
  1791. audioSource.Stop();
  1792. }
  1793. if (audioClip != null)
  1794. {
  1795. Destroy(audioClip);
  1796. audioClip = null;
  1797. }
  1798. }
  1799. Thread loader = null;
  1800. private unsafe bool Call_Load(string strFileName, int iSeek)
  1801. {
  1802. fCurrentSeekTime = 0.0f;
  1803. fLastFrameTime = 0.0f;
  1804. iSoundCount = 0;
  1805. iInitCount = 0;
  1806. bSeekTo = true;
  1807. bEnd = false;
  1808. if (audioSource != null)
  1809. {
  1810. audioSource.Stop();
  1811. audioSource.time = 0.0f;
  1812. }
  1813. if (audioClip != null)
  1814. {
  1815. Destroy(audioClip);
  1816. audioClip = null;
  1817. }
  1818. pFormatContext = ffmpeg.avformat_alloc_context();
  1819. if (strFileName.Contains("://") == false)
  1820. {
  1821. strFileName = Application.streamingAssetsPath + "/" + strFileName;
  1822. Debug.Log(strFileName);
  1823. }
  1824. else if (strFileName.Contains("file://") == true)
  1825. {
  1826. strFileName = strFileName.Replace("file://", "");
  1827. }
  1828. loader = new Thread(() => {
  1829. AVFormatContext* ppFomatContext = null;
  1830. if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, null) != 0)
  1831. {
  1832. pFormatContext = null;
  1833. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1834. Debug.Log("Could not open file");
  1835. throw new ApplicationException(@"Could not open file");
  1836. }
  1837. //ffmpeg.av_dict_free(&opts);
  1838. pFormatContext = ppFomatContext;
  1839. if (ffmpeg.avformat_find_stream_info(pFormatContext, null) != 0)
  1840. {
  1841. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1842. Debug.Log("Could not find stream info");
  1843. throw new ApplicationException(@"Could not find stream info");
  1844. }
  1845. AddActionForUnityMainThread(() => {
  1846. LoadVideoPart2();
  1847. });
  1848. });
  1849. loader.Priority = System.Threading.ThreadPriority.AboveNormal;
  1850. loader.IsBackground = true;
  1851. loader.Start();
  1852. return true;
  1853. }
  1854. void LoadVideoPart2()
  1855. {
  1856. pStream = null;
  1857. pStreamAudio = null;
  1858. bool bFindVideo = false;
  1859. for (var i = 0; i < (pFormatContext)->nb_streams; i++)
  1860. {
  1861. if ((pFormatContext)->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
  1862. {
  1863. if (bFindVideo == false)
  1864. {
  1865. bFindVideo = true;
  1866. pStream = (pFormatContext)->streams[i];
  1867. iStreamIndex = i;
  1868. Debug.Log("Video" + iStreamIndex);
  1869. }
  1870. }
  1871. else if ((pFormatContext)->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
  1872. {
  1873. pStreamAudio = (pFormatContext)->streams[i];
  1874. iStreamAudioIndex = i;
  1875. }
  1876. }
  1877. if (pStream == null)
  1878. {
  1879. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1880. Debug.Log("Could not found video stream");
  1881. throw new ApplicationException(@"Could not found video stream");
  1882. }
  1883. if (pStreamAudio == null)
  1884. {
  1885. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1886. //throw new ApplicationException(@"Could not found audio stream");
  1887. }
  1888. var codecContext = *pStream->codec;
  1889. m_iWidth = codecContext.width;
  1890. m_iHeight = codecContext.height;
  1891. var sourcePixFmt = codecContext.pix_fmt;
  1892. var codecId = codecContext.codec_id;
  1893. var convertToPixFmt = AVPixelFormat.AV_PIX_FMT_RGBA;
  1894. #if UNITY_5
  1895. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D9)
  1896. {
  1897. convertToPixFmt = AVPixelFormat.AV_PIX_FMT_BGRA;
  1898. }
  1899. #endif
  1900. pConvertContext = ffmpeg.sws_getContext(m_iWidth, m_iHeight, sourcePixFmt,
  1901. m_iWidth, m_iHeight, convertToPixFmt,
  1902. ffmpeg.SWS_FAST_BILINEAR, null, null, null);
  1903. if (pConvertContext == null)
  1904. {
  1905. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1906. Debug.Log("Could not initialize the conversion context");
  1907. throw new ApplicationException(@"Could not initialize the conversion context");
  1908. }
  1909. //pConvertedFrame = ffmpeg.avcodec_alloc_frame();
  1910. pConvertedFrame = ffmpeg.av_frame_alloc();
  1911. var convertedFrameBufferSize = ffmpeg.avpicture_get_size(convertToPixFmt, m_iWidth, m_iHeight);
  1912. pConvertedFrameBuffer = (sbyte*)ffmpeg.av_malloc((ulong)convertedFrameBufferSize);
  1913. AVPicture* tempPicture = (AVPicture*)pConvertedFrame;
  1914. ffmpeg.avpicture_fill(tempPicture, pConvertedFrameBuffer, convertToPixFmt, m_iWidth, m_iHeight);
  1915. var pCodec = ffmpeg.avcodec_find_decoder(codecId);
  1916. if (pCodec == null)
  1917. {
  1918. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1919. Debug.Log("Unsupported codec");
  1920. throw new ApplicationException(@"Unsupported codec");
  1921. }
  1922. pCodecContext = pStream->codec;
  1923. pCodecContext->hwaccel = ff_find_hwaccel(pCodecContext->codec_id, sourcePixFmt);
  1924. //if ((pCodec->capabilities & ffmpeg.AV_CODEC_CAP_TRUNCATED) == ffmpeg.AV_CODEC_CAP_TRUNCATED)
  1925. {
  1926. //pCodecContext->flags = ffmpeg.AV_CODEC_FLAG_TRUNCATED;
  1927. }
  1928. if (ffmpeg.avcodec_open2(pCodecContext, pCodec, null) < 0)
  1929. {
  1930. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1931. Debug.Log("Could not open codec");
  1932. throw new ApplicationException(@"Could not open codec");
  1933. }
  1934. if (pStreamAudio != null)
  1935. {
  1936. var codecAudioContext = *pStreamAudio->codec;
  1937. var pAudioCodec = ffmpeg.avcodec_find_decoder(codecAudioContext.codec_id);
  1938. if (pAudioCodec == null)
  1939. {
  1940. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1941. Debug.Log("Unsupported codec");
  1942. throw new ApplicationException(@"Unsupported codec");
  1943. }
  1944. pAudioCodecContext = pStreamAudio->codec;
  1945. ///> Initialize Codec Context as Decoder
  1946. if (ffmpeg.avcodec_open2(pAudioCodecContext, pAudioCodec, null) < 0)
  1947. {
  1948. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1949. Debug.Log("Could not open codec");
  1950. throw new ApplicationException(@"Could not open codec");
  1951. }
  1952. }
  1953. pDecodedFrame = ffmpeg.av_frame_alloc();
  1954. pDecodedAudioFrame = ffmpeg.av_frame_alloc();
  1955. listAudio = new List<float[]>();
  1956. listVideo = new Queue<byte[]>();
  1957. listAudioPts = new List<double>();
  1958. listAudioPtsTime = new List<double>();
  1959. listVideoPts = new Queue<float>();
  1960. if (m_strFileName.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase) == true)
  1961. {
  1962. }
  1963. else
  1964. {
  1965. action = Interrupt1;
  1966. //pFormatContext->interrupt_callback.callback = Marshal.GetFunctionPointerForDelegate(action);
  1967. }
  1968. bVideoFirstFrameReady = false;
  1969. threadVideo = new Thread(ThreadUpdate);
  1970. threadVideo.IsBackground = true;
  1971. threadVideo.Start();
  1972. if (m_bAutoPlay == true)
  1973. {
  1974. if (OnReady != null)
  1975. OnReady();
  1976. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  1977. }
  1978. else
  1979. {
  1980. m_CurrentState = MEDIAPLAYER_STATE.READY;
  1981. if (OnReady != null)
  1982. OnReady();
  1983. }
  1984. loader = null;
  1985. }
  1986. AVHWAccel* ff_find_hwaccel(AVCodecID codec_id, AVPixelFormat pix_fmt)
  1987. {
  1988. AVHWAccel* hwaccel = null;
  1989. while ((hwaccel = ffmpeg.av_hwaccel_next(hwaccel)) != null)
  1990. {
  1991. if (hwaccel->id == codec_id
  1992. && hwaccel->pix_fmt == pix_fmt)
  1993. return hwaccel;
  1994. }
  1995. return null;
  1996. }
  1997. public delegate void VideoInterrupt();
  1998. VideoInterrupt action = null;
  1999. bool bInterrupt = false;
  2000. public void Interrupt1()
  2001. {
  2002. pFormatContext->interrupt_callback.callback = (IntPtr)null;
  2003. bInterrupt = true;
  2004. //Debug.Log ("Interrupt1");
  2005. //Call_Pause ();
  2006. }
  2007. static double av_q2d(AVRational a)
  2008. {
  2009. return a.num / (double)a.den;
  2010. }
  2011. float fLastFrameTime = 0.0f;
  2012. float fCurrentSeekTime = 0.0f;
  2013. float[] fAudioData;
  2014. bool bEnd = false;
  2015. Thread threadVideo;
  2016. List<float[]> listAudio;
  2017. Queue<byte[]> listVideo;
  2018. List<double> listAudioPts;
  2019. List<double> listAudioPtsTime;
  2020. Queue<float> listVideoPts;
  2021. private static void DebugMethod(string message)
  2022. {
  2023. Debug.Log("EasyMovieTexture: " + message);
  2024. }
  2025. private void ThreadUpdate()
  2026. {
  2027. /*lock (listVideo)
  2028. {
  2029. if (listVideo != null)
  2030. {
  2031. listVideo.Clear();
  2032. }
  2033. }
  2034. lock (listVideoPts)
  2035. {
  2036. if (listVideoPts != null)
  2037. {
  2038. listVideoPts.Clear();
  2039. }
  2040. }*/
  2041. while (true)
  2042. {
  2043. if (m_CurrentState == MEDIAPLAYER_STATE.ERROR)
  2044. break;
  2045. if (listVideo != null)
  2046. {
  2047. while (listVideo.Count > 30 || bEnd == true)
  2048. {
  2049. Thread.Sleep(5);
  2050. }
  2051. }
  2052. // if( m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  2053. UpdateVideo();
  2054. Thread.Sleep(1);
  2055. //Thread.Sleep(5);
  2056. }
  2057. }
  2058. private void UpdateVideo()
  2059. {
  2060. var gotPicture = 0;
  2061. var gotSound = 0;
  2062. if (m_CurrentState != MEDIAPLAYER_STATE.PAUSED)
  2063. {
  2064. if (pPacket != null)
  2065. {
  2066. if (pPacket->stream_index == iStreamIndex)
  2067. {
  2068. var size = ffmpeg.avcodec_decode_video2(pCodecContext, pDecodedFrame, &gotPicture, pPacket);
  2069. if (size < 0)
  2070. {
  2071. //throw new ApplicationException (string.Format (@"Error while decoding frame "));
  2072. Debug.Log("Error while decoding frame");
  2073. return;
  2074. }
  2075. if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE)
  2076. {
  2077. pts = ffmpeg.av_frame_get_best_effort_timestamp(pDecodedFrame);
  2078. }
  2079. else
  2080. {
  2081. pts = 0;
  2082. }
  2083. pts *= av_q2d(pStream->time_base);
  2084. if (gotPicture == 1)
  2085. {
  2086. if (pts > 0)
  2087. {
  2088. if (listVideo.Count > 5)
  2089. {
  2090. if (!m_bIsFirstFrameReady)
  2091. {
  2092. //Debug.Log("FistReady" + listVideo.Count);
  2093. m_bIsFirstFrameReady = true;
  2094. bVideoFirstFrameReady = true;
  2095. }
  2096. }
  2097. }
  2098. //Debug.Log ("Video " + pts);
  2099. //fLastFrameTime = (float)pts;
  2100. sbyte** src = &pDecodedFrame->data0;
  2101. sbyte** dst = &pConvertedFrame->data0;
  2102. int* srcStride = pDecodedFrame->linesize;
  2103. int* dstStride = pConvertedFrame->linesize;
  2104. int iLineSize = srcStride[0];
  2105. int iLineSize1 = srcStride[1];
  2106. int iLineSize2 = srcStride[2];
  2107. if (m_bPC_FastMode == true)
  2108. {
  2109. int iVideoSize = m_iWidth * m_iHeight + (2 * m_iWidth / 2 * m_iHeight / 2);
  2110. byte[] buffer = new byte[iVideoSize];
  2111. if (iLineSize != m_iWidth)
  2112. {
  2113. sbyte* convertedFrameAddress;
  2114. IntPtr imageBufferPtr;
  2115. for (int i = 0; i < m_iHeight; i++)
  2116. {
  2117. convertedFrameAddress = pDecodedFrame->data0 + iLineSize * i;
  2118. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2119. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * i, m_iWidth);
  2120. }
  2121. for (int i = 0; i < m_iHeight / 2; i++)
  2122. {
  2123. convertedFrameAddress = pDecodedFrame->data1 + iLineSize1 * i;
  2124. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2125. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * i, m_iWidth / 2);
  2126. convertedFrameAddress = pDecodedFrame->data2 + iLineSize2 * i;
  2127. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2128. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * m_iHeight / 2 + m_iWidth / 2 * i, m_iWidth / 2);
  2129. }
  2130. }
  2131. else
  2132. {
  2133. sbyte* convertedFrameAddress = pDecodedFrame->data0;
  2134. var imageBufferPtr = new IntPtr(convertedFrameAddress);
  2135. Marshal.Copy(imageBufferPtr, buffer, 0, m_iWidth * m_iHeight);
  2136. convertedFrameAddress = pDecodedFrame->data1;
  2137. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2138. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight, m_iWidth / 2 * m_iHeight / 2);
  2139. convertedFrameAddress = pDecodedFrame->data2;
  2140. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2141. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * m_iHeight / 2, m_iWidth / 2 * m_iHeight / 2);
  2142. }
  2143. lock (listVideo)
  2144. {
  2145. listVideo.Enqueue(buffer);
  2146. lock (listVideoPts)
  2147. {
  2148. listVideoPts.Enqueue((float)pts);
  2149. }
  2150. }
  2151. }
  2152. else
  2153. {
  2154. ffmpeg.sws_scale(pConvertContext, src, srcStride, 0, m_iHeight, dst, dstStride);
  2155. sbyte* convertedFrameAddress = pConvertedFrame->data0;
  2156. var imageBufferPtr = new IntPtr(convertedFrameAddress);
  2157. byte[] buffer = new byte[4 * m_iWidth * m_iHeight];
  2158. Marshal.Copy(imageBufferPtr, buffer, 0, 4 * m_iWidth * m_iHeight);
  2159. lock (listVideo)
  2160. {
  2161. listVideo.Enqueue(buffer);
  2162. lock (listVideoPts)
  2163. {
  2164. listVideoPts.Enqueue((float)pts);
  2165. }
  2166. }
  2167. }
  2168. }
  2169. }
  2170. }
  2171. else
  2172. {
  2173. pPacket = (AVPacket*)Marshal.AllocCoTaskMem(sizeof(AVPacket));
  2174. ffmpeg.av_init_packet(pPacket);
  2175. }
  2176. do
  2177. {
  2178. if (pPacket != null)
  2179. {
  2180. ffmpeg.av_free_packet(pPacket);
  2181. }
  2182. int ret = ffmpeg.av_read_frame(pFormatContext, pPacket);
  2183. if (bInterrupt == true && listVideo.Count > 20)
  2184. {
  2185. //Debug.Log ("11");
  2186. bInterrupt = false;
  2187. pFormatContext->interrupt_callback.callback = Marshal.GetFunctionPointerForDelegate(action);
  2188. }
  2189. if (ret < 0)
  2190. {
  2191. if (ret == -541478725)
  2192. {
  2193. if (listVideo.Count < 3)
  2194. {
  2195. bEnd = true;
  2196. //threadVideo.Abort();
  2197. return;
  2198. }
  2199. }
  2200. else
  2201. {
  2202. //throw new ApplicationException(@"Could not read frame");
  2203. Debug.Log("Could not read frame");
  2204. }
  2205. }
  2206. // Debug.Log(pPacket->pts + " " + pCodecContext->pts_correction_last_pts );
  2207. if (pStreamAudio != null)
  2208. {
  2209. if (pPacket->stream_index == iStreamAudioIndex)
  2210. {
  2211. int iAudioLen = ffmpeg.avcodec_decode_audio4(pAudioCodecContext, pDecodedAudioFrame, &gotSound, pPacket);
  2212. if (iAudioLen >= 0)
  2213. {
  2214. if (gotSound == 1)
  2215. {
  2216. int iDataSize = ffmpeg.av_samples_get_buffer_size(null, pAudioCodecContext->channels, pDecodedAudioFrame->nb_samples, pAudioCodecContext->sample_fmt, 1);
  2217. int iDataSize2 = ffmpeg.av_samples_get_buffer_size(null, pAudioCodecContext->channels, pDecodedAudioFrame->nb_samples, AVSampleFormat.AV_SAMPLE_FMT_FLT, 1);
  2218. //if( pAudioCodecContext->sample_fmt != AVSampleFormat.AV_SAMPLE_FMT_FLT)
  2219. {
  2220. //for(int i = 0; i < pAudioCodecContext->channels; i++)
  2221. {
  2222. sbyte* outData = (sbyte*)Marshal.AllocCoTaskMem(iDataSize2 * sizeof(sbyte)); ;
  2223. SwrContext* pAudioCvtContext = null;
  2224. if (pAudioCodecContext->channel_layout == 0)
  2225. {
  2226. pAudioCvtContext = ffmpeg.swr_alloc_set_opts(null, (long)3, AVSampleFormat.AV_SAMPLE_FMT_FLT, pAudioCodecContext->sample_rate
  2227. , (long)3, pAudioCodecContext->sample_fmt, pAudioCodecContext->sample_rate, 0, (void*)0);
  2228. }
  2229. else
  2230. {
  2231. pAudioCvtContext = ffmpeg.swr_alloc_set_opts(null, (long)pAudioCodecContext->channel_layout, AVSampleFormat.AV_SAMPLE_FMT_FLT, pAudioCodecContext->sample_rate
  2232. , (long)pAudioCodecContext->channel_layout, pAudioCodecContext->sample_fmt, pAudioCodecContext->sample_rate, 0, (void*)0);
  2233. }
  2234. int error = 0;
  2235. if ((error = ffmpeg.swr_init(pAudioCvtContext)) < 0)
  2236. {
  2237. Debug.Log("error " + error);
  2238. }
  2239. ffmpeg.swr_convert(pAudioCvtContext, &outData, iDataSize2, pDecodedAudioFrame->extended_data, pDecodedAudioFrame->nb_samples);
  2240. sbyte* soundFrameAddress = outData;
  2241. var soundBufferPtr = new IntPtr(soundFrameAddress);
  2242. byte[] buffer = new byte[iDataSize2];
  2243. Marshal.Copy(soundBufferPtr, buffer, 0, iDataSize2);
  2244. if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE)
  2245. {
  2246. pts = ffmpeg.av_frame_get_best_effort_timestamp(pDecodedAudioFrame);
  2247. }
  2248. else
  2249. {
  2250. pts = 0;
  2251. }
  2252. pts *= av_q2d(pStreamAudio->time_base);
  2253. if (bSeekTo == true)
  2254. {
  2255. double value = pts * (double)pDecodedAudioFrame->sample_rate / ((double)iDataSize2 / 4 / pDecodedAudioFrame->channels);
  2256. //Debug.Log(value + " " + pts + " " + GetDuration() + " " + pDecodedAudioFrame->pkt_duration);
  2257. iSoundCount = (int)value;
  2258. fCurrentSeekTime = (float)pts;
  2259. bSeekTo = false;
  2260. }
  2261. while (pts > 600.0f * (iInitCount + 1))
  2262. {
  2263. iSoundCount -= (int)(600.0f * (double)pDecodedAudioFrame->sample_rate / ((double)iDataSize2 / 4 / pDecodedAudioFrame->channels));
  2264. iInitCount++; ;
  2265. }
  2266. //Debug.Log ("sound " + iSoundCount);
  2267. //Debug.Log (pDecodedAudioFrame->pkt_dts + " " +pDecodedAudioFrame->pkt_duration + " " + pDecodedAudioFrame->pkt_pos + " " + pDecodedAudioFrame->pkt_pts + " " + pts);
  2268. fAudioData = new float[buffer.Length / 4];
  2269. Buffer.BlockCopy(buffer, 0, fAudioData, 0, buffer.Length);
  2270. lock (listAudio)
  2271. {
  2272. listAudio.Add(fAudioData);
  2273. lock (listAudioPts)
  2274. {
  2275. lock (listAudioPtsTime)
  2276. {
  2277. //Debug.Log(pDecodedAudioFrame->pkt_pts % pDecodedAudioFrame->pkt_duration + " " + iDataSize2);
  2278. //listAudioPts.Add(iSoundCount++ * iDataSize2 / 4 / pDecodedAudioFrame->channels );
  2279. /*if( m_strFileName.Contains(".m3u8") )
  2280. {
  2281. listAudioPts.Add(pDecodedAudioFrame->pkt_pts / pDecodedAudioFrame->pkt_duration *iDataSize2 / 4 / pDecodedAudioFrame->channels );
  2282. }
  2283. else*/
  2284. {
  2285. listAudioPts.Add(iSoundCount++ * iDataSize2 / 4 / pDecodedAudioFrame->channels);
  2286. }
  2287. listAudioPtsTime.Add(pts);
  2288. }
  2289. }
  2290. }
  2291. //Debug.Log ( iDataSize + " "+ pts + " " + pAudioCodecContext->sample_rate + " " + pDecodedAudioFrame->sample_rate + " "+ pDecodedAudioFrame->nb_samples + " " + pDecodedAudioFrame->pkt_pts + " " + pDecodedAudioFrame->pkt_pos);
  2292. //Debug.Log ("sound decode time " + pts);
  2293. //audioClip.SetData(fAudioData,(int)(pAudioCodecContext->sample_rate * pts ) );
  2294. //audioClip.SetData(fAudioData,(int)(pDecodedAudioFrame->pkt_pts ) );
  2295. ffmpeg.swr_free(&pAudioCvtContext);
  2296. Marshal.FreeCoTaskMem((IntPtr)outData);
  2297. }
  2298. }
  2299. /*else
  2300. {
  2301. sbyte* soundFrameAddress = pDecodedAudioFrame->extended_data[0];
  2302. var soundBufferPtr = new IntPtr (soundFrameAddress);
  2303. byte[] buffer = new byte[iDataSize];
  2304. Marshal.Copy(soundBufferPtr,buffer,0,iDataSize);
  2305. if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE)
  2306. {
  2307. pts = ffmpeg.av_frame_get_best_effort_timestamp(pDecodedAudioFrame);
  2308. }
  2309. else
  2310. {
  2311. pts = 0;
  2312. }
  2313. pts *= av_q2d(pStreamAudio->time_base);
  2314. //Debug.Log (pDecodedAudioFrame->pkt_dts + " " +pDecodedAudioFrame->pkt_duration + " " + pDecodedAudioFrame->pkt_pos + " " + pDecodedAudioFrame->pkt_pts + " " + pts);
  2315. if (bSeekTo == true)
  2316. {
  2317. double value = pts * (double)pDecodedAudioFrame->sample_rate / ((double)iDataSize2 / 4 / pDecodedAudioFrame->channels);
  2318. //Debug.Log(value + " " + pts + " " + GetDuration() + " " + pDecodedAudioFrame->pkt_duration);
  2319. iSoundCount = (int)value;
  2320. fCurrentSeekTime = (float)pts;
  2321. bSeekTo = false;
  2322. }
  2323. while (pts > 600.0f * (iInitCount + 1))
  2324. {
  2325. iSoundCount -= (int)(600.0f * (double)pDecodedAudioFrame->sample_rate / ((double)iDataSize2 / 4 / pDecodedAudioFrame->channels));
  2326. iInitCount++; ;
  2327. }
  2328. fAudioData = new float[buffer.Length / 4];
  2329. Buffer.BlockCopy(buffer, 0, fAudioData, 0, buffer.Length);
  2330. lock ( listAudio)
  2331. {
  2332. listAudio.Add(fAudioData);
  2333. lock(listAudioPts)
  2334. {
  2335. lock(listAudioPts)
  2336. {
  2337. listAudioPts.Add(pts);
  2338. listAudioPtsTime.Add(pts);
  2339. }
  2340. }
  2341. }
  2342. //Debug.Log ( pDecodedAudioFrame->channels + " "+buffer.Length / 4 + " " + pDecodedAudioFrame->pkt_pts + " " + pDecodedAudioFrame->pkt_pos);
  2343. //Debug.Log ("sound decode time " + pts);
  2344. }*/
  2345. }
  2346. }
  2347. }
  2348. }
  2349. } while (pPacket->stream_index != iStreamIndex);
  2350. }
  2351. }
  2352. int iSoundBufferCount = 0;
  2353. /*void OnAudioSetPosition(int newPosition)
  2354. {
  2355. //Debug.Log (newPosition);
  2356. //Debug.Log("Pts" + listAudioPtsTime[0]);
  2357. }*/
  2358. void OnAudioRead(float[] data)
  2359. {
  2360. //Debug.Log (listAudio.Count);
  2361. if (listAudio.Count < 3)
  2362. {
  2363. return;
  2364. }
  2365. //Debug.Log (data.Length + " " + listAudio[1].Length);
  2366. if (iSoundBufferCount == 0)
  2367. {
  2368. iSoundBufferCount = listAudio[0].Length;
  2369. }
  2370. if (iSoundBufferCount < data.Length)
  2371. {
  2372. Array.Copy(listAudio[0], listAudio[0].Length - iSoundBufferCount, data, 0, iSoundBufferCount);
  2373. //Debug.Log (iSoundBufferCount + " " + data.Length + " " + listAudio [1].Length);
  2374. if ((data.Length - iSoundBufferCount) > listAudio[1].Length)
  2375. {
  2376. Array.Copy(listAudio[1], 0, data, 0, listAudio[1].Length);
  2377. Array.Copy(listAudio[2], 0, data, iSoundBufferCount + listAudio[1].Length, data.Length - iSoundBufferCount - listAudio[1].Length);
  2378. iSoundBufferCount = listAudio[2].Length - (data.Length - iSoundBufferCount - listAudio[1].Length);
  2379. listAudio.RemoveAt(0);
  2380. listAudioPts.RemoveAt(0);
  2381. listAudioPtsTime.RemoveAt(0);
  2382. }
  2383. else
  2384. {
  2385. Array.Copy(listAudio[1], 0, data, iSoundBufferCount, data.Length - iSoundBufferCount);
  2386. iSoundBufferCount = listAudio[1].Length - (data.Length - iSoundBufferCount);
  2387. }
  2388. listAudio.RemoveAt(0);
  2389. listAudioPts.RemoveAt(0);
  2390. listAudioPtsTime.RemoveAt(0);
  2391. }
  2392. else
  2393. {
  2394. Array.Copy(listAudio[0], listAudio[0].Length - iSoundBufferCount, data, 0, data.Length);
  2395. iSoundBufferCount -= data.Length;
  2396. }
  2397. if (iSoundBufferCount == 0)
  2398. {
  2399. listAudio.RemoveAt(0);
  2400. listAudioPts.RemoveAt(0);
  2401. listAudioPtsTime.RemoveAt(0);
  2402. iSoundBufferCount = listAudio[0].Length;
  2403. }
  2404. }
  2405. private int m_LastAudioPartSetted = -1;
  2406. private void Call_UpdateVideoTexture()
  2407. {
  2408. if (bEnd == true && listVideo.Count == 0)
  2409. {
  2410. m_CurrentState = MEDIAPLAYER_STATE.END;
  2411. if (OnEnd != null)
  2412. {
  2413. OnEnd();
  2414. //return;
  2415. }
  2416. if (m_bLoop == true)
  2417. {
  2418. Destroy(audioClip);
  2419. audioClip = null;
  2420. Load(m_strFileName);
  2421. bEnd = false;
  2422. return;
  2423. }
  2424. bEnd = false;
  2425. return;
  2426. }
  2427. if (bInterrupt == true)
  2428. {
  2429. if (audioSource != null)
  2430. {
  2431. audioSource.Pause();
  2432. }
  2433. }
  2434. else
  2435. {
  2436. if (audioSource != null && m_CurrentState == MEDIAPLAYER_STATE.PLAYING && m_bIsFirstFrameReady == true)
  2437. {
  2438. if (audioSource.isPlaying == false/* && audioSource.time > 0.02f*/)
  2439. audioSource.Play();
  2440. }
  2441. }
  2442. if (threadVideo == null && m_CurrentState != MEDIAPLAYER_STATE.END && m_CurrentState != MEDIAPLAYER_STATE.NOT_READY && m_CurrentState != MEDIAPLAYER_STATE.ERROR)
  2443. {
  2444. threadVideo = new Thread(ThreadUpdate);
  2445. threadVideo.IsBackground = true;
  2446. threadVideo.Start();
  2447. }
  2448. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING && m_bIsFirstFrameReady == true && bInterrupt == false)
  2449. {
  2450. if (listVideo.Count > 0)
  2451. fCurrentSeekTime += Time.deltaTime * m_fSpeed;
  2452. }
  2453. //Debug.Log("last " + fLastFrameTime + " " + fCurrentSeekTime + " " + listVideo.Count);
  2454. if (Mathf.Abs(fLastFrameTime - fCurrentSeekTime) < 0.1f)
  2455. {
  2456. for (int i = 0; i < listAudio.Count; i++)
  2457. {
  2458. if (listAudioPtsTime.Count > i)
  2459. {
  2460. if (audioSource == null && (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))) > 0)
  2461. {
  2462. audioSource = gameObject.AddComponent<AudioSource>();
  2463. audioSource.volume = m_fVolume;
  2464. }
  2465. }
  2466. if (audioClip == null && audioSource != null)
  2467. {
  2468. /*if ((float)Call_GetDuration() <= 0)
  2469. {
  2470. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 600.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,true,OnAudioRead);
  2471. }
  2472. else*/
  2473. {
  2474. #if UNITY_5
  2475. audioClip = AudioClip.Create("videoAudio", (int)((float)pAudioCodecContext->sample_rate * 600.0f), pAudioCodecContext->channels, pAudioCodecContext->sample_rate, false);
  2476. #else
  2477. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 600.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false,false);
  2478. #endif
  2479. }
  2480. audioSource.clip = audioClip;
  2481. }
  2482. if (audioSource != null && Call_GetDuration() > 0)
  2483. {
  2484. if (listAudioPts.Count > i)
  2485. {
  2486. if (listAudioPts[i] >= 0)
  2487. {
  2488. {
  2489. /*if( listAudioPts[i] > (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))))
  2490. {
  2491. audioClip.SetData(listAudio[i],(int)(((double)pAudioCodecContext->sample_rate) * listAudioPtsTime[i] ) );
  2492. }
  2493. else*/
  2494. {
  2495. //Debug.Log(audioSource.time + " " + (float)listAudioPtsTime[i] + " " +listAudioPts [i] +" " + fLastFrameTime);
  2496. audioClip.SetData(listAudio[i], (int)(listAudioPts[i] % (pAudioCodecContext->sample_rate * 600.0f)));
  2497. }
  2498. }
  2499. }
  2500. }
  2501. }
  2502. }
  2503. if (audioSource != null && audioSource.isPlaying && Call_GetDuration() > 0)
  2504. {
  2505. listAudio.Clear();
  2506. listAudioPts.Clear();
  2507. listAudioPtsTime.Clear();
  2508. }
  2509. }
  2510. else
  2511. {
  2512. //do
  2513. {
  2514. #if (UNITY_5_2 || UNITY_5_3_OR_NEWER)
  2515. if (listVideo.Count > 0)
  2516. {
  2517. if (m_bPC_FastMode == true)
  2518. {
  2519. SetTextureFromUnity(m_iID, (IntPtr)0, m_texPtrY, m_texPtrU, m_texPtrV, m_iWidth, m_iHeight, listVideo.Dequeue());
  2520. GL.IssuePluginEvent(GetRenderEventFunc(), 7000 + m_iID);
  2521. }
  2522. else
  2523. {
  2524. SetTextureFromUnity(m_iID, m_texPtr, (IntPtr)0, (IntPtr)0, (IntPtr)0, m_iWidth, m_iHeight, listVideo.Dequeue());
  2525. GL.IssuePluginEvent(GetRenderEventFunc(), 7000 + m_iID);
  2526. }
  2527. }
  2528. if (listVideoPts.Count > 0)
  2529. {
  2530. float fpts = listVideoPts.Dequeue();
  2531. if (fpts > fCurrentSeekTime)
  2532. {
  2533. fCurrentSeekTime = fpts;
  2534. }
  2535. if (fLastFrameTime == 0)
  2536. {
  2537. {
  2538. if (fpts < 0)
  2539. {
  2540. fLastFrameTime = 0;
  2541. }
  2542. else
  2543. {
  2544. fCurrentSeekTime = fpts;
  2545. fLastFrameTime = fpts;
  2546. }
  2547. if (audioSource != null && Call_GetDuration() > 0)
  2548. {
  2549. audioSource.time = fLastFrameTime % 600.0f;
  2550. }
  2551. }
  2552. }
  2553. else
  2554. {
  2555. if (fpts <= 0)
  2556. fLastFrameTime = fCurrentSeekTime - 0.05f;
  2557. else
  2558. fLastFrameTime = fpts;
  2559. }
  2560. }
  2561. if (audioSource != null && Call_GetDuration() > 0)
  2562. {
  2563. if (audioSource.time - fLastFrameTime % 600.0f > 0.1f)
  2564. {
  2565. //Debug.Log("sync1 " + audioSource.time + " " + fLastFrameTime);
  2566. audioSource.time = fLastFrameTime % 600.0f;
  2567. }
  2568. else if (audioSource.time - fLastFrameTime % 600.0f < -0.1f)
  2569. {
  2570. //Debug.Log("sync2");
  2571. audioSource.time = fLastFrameTime % 600.0f;
  2572. }
  2573. }
  2574. #else
  2575. //var imageBufferPtr = new IntPtr (convertedFrameAddress);
  2576. if( listVideo.Count > 0)
  2577. {
  2578. m_VideoTexture.LoadRawTextureData(listVideo.Dequeue());
  2579. m_VideoTexture.Apply();
  2580. }
  2581. if( listVideoPts.Count > 0)
  2582. {
  2583. float fpts = listVideoPts.Dequeue();
  2584. if(fLastFrameTime == 0)
  2585. {
  2586. if( fpts > fCurrentSeekTime)
  2587. {
  2588. fLastFrameTime = fCurrentSeekTime;
  2589. }
  2590. }
  2591. else
  2592. {
  2593. fLastFrameTime = fpts;
  2594. }
  2595. }
  2596. if( audioSource != null)
  2597. {
  2598. /*if( audioSource.time - fLastFrameTime > 0)
  2599. {
  2600. //Debug.Log("sync1");
  2601. audioSource.time = fLastFrameTime;
  2602. }
  2603. else if( audioSource.time - fLastFrameTime < -0.2f)
  2604. {
  2605. //Debug.Log("sync2");
  2606. audioSource.time = fLastFrameTime;
  2607. }*/
  2608. }
  2609. #endif
  2610. Material material = new Material(Shader.Find("Unlit/Unlit_YUV"));
  2611. if (m_TargetMaterial != null)
  2612. {
  2613. for (int i = 0; i < m_TargetMaterial.Length; i++)
  2614. {
  2615. if (m_TargetMaterial[i] == null)
  2616. continue;
  2617. if (m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
  2618. {
  2619. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture != m_VideoTexture)
  2620. {
  2621. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = m_VideoTexture;
  2622. }
  2623. }
  2624. if (m_bPC_FastMode == true)
  2625. {
  2626. if (m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
  2627. {
  2628. m_TargetMaterial[i].GetComponent<MeshRenderer>().material = material;
  2629. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_Y") != m_VideoTextureY)
  2630. {
  2631. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_Y", m_VideoTextureY);
  2632. }
  2633. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_U") != m_VideoTextureU)
  2634. {
  2635. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_U", m_VideoTextureU);
  2636. }
  2637. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_V") != m_VideoTextureV)
  2638. {
  2639. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_V", m_VideoTextureV);
  2640. }
  2641. }
  2642. }
  2643. if (m_TargetMaterial[i].GetComponent<RawImage>() != null)
  2644. {
  2645. if (m_TargetMaterial[i].GetComponent<RawImage>().texture != m_VideoTexture)
  2646. {
  2647. m_TargetMaterial[i].GetComponent<RawImage>().texture = m_VideoTexture;
  2648. }
  2649. }
  2650. if (m_bPC_FastMode == true)
  2651. {
  2652. if (m_TargetMaterial[i].GetComponent<RawImage>() != null)
  2653. {
  2654. m_TargetMaterial[i].GetComponent<RawImage>().material = material;
  2655. if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_Y") != m_VideoTextureY)
  2656. {
  2657. m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_Y", m_VideoTextureY);
  2658. }
  2659. if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_U") != m_VideoTextureU)
  2660. {
  2661. m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_U", m_VideoTextureU);
  2662. }
  2663. if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_V") != m_VideoTextureV)
  2664. {
  2665. m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_V", m_VideoTextureV);
  2666. }
  2667. }
  2668. }
  2669. }
  2670. }
  2671. if (bVideoFirstFrameReady == true)
  2672. {
  2673. if (OnVideoFirstFrameReady != null)
  2674. {
  2675. OnVideoFirstFrameReady();
  2676. bVideoFirstFrameReady = false;
  2677. }
  2678. if (listAudio.Count > 0)
  2679. {
  2680. if (audioSource == null /*&& (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))) > 0*/)
  2681. {
  2682. audioSource = gameObject.AddComponent<AudioSource>();
  2683. }
  2684. if (audioClip == null && audioSource != null)
  2685. {
  2686. /*if ((float)Call_GetDuration() <= 0)
  2687. {
  2688. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 600.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,true,OnAudioRead);
  2689. }
  2690. else*/
  2691. {
  2692. /*if (Call_GetDuration () > 1500000) {
  2693. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 10.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
  2694. }
  2695. else{
  2696. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 600.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
  2697. }*/
  2698. #if UNITY_5
  2699. audioClip = AudioClip.Create("videoAudio", (int)((float)pAudioCodecContext->sample_rate * 600.0f), pAudioCodecContext->channels, pAudioCodecContext->sample_rate, false);
  2700. #else
  2701. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 600.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false,false);
  2702. #endif
  2703. }
  2704. audioSource.clip = audioClip;
  2705. //audioSource.Play();
  2706. //AudioSource.PlayClipAtPoint(audioClip,new Vector3(0,0,0));
  2707. }
  2708. }
  2709. // Just to be sure I guess - don't know what migh happen if I seek in video etc...
  2710. if (m_LastAudioPartSetted > listAudio.Count)
  2711. {
  2712. m_LastAudioPartSetted = -1;
  2713. }
  2714. // using m_LastAudioPartSetted - 1 again to be sure - what if the last part of audio gets updated/appended? IDK...
  2715. for (int i = Math.Max(0, m_LastAudioPartSetted - 1); i < listAudio.Count; i++)
  2716. {
  2717. m_LastAudioPartSetted = i;
  2718. if (audioSource != null && Call_GetDuration() > 0)
  2719. {
  2720. if (listAudioPts.Count > i)
  2721. {
  2722. if (listAudioPts[i] >= 0)
  2723. {
  2724. {
  2725. /*if (listAudioPts [i] > (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime [i] + ((float)Call_GetDuration () / 1000.0f)))) {
  2726. audioClip.SetData (listAudio [i], (int)(((double)pAudioCodecContext->sample_rate) * listAudioPtsTime [i]));
  2727. } else*/
  2728. {
  2729. //Debug.Log(audioSource.time + " " + (float)listAudioPtsTime[i] + " " +listAudioPts [i] +" " + fLastFrameTime);
  2730. audioClip.SetData(listAudio[i], (int)(listAudioPts[i] % (pAudioCodecContext->sample_rate * 600.0f)));
  2731. }
  2732. }
  2733. }
  2734. }
  2735. //if(audioSource.isPlaying == false)
  2736. //audioSource.Play();
  2737. }
  2738. }
  2739. }
  2740. }//while(fLastFrameTime < fCurrentSeekTime );
  2741. //Debug.Log(listAudio.Count);
  2742. }
  2743. }
  2744. private void Call_SetVolume(float fVolume)
  2745. {
  2746. if (audioSource != null)
  2747. audioSource.volume = fVolume;
  2748. }
  2749. bool bSeekTo = false;
  2750. private void Call_SetSeekPosition(int iSeek)
  2751. {
  2752. if (threadVideo != null)
  2753. {
  2754. while (threadVideo.IsAlive == true)
  2755. {
  2756. threadVideo.Abort();
  2757. }
  2758. threadVideo = null;
  2759. }
  2760. bSeekTo = true;
  2761. iInitCount = 0;
  2762. long seek_target = (long)iSeek * 1000;
  2763. seek_target = ffmpeg.av_rescale_q(seek_target, ffmpeg.av_get_time_base_q(), pStream->time_base);
  2764. //int seek_flags = iSeek - (int)(fLastFrameTime * 1000.0f) < 0 ? ffmpeg.AVSEEK_FLAG_BACKWARD : ffmpeg.AVSEEK_FLAG_BACKWARD;
  2765. if (ffmpeg.av_seek_frame(pFormatContext, iStreamIndex,
  2766. seek_target, ffmpeg.AVSEEK_FLAG_BACKWARD) < 0)
  2767. {
  2768. //error
  2769. }
  2770. else
  2771. {
  2772. /* handle packet queues... more later... */
  2773. }
  2774. fCurrentSeekTime = (float)iSeek / 1000.0f;
  2775. //fLastFrameTime = 0;
  2776. listVideo.Clear();
  2777. listVideoPts.Clear();
  2778. listAudio.Clear();
  2779. listAudioPts.Clear();
  2780. listAudioPtsTime.Clear();
  2781. Destroy(audioClip);
  2782. audioClip = null;
  2783. ffmpeg.avcodec_flush_buffers(pCodecContext);
  2784. }
  2785. private int Call_GetSeekPosition()
  2786. {
  2787. return (int)(fCurrentSeekTime * 1000.0f);
  2788. }
  2789. private void Call_Play(int iSeek)
  2790. {
  2791. if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  2792. {
  2793. SeekTo(iSeek);
  2794. if (audioSource != null)
  2795. {
  2796. if (audioSource.isPlaying == false)
  2797. audioSource.Play();
  2798. audioSource.time = (float)iSeek / 1000.0f;
  2799. }
  2800. bEnd = false;
  2801. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  2802. }
  2803. }
  2804. private void Call_Reset()
  2805. {
  2806. }
  2807. private void Call_Stop()
  2808. {
  2809. SeekTo(0);
  2810. if (audioSource != null)
  2811. {
  2812. audioSource.Stop();
  2813. audioSource.time = 0.0f;
  2814. }
  2815. m_CurrentState = MEDIAPLAYER_STATE.STOPPED;
  2816. }
  2817. private void Call_RePlay()
  2818. {
  2819. if (audioSource != null)
  2820. {
  2821. if (audioSource.isPlaying == false)
  2822. audioSource.Play();
  2823. }
  2824. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  2825. }
  2826. private void Call_Pause()
  2827. {
  2828. if (audioSource != null)
  2829. {
  2830. audioSource.Pause();
  2831. }
  2832. m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
  2833. }
  2834. private int Call_GetVideoWidth()
  2835. {
  2836. //Debug.Log (m_iWidth);
  2837. return m_iWidth;
  2838. }
  2839. private int Call_GetVideoHeight()
  2840. {
  2841. //Debug.Log (m_iHeight);
  2842. return m_iHeight;
  2843. }
  2844. private void Call_SetUnityTexture(int iTextureID)
  2845. {
  2846. }
  2847. private void Call_SetWindowSize()
  2848. {
  2849. }
  2850. private void Call_SetLooping(bool bLoop)
  2851. {
  2852. }
  2853. private void Call_SetRockchip(bool bValue)
  2854. {
  2855. }
  2856. public void Call_SetUnityActivity()
  2857. {
  2858. ffmpeg.av_register_all();
  2859. ffmpeg.avcodec_register_all();
  2860. ffmpeg.avformat_network_init();
  2861. m_iID = SetTexture();
  2862. }
  2863. private int Call_GetError()
  2864. {
  2865. return 0;
  2866. }
  2867. private int Call_GetErrorExtra()
  2868. {
  2869. return 0;
  2870. }
  2871. private int Call_GetDuration()
  2872. {
  2873. //Debug.Log ((int)(pFormatContext->duration / 1000));
  2874. if (pFormatContext != null)
  2875. return (int)(pFormatContext->duration / 1000);
  2876. return 0;
  2877. }
  2878. private int Call_GetCurrentSeekPercent()
  2879. {
  2880. return -1;
  2881. }
  2882. private void Call_SetSplitOBB(bool bValue, string strOBBName)
  2883. {
  2884. }
  2885. private void Call_SetSpeed(float fSpeed)
  2886. {
  2887. if (audioSource != null)
  2888. {
  2889. audioSource.pitch = fSpeed;
  2890. }
  2891. }
  2892. private MEDIAPLAYER_STATE Call_GetStatus()
  2893. {
  2894. return m_CurrentState;
  2895. }
  2896. #endif // !UNITY_EDITOR
  2897. public IEnumerator DownloadStreamingVideoAndLoad(string strURL)
  2898. {
  2899. strURL = strURL.Trim();
  2900. Debug.Log("DownloadStreamingVideo : " + strURL);
  2901. WWW www = new WWW(strURL);
  2902. yield return www;
  2903. if (string.IsNullOrEmpty(www.error))
  2904. {
  2905. if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
  2906. System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
  2907. string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
  2908. /* if(System.IO.File.Exists(write_path) == true)
  2909. {
  2910. Debug.Log("Delete : " + write_path);
  2911. System.IO.File.Delete(write_path);
  2912. }
  2913. */
  2914. System.IO.File.WriteAllBytes(write_path, www.bytes);
  2915. Load("file://" + write_path);
  2916. }
  2917. else
  2918. {
  2919. Debug.Log(www.error);
  2920. }
  2921. www.Dispose();
  2922. www = null;
  2923. Resources.UnloadUnusedAssets();
  2924. }
  2925. public IEnumerator DownloadStreamingVideoAndLoad2(string strURL)
  2926. {
  2927. strURL = strURL.Trim();
  2928. string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
  2929. if (System.IO.File.Exists(write_path) == true)
  2930. {
  2931. Load("file://" + write_path);
  2932. }
  2933. else
  2934. {
  2935. WWW www = new WWW(strURL);
  2936. yield return www;
  2937. if (string.IsNullOrEmpty(www.error))
  2938. {
  2939. if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
  2940. System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
  2941. System.IO.File.WriteAllBytes(write_path, www.bytes);
  2942. Load("file://" + write_path);
  2943. }
  2944. else
  2945. {
  2946. Debug.Log(www.error);
  2947. }
  2948. www.Dispose();
  2949. www = null;
  2950. Resources.UnloadUnusedAssets();
  2951. }
  2952. }
  2953. IEnumerator CopyStreamingAssetVideoAndLoad(string strURL)
  2954. {
  2955. strURL = strURL.Trim();
  2956. string write_path = Application.persistentDataPath + "/" + strURL;
  2957. if (System.IO.File.Exists(write_path) == false)
  2958. {
  2959. Debug.Log("CopyStreamingAssetVideoAndLoad : " + strURL);
  2960. WWW www = new WWW(Application.streamingAssetsPath + "/" + strURL);
  2961. yield return www;
  2962. if (string.IsNullOrEmpty(www.error))
  2963. {
  2964. Debug.Log(write_path);
  2965. System.IO.File.WriteAllBytes(write_path, www.bytes);
  2966. Load("file://" + write_path);
  2967. }
  2968. else
  2969. {
  2970. Debug.Log(www.error);
  2971. }
  2972. www.Dispose();
  2973. www = null;
  2974. }
  2975. else
  2976. {
  2977. Load("file://" + write_path);
  2978. }
  2979. }
  2980. #endif
  2981. #region UnityMainThread adapter
  2982. private List<Action> unityMainThreadActionList = new List<Action>();
  2983. private bool checkNewActions = false;
  2984. private object thisLock = new object();
  2985. /// <summary>
  2986. /// Execute UnityMainThread operations from unityMainThreadActionList List
  2987. /// </summary>
  2988. void CheckThreading()
  2989. {
  2990. lock (thisLock)
  2991. {
  2992. if (unityMainThreadActionList.Count > 0)
  2993. {
  2994. foreach (var a in unityMainThreadActionList)
  2995. {
  2996. a();
  2997. }
  2998. unityMainThreadActionList.Clear();
  2999. }
  3000. }
  3001. }
  3002. /// <summary>
  3003. /// Add a sequence of Code enclosed into the Action (a). It triggers checkNewActions to execute code into the Unity Thread Update
  3004. /// </summary>
  3005. /// <param name="a">The alpha component.</param>
  3006. void AddActionForUnityMainThread(Action a)
  3007. {
  3008. lock (thisLock)
  3009. {
  3010. unityMainThreadActionList.Add(a);
  3011. }
  3012. checkNewActions = true;
  3013. }
  3014. #endregion
  3015. public VideoDate filesVideo;
  3016. IEnumerator DownloadStreamingVideoAndLoad(string strURL, int time)
  3017. {
  3018. strURL = strURL.Trim();
  3019. Debug.Log("DownloadStreamingVideo : " + strURL);
  3020. WWW www = new WWW(strURL);
  3021. yield return www;
  3022. //Debug.Log(www);
  3023. if (string.IsNullOrEmpty(www.error))
  3024. {
  3025. if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
  3026. System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
  3027. string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
  3028. // Debug.Log(write_path);
  3029. if(System.IO.File.Exists(write_path) == false)
  3030. {
  3031. System.IO.File.WriteAllBytes(write_path, www.bytes);
  3032. //Debug.Log("Delete : " + write_path);
  3033. //System.IO.File.Delete(write_path);
  3034. }
  3035. filesVideo.Url = "file://" + write_path;
  3036. filesVideo.TimeSec = time;
  3037. Debug.Log("filesVideo.Url " + write_path);
  3038. //MedaiPlayerSampleGUI.Instance.Timer = time;
  3039. switch (MenuManager._instance.CurStateGameState)
  3040. {
  3041. case MenuManager.StateGameProduction.DurexLifja:
  3042. if (random % 2 == 0)
  3043. {
  3044. if (!CashUrlVideoAdDurex.Contains(filesVideo))
  3045. {
  3046. CashUrlVideoAdDurex.Add(filesVideo);
  3047. }
  3048. }
  3049. else
  3050. {
  3051. if (!CashUrlVideoAdLifja.Contains(filesVideo))
  3052. {
  3053. CashUrlVideoAdLifja.Add(filesVideo);
  3054. }
  3055. }
  3056. break;
  3057. case MenuManager.StateGameProduction.None:
  3058. if (!CashUrlVideoAdLifja.Contains(filesVideo))
  3059. {
  3060. CashUrlVideoAdLifja.Add(filesVideo);
  3061. }
  3062. break;
  3063. }
  3064. //Debug.Log (filesVideo+ " !!!");
  3065. //Call_Load(filesVideo, 0);
  3066. }
  3067. else
  3068. {
  3069. Debug.Log(www.error);
  3070. }
  3071. www.Dispose();
  3072. www = null;
  3073. Resources.UnloadUnusedAssets();
  3074. }
  3075. public ScreenBase IndecatorWait;
  3076. public IEnumerator DownloadVideo()
  3077. {
  3078. //IndecatorWait.Show();
  3079. //MenuManager._instance.VideoAd.Show();
  3080. MenuManager._instance.livesCounterScreen.Hide();
  3081. MedaiPlayerSampleGUI.Instance.progressIndicator.Show();
  3082. while (string.IsNullOrEmpty(filesVideo.Url))
  3083. {
  3084. yield return new WaitForSeconds(0.1f);
  3085. }
  3086. MedaiPlayerSampleGUI.Instance.VideoAd.enabled = true;
  3087. //MenuManager._instance.OnEnteringViewingVideoAd();
  3088. //rend.enabled = true;
  3089. StartCoroutine (SetRend ());
  3090. MedaiPlayerSampleGUI.Instance.Timer = filesVideo.TimeSec;
  3091. Call_Load(filesVideo.Url, 0);
  3092. /*switch (MenuManager._instance.CurStateGameState)
  3093. {
  3094. case MenuManager.StateGameProduction.DurexLifja:
  3095. Debug.Log ("filesVideo " + filesVideo.Url);
  3096. if (random % 2 == 0)
  3097. {
  3098. if (CurCashIndexDurex > CashUrlVideoAdDurex.Count - 1)
  3099. {
  3100. CurCashIndexDurex = 0;
  3101. }
  3102. MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo = CashUrlVideoAdDurex[CurCashIndexDurex];
  3103. if (CashUrlVideoAdDurex.Count > 1)
  3104. {
  3105. Call_Load(CashUrlVideoAdDurex[CurCashIndexDurex].Url, 0);
  3106. MedaiPlayerSampleGUI.Instance.Timer = CashUrlVideoAdDurex[CurCashIndexDurex].TimeSec;
  3107. MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = CashUrlVideoAdDurex[CurCashIndexDurex].TimeSec;
  3108. }
  3109. else
  3110. {
  3111. Call_Load(filesVideo.Url, 0);
  3112. MedaiPlayerSampleGUI.Instance.Timer = filesVideo.TimeSec;
  3113. MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = filesVideo.TimeSec;
  3114. }
  3115. if (CurCashIndexDurex > CashUrlVideoAdDurex.Count - 1)
  3116. {
  3117. CurCashIndexDurex = 0;
  3118. }
  3119. else
  3120. {
  3121. CurCashIndexDurex++;
  3122. }
  3123. }
  3124. else
  3125. {
  3126. if (CurCashIndexLifja > CashUrlVideoAdLifja.Count - 1)
  3127. {
  3128. CurCashIndexLifja = 0;
  3129. }
  3130. if (CashUrlVideoAdLifja.Count > 1)
  3131. {
  3132. Call_Load(CashUrlVideoAdLifja[CurCashIndexLifja].Url, 0);
  3133. MedaiPlayerSampleGUI.Instance.Timer = CashUrlVideoAdLifja[CurCashIndexLifja].TimeSec;
  3134. MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = CashUrlVideoAdLifja[CurCashIndexLifja].TimeSec;
  3135. }
  3136. else
  3137. {
  3138. Call_Load(filesVideo.Url, 0);
  3139. MedaiPlayerSampleGUI.Instance.Timer = filesVideo.TimeSec;
  3140. MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = filesVideo.TimeSec;
  3141. }
  3142. if (CurCashIndexLifja > CashUrlVideoAdLifja.Count - 1)
  3143. {
  3144. CurCashIndexLifja = 0;
  3145. }
  3146. else
  3147. {
  3148. CurCashIndexLifja++;
  3149. }
  3150. }
  3151. Debug.Log("DownloadVideo");
  3152. break;
  3153. case MenuManager.StateGameProduction.None:
  3154. if (CashUrlVideoAdLifja.Count > 1)
  3155. {
  3156. Call_Load(CashUrlVideoAdLifja[UnityEngine.Random.Range(0, CashUrlVideoAdLifja.Count)].Url, 0);
  3157. }
  3158. else
  3159. {
  3160. Call_Load(filesVideo.Url, 0);
  3161. }
  3162. break;
  3163. }*/
  3164. //MedaiPlayerSampleGUI.Instance.VideoAd.enabled = true;
  3165. }
  3166. IEnumerator SetRend()
  3167. {
  3168. yield return new WaitForSeconds (0.5f);
  3169. rend.enabled = true;
  3170. }
  3171. public void PlayAd()
  3172. {
  3173. IndecatorWait.Show();
  3174. SoundManager.Instance.SetSfxVolume(0);
  3175. SoundManager.Instance.SetMusicVolume(0);
  3176. m_bFirst = false;
  3177. //filesVideo.Url = "";
  3178. if (string.IsNullOrEmpty(filesVideo.Url))
  3179. {
  3180. Debug.Log("DownloadVideo string.IsNullOrEmpty(filesVideo.Url)");
  3181. StartCoroutine(DownloadVideo());
  3182. return;
  3183. }
  3184. //MenuManager._instance.VideoAd.Show();
  3185. MenuManager._instance.livesCounterScreen.Hide();
  3186. MedaiPlayerSampleGUI.Instance.progressIndicator.Show();
  3187. MedaiPlayerSampleGUI.Instance.VideoAd.enabled = true;
  3188. MedaiPlayerSampleGUI.Instance.Timer = MedaiPlayerSampleGUI.Instance.scrMedia.CurVideo.TimeSec - 0.5F;
  3189. MenuManager._instance.OnEnteringViewingVideoAd();
  3190. StartCoroutine(SetRend());
  3191. Debug.Log("PLAY " + filesVideo.Url);
  3192. if (FlurryLogger.Instance)
  3193. {
  3194. FlurryLogger.Instance.ShowVideoAd();
  3195. }
  3196. switch (MenuManager._instance.CurStateGameState)
  3197. {
  3198. case MenuManager.StateGameProduction.DurexLifja:
  3199. //Debug.Log ("filesVideo " + filesVideo);
  3200. //if (random % 2 == 0)
  3201. //{
  3202. if (CurCashIndexDurex > CashUrlVideoAdDurex.Count - 1)
  3203. {
  3204. CurCashIndexDurex = 0;
  3205. }
  3206. if (CashUrlVideoAdDurex.Count > 1)
  3207. {
  3208. Call_Load(CashUrlVideoAdDurex[CurCashIndexDurex].Url, 0);
  3209. MedaiPlayerSampleGUI.Instance.Timer = CashUrlVideoAdDurex[CurCashIndexDurex].TimeSec;
  3210. MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = CashUrlVideoAdDurex[CurCashIndexDurex].TimeSec;
  3211. }
  3212. else
  3213. {
  3214. Call_Load(filesVideo.Url, 0);
  3215. MedaiPlayerSampleGUI.Instance.Timer = filesVideo.TimeSec;
  3216. MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = filesVideo.TimeSec;
  3217. }
  3218. if (CurCashIndexDurex > CashUrlVideoAdDurex.Count - 1)
  3219. {
  3220. CurCashIndexDurex = 0;
  3221. }
  3222. else
  3223. {
  3224. CurCashIndexDurex++;
  3225. }
  3226. //}
  3227. //else
  3228. //{
  3229. // if (CurCashIndexLifja > CashUrlVideoAdLifja.Count - 1)
  3230. // {
  3231. // CurCashIndexLifja = 0;
  3232. // }
  3233. // if (CashUrlVideoAdLifja.Count > 1)
  3234. // {
  3235. // Call_Load(CashUrlVideoAdLifja[CurCashIndexLifja].Url, 0);
  3236. // MedaiPlayerSampleGUI.Instance.Timer = CashUrlVideoAdLifja[CurCashIndexLifja].TimeSec;
  3237. // MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = CashUrlVideoAdLifja[CurCashIndexLifja].TimeSec;
  3238. // }
  3239. // else
  3240. // {
  3241. // Call_Load(filesVideo.Url, 0);
  3242. // MedaiPlayerSampleGUI.Instance.Timer = filesVideo.TimeSec;
  3243. // MedaiPlayerSampleGUI.Instance.scrMedia.CurTimeVideo = filesVideo.TimeSec;
  3244. // }
  3245. // if (CurCashIndexLifja > CashUrlVideoAdLifja.Count - 1)
  3246. // {
  3247. // CurCashIndexLifja = 0;
  3248. // }
  3249. // else
  3250. // {
  3251. // CurCashIndexLifja++;
  3252. // }
  3253. //}
  3254. //Debug.Log("playAd");
  3255. break;
  3256. case MenuManager.StateGameProduction.None:
  3257. if (CashUrlVideoAdLifja.Count > 1)
  3258. {
  3259. Call_Load(CashUrlVideoAdLifja[UnityEngine.Random.Range(0, CashUrlVideoAdLifja.Count)].Url, 0);
  3260. }
  3261. else
  3262. {
  3263. Call_Load(filesVideo.Url, 0);
  3264. }
  3265. break;
  3266. }
  3267. //MedaiPlayerSampleGUI.Instance.progressIndicator.Hide();
  3268. //MedaiPlayerSampleGUI.Instance.VideoAd.enabled = true;
  3269. }
  3270. public GameOverScreenTitle GameOverScreenTitle;
  3271. #if UNITY_ANDROID
  3272. //private int _onCallBack;
  3273. #endif
  3274. public void StopAd()
  3275. {
  3276. SoundManager.Instance.SetSfxVolume(1);
  3277. SoundManager.Instance.SetMusicVolume(1);
  3278. //if (MenuManager._instance.gameOverSummaryScreen.isActiveAndEnabled && !MenuManager._instance.IsLevelEnd && !MenuManager._instance.IsSpermWaveAd)
  3279. //{
  3280. // MenuManager._instance.gameOverSummaryScreen.Hide();
  3281. //}
  3282. GameOverScreenTitle.OnActive();
  3283. rend.enabled = false;
  3284. Call_Stop();
  3285. IndecatorWait.Hide();
  3286. }
  3287. }