> For the complete documentation index, see [llms.txt](https://glitch9.gitbook.io/native-media-player/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://glitch9.gitbook.io/native-media-player/legacy-docs/legacy-api-references/mediaevents.md).

# MediaEvents

The `MediaEvents` class provides a set of events and methods for handling media-related events in your application. Below is a detailed reference for each property, event, and method available in the `MediaEvents` class.

### Properties

#### Error

* **Description**: Gets the last error message from the native plugin.
* **Type**: `string`
* **Usage**:

  ```csharp
  string errorMessage = MediaEvents.Error;
  ```

#### IsInitialized

* **Description**: Indicates whether the media player is initialized.
* **Type**: `bool`
* **Usage**:

  ```csharp
  bool isInitialized = MediaEvents.IsInitialized;
  ```

### Events

#### onIsBufferingChanged

* **Description**: Event triggered when the buffering state changes.
* **Type**: `Action<bool>`
* **Usage**:

  ```csharp
  MediaEvents.onIsBufferingChanged += (isBuffering) => {
      // Handle buffering state change
  };
  ```

#### onIsPlayingChanged

* **Description**: Event triggered when the playing state changes.
* **Type**: `Action<bool>`
* **Usage**:

  ```csharp
  MediaEvents.onIsPlayingChanged += (isPlaying) => {
      // Handle playing state change
  };
  ```

#### onIsLoadingChanged

* **Description**: Event triggered when the loading state changes.
* **Type**: `Action<bool>`
* **Usage**:

  ```csharp
  MediaEvents.onIsLoadingChanged += (isLoading) => {
      // Handle loading state change
  };
  ```

#### onInitialized

* **Description**: Event triggered when the media player initialization state changes.
* **Type**: `Action<bool>`
* **Usage**:

  ```csharp
  MediaEvents.onInitialized += (isInitialized) => {
      // Handle initialization state change
  };
  ```

#### onPrepared

* **Description**: Event triggered when the media player is prepared.
* **Type**: `Action`
* **Usage**:

  ```csharp
  MediaEvents.onPrepared += () => {
      // Handle media player prepared
  };
  ```

#### onComplete

* **Description**: Event triggered when the media playback is complete.
* **Type**: `Action`
* **Usage**:

  ```csharp
  MediaEvents.onComplete += () => {
      // Handle playback complete
  };
  ```

#### onError

* **Description**: Event triggered when an error occurs.
* **Type**: `Action<string>`
* **Usage**:

  ```csharp
  MediaEvents.onError += (error) => {
      // Handle error
  };
  ```

#### onDownloadProgress

* **Description**: Event triggered to report download progress.
* **Type**: `Action<string, float>`
* **Usage**:

  ```csharp
  MediaEvents.onDownloadProgress += (fileName, progress) => {
      // Handle download progress
  };
  ```

#### onPlaylistUpdated

* **Description**: Internal event triggered when the playlist is updated.
* **Type**: `Action`
* **Usage**:

  ```csharp
  // This event is for internal use and should be handled accordingly.
  ```

### Methods

#### SyncMediaDataOnReturn

* **Description**: Syncs media data when returning to the app.
* **Usage**:

  ```csharp
  csharp코드 복사MediaEvents.SyncMediaDataOnReturn();
  ```

#### GetPlayerName

* **Description**: Gets the name of the player based on the platform and URI type.
* **Type**: `string`
* **Usage**:

  ```csharp
  string playerName = MediaEvents.GetPlayerName();
  ```

#### OnUnityReceiveMessage

* **Description**: Handles messages received from Unity.
* **Usage**:

  ```csharp
  MediaEvents.OnUnityReceiveMessage("ON_INIT_CHANGED_TRUE");
  ```

#### OnIsBufferingChangedFalse

* **Description**: Manually triggers the buffering state to false.
* **Usage**:

  ```csharp
  MediaEvents.OnIsBufferingChangedFalse();
  ```

#### OnError

* **Description**: Manually triggers an error event.
* **Usage**:

  ```csharp
  MediaEvents.OnError("Error message");
  ```

#### OnDownloadProgress

* **Description**: Manually triggers a download progress event.
* **Usage**:

  ```csharp
  MediaEvents.OnDownloadProgress("filename_0.5"); // 50% progress
  ```

### Example Usage

Here's an example of how you might use the `MediaEvents` class in your application:

```csharp
using Glitch9.NativePlugins.NativeMediaPlayer;

public class MediaPlayerExample : MonoBehaviour
{
    void Start()
    {
        // Subscribe to events
        MediaEvents.onInitialized += OnInitialized;
        MediaEvents.onIsPlayingChanged += OnIsPlayingChanged;
        MediaEvents.onError += OnError;
    }

    void OnInitialized(bool isInitialized)
    {
        Debug.Log($"Media Player Initialized: {isInitialized}");
    }

    void OnIsPlayingChanged(bool isPlaying)
    {
        Debug.Log($"Is Playing: {isPlaying}");
    }

    void OnError(string error)
    {
        Debug.LogError($"Media Player Error: {error}");
    }
}
```

This API reference provides detailed information on each property, event, and method available in the `MediaEvents` class, along with their usage examples. This should help users understand and utilize the features of your media player effectively.
