Skip to content
Samuel Maddock edited this page Nov 3, 2015 · 3 revisions

GMod Media Player was designed to be very easy to setup and only requires a few lines of code to get started. A media player will most often be used in one of two ways; either used globally or attached to an entity.

Global Media Player

A global media player is most similar to what server owners may refer to as a "jukebox." Media will be broadcasted to all players on the server.

The first step in creating a global media player is creating the media player object. The following code creates a media player using the identifier global which will always be available on the client and server.

-- Shared Lua file
GlobalPlayer = MediaPlayer.Create( 'global' )

GlobalPlayer now stores a reference to the media player. The next step would be to create a user interface that players can interact with to request and view queued media.

Entity Media Player

In some cases, media may want to be localized to a certain entity within the game world. An example might be a television or theater.

The following code is a partial example of a scripted entity, without boilerplate code, which will properly install a media player object. For a complete example, check out the mediaplayer_tv entity script.

-- Always inherit the `mediaplayer_base` entity
ENT.Base = "mediaplayer_base"

-- Media player type can be overwritten using the `MediaPlayerType` property;
-- this will default to 'entity'
ENT.MediaPlayerType = "entity"

-- Store a reference to the base entity in the `BaseClass` variable
DEFINE_BASECLASS( 'mediaplayer_base' )

if SERVER then
	
	function ENT:Initialize()
		-- Call the inherited entity's `Initialize` method
		BaseClass.Initialize(self)

		-- ...
	end

else -- CLIENT

	-- Define player configuration for correcting screen rendering orientation
	ENT.PlayerConfig = {
		angle = Angle(-90, 90, 0),
		offset = Vector(1.1, 25.535, 35.06),
		width = 51.19,
		height = 27.928
	}

end

---
-- Called after the media player is fully initialized
--
function ENT:SetupMediaPlayer( mp )

	-- Listen for media change events using the media player's event emitter
	-- interface
	mp:on('mediaChanged', function( media )
		-- Handle event..
	end)

end

If you would like to avoid inheriting the mediaplayer_base entity, please refer to its setup code.

Clone this wiki locally