Cocos Video Puzzle Now Runs on WeChat Mini Games with a Single Codebase
WeChat Mini Games are a massive distribution channel, but video texture access has been a persistent gap because the platform lacks standard DOM APIs and custom native bindings. This approach closes that gap with an official WeChat API, letting a single Cocos project target Web, Android, and WeChat without forking the game logic.
A video-based puzzle game built in Cocos Creator now runs across Web, Android native, and WeChat Mini Games from a single codebase. The puzzle logic, Board, Piece, and Shader slicing remain untouched across platforms; only the video-to-texture pipeline changes. For WeChat, the approach replaces the browser's video element and Android's custom gfx.Video with wx.createVideoDecoder, which exposes raw RGBA frame data via getFrameData().
The core flow creates a decoder, listens for the start event to get video dimensions and build a Texture2D, then calls uploadData in the update loop. The trickiest part was looping: the decoder fires ended, not stop, when playback finishes, and a bare seek(0) freezes the frame. The fix listens for ended, calls stop() then start() to restart decoding, and uses flags to prevent re-entry and destruction race conditions.
The VideoTexturePlayer component encapsulates all three platform paths behind a single load() API, so GameManager and the puzzle board need no platform-specific code. The complete source is bundled into the author's paid Cocos mini-game collection.
The ended-vs-stop event mismatch is a sharp API design trap: the decoder's natural completion fires ended, but the intuitive loop point is stop, so a developer following the docs naively writes broken loop logic.
Reusing the same puzzle Shader across three platforms by swapping only the texture source is a clean separation of concerns that many Cocos projects could adopt for any video-driven gameplay, not just puzzles.
The fallback strategy — try stop-then-start first, fall back to seek(0) only on failure — is a defensive pattern worth copying for any stateful media API where restart semantics are unclear.