# How To Know The Playing Clip

**URL:** <https://qa.fmod.com/t/how-to-know-the-playing-clip/21600>\
**Category:** Unity\
**Tags:** unity\
**Created:** [May 9, 2024, 1:27pm UTC](https://qa.fmod.com/t/how-to-know-the-playing-clip/21600 "2024-05-09T13:27:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Waseem](https://avatars.discourse-cdn.com/v4/letter/w/9dc877/32.png) [@Waseem](https://qa.fmod.com/u/Waseem)\
**Post date:** [May 9, 2024, 1:27pm UTC](https://qa.fmod.com/t/how-to-know-the-playing-clip/21600/1 "2024-05-09T13:27:01Z")

</div>

Hello,

I have a radio in my game, as you know radio is just playing music in sequence, so i have setup the FMOD project in one back putting all music in sequence to be played, and i start it by call an event to start it.

My problem is how to know that current song ended and a new one started, also how to know what is the new song name.

Also, if i want to create a favorite list of song, which play songs as you want, how to play a specific song and know that its end to start a new one.

Thanks.

---

<div class="post-metadata">

**Author:** ![li\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/li_fmod/32/6577_2.png) [@li\_fmod](https://qa.fmod.com/u/li_fmod)\
**Post date:** [May 15, 2024, 6:52am UTC](https://qa.fmod.com/t/how-to-know-the-playing-clip/21600/2 "2024-05-15T06:52:21Z")

</div>

Hi,

> [@Waseem](#):
>
> how to know that current song ended and a new one started,

For knowing when the current song ended and started, you could create a callback function for FMOD events. The callback function will be triggered when specific event types occur, like when a song stops or a song starts. If you are looking for reference, we have a `Timeline-Callback` script example here : [Unity Integration | 11. Scripting Examples | Timeline Callbacks](https://fmod.com/docs/2.02/unity/examples-timeline-callbacks.html)

> [@Waseem](#):
>
> how to know what is the new song name.

For getting an event’s name, you can get the song’s name from the `EventDescription.getPath()`[FMOD Engine|6. Studio API Reference|Studio::EventDescription| Studio::EventDescription::getPath](https://fmod.com/docs/2.03/api/studio-api-eventdescription.html#studio_eventdescription_getpath)

A similar post you could find here:

> [@Get Instantiated Event Name in Unity](https://qa.fmod.com/t/get-instantiated-event-name-in-unity/13682):
>
> Hello Support, As the title suggest, I’m trying to get the name of the instantiated event (FMOD.Studio.EventInstance) I receive from another method. It is possible to have it? I so some very old posts on how to do that in C++ but it looks like it’s not working on Unity’s integration. Thanks, Andrea

> [@Waseem](#):
>
> Also, if i want to create a favorite list of songs, which play songs as you want, how to play a specific song and know that its end to start a new one.

For playing a new song when the previous song finished, you could use `Event_CALLBACK` [Unity Integration | 2. User Guide | 2.5 Callbacks](https://fmod.com/docs/2.02/unity/user-guide.html#callbacks), which will be executed when an event starts/stops. Here is a simple script example:

> **NewRadioControllerTest.cs**
>
> ```auto
> using FMOD.Studio;
> using FMODUnity;
> using System;
> using System.Runtime.InteropServices;
> using UnityEngine;
> 
> public class NewRadioControllerTest : MonoBehaviour
> {
> [SerializeField] private EventReference[] radioEventRefs;
> [SerializeField] private int currentSongIndex;
> 
> public class SongInfo
> {
> public int SongIndex = 0;
> public EventReference[] RadioEventRefs;
> }
> 
> private static EVENT_CALLBACK sSongCallback;
> private static SongInfo sSongInfo;
> private static GCHandle sGCHandle;
> 
> private EventInstance mCurrentSong;
> 
> void Start()
> {
> sSongCallback = new EVENT_CALLBACK(SongEventCallback);
> 
> //Create Song Info
> mCurrentSong = RuntimeManager.CreateInstance(radioEventRefs[currentSongIndex]);
> sSongInfo = new SongInfo();
> sSongInfo.RadioEventRefs = radioEventRefs;
> sSongInfo.SongIndex = currentSongIndex;
> 
> SetCallBackAndPlaySong(mCurrentSong);
> }
> 
> void OnDestroy()
> {
> mCurrentSong.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
> mCurrentSong.release();
> }
> 
> // FMOD Event Callback function
> [AOT.MonoPInvokeCallback(typeof(EVENT_CALLBACK))]
> private static FMOD.RESULT SongEventCallback(EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
> {
> EventInstance instance = new EventInstance(instancePtr);
> 
> // Retrieve the user data
> IntPtr songInfoPtr;
> FMOD.RESULT result = instance.getUserData(out songInfoPtr);
> GCHandle gcHandle = GCHandle.FromIntPtr(songInfoPtr);
> SongInfo songInfo = (SongInfo)gcHandle.Target;
> 
> if (type == EVENT_CALLBACK_TYPE.STOPPED)
> {
> Debug.Log("Song has ended.");
> songInfo.SongIndex++;
> songInfo.SongIndex = songInfo.SongIndex % songInfo.RadioEventRefs.Length;
> 
> gcHandle.Free();
> PlayNextSong(instance, songInfo.SongIndex, songInfo);
> }
> return FMOD.RESULT.OK;
> }
> 
> public static void PlayNextSong(EventInstance instance, int index, SongInfo info)
> {
> if (instance.isValid())
> {
> instance.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
> instance.release();
> EventInstance newSong = RuntimeManager.CreateInstance(info.RadioEventRefs[index]);
> sSongInfo = info;
> 
> SetCallBackAndPlaySong(newSong);
> }
> }
> 
> public static void SetCallBackAndPlaySong(EventInstance instance) 
> {
> // Pin the class that will store the data modified during the callback
> sGCHandle = GCHandle.Alloc(sSongInfo);
> 
> // Pass the object through the userdata of the instance
> instance.setUserData(GCHandle.ToIntPtr(sGCHandle));
> instance.setCallback(sSongCallback, EVENT_CALLBACK_TYPE.STOPPED);
> instance.start();
> }
> }
> 
> ```

That way whenever the Song instance’s state has changed, the callback method will also be triggered, for your case, you could start playing the next song when the previous song has ended.

Hope it helps, let me know if you have more questions.
