# SPlayer lifecycle


The `splayer_api` struct, defined in `splayerapi-5.h`, is the main interface for controlling the SDK. It provides the functions to initialize, operate, and cleanly shut down the player. The examples below call them through a pointer to it, `const struct splayer_api* api = &SPLAYER_API;`, as in [Getting started](/sdk/getting-started).

## 1. Initialization

The lifecycle begins by creating a player context with `api->create_with_options()`. It takes the `splayer_config_t` and an options object from `splayer_options_api-1.h`, on which you set the device's brand and model with `set_device_info` and, to route the SDK's console logs to your own handler, `set_log_handler`. `create()` and `create_with_log_handler()` are deprecated (see the [changelog](/sdk/changelog)). This call allocates and initializes internal resources and returns a pointer to a `splayer_t` instance. This handle must be passed to other API functions for operations such as authentication, playback control, and metadata handling.

Once the context is successfully created, the player is considered active. Multiple internal threads are spawned by the SDK during this process. These threads remain active until the player is shut down.

## 2. Monitoring and shutdown handling

After the player context is created, the SDK manages its own internal threads and continues running independently in the background.

To determine when the player intends to shut down, your application should regularly check the return value of `splayer_api->should_exit()`. This function returns `1` when the player is ready to exit. A good guideline is to check this value every 500ms in your application to determine when to begin the shutdown sequence.

Calling `splayer_api->request_exit()` signals the player to begin shutting down which initializes the shutdown process of the SPlayer in a controlled and responsive manner.

## 3. Clean shutdown

To shut down the player cleanly, follow this two-step process:

1. **Request Shutdown**  
   Call `splayer_api->request_exit()`, passing the player context and an appropriate exit reason from the `splayer_exit_t` enum.

2. **Free Resources**  
   After `request_exit()` is called and `should_exit()` returns `1`, you must call `splayer_api->free()` to release all allocated resources. Failing to call this function will result in a memory leak.
