I am using Pjsip C# api to call a person and play a message. After playing first message by calling AudioMediaPlayer.startTransmit function , if user replies something, then plays another message by creating AudioMediaPlayer with other file and calling starttransmit. But at second creation of AudioMediaPlayer it shows exception that access to protected memory occured.
How to play another file with AudioMediaPlayer?
Ok. After few days of crashing head to wall, I found a reason and solution. I should've call Endpoint.libRegisterThread function for each thread where pjsip api call is made. But the problem is that the parameter of this function is thread name and c# winform's main thread name is null. So I created a thread with some name, and called libRegisterThread and other pjsip functions(AudioMediaPlayer.startTransimit() in my case). This resolved the problem.
[MethodImpl(MethodImplOptions.Synchronized)]
public static void checkThread()
{
try
{
if (Dialer.ep != null && !Dialer.ep.libIsThreadRegistered())
Dialer.ep.libRegisterThread(Thread.CurrentThread.Name);
}
catch (Exception ex)
{
Console.WriteLine("CheckThread error : " + ex.Message);
}
}
void playerCreateAndPlayerCallback(string filePath)
{
Thread t = new Thread(() =>
{
checkThread();
player.stopTransmit(mediaOut);
player.Dispose();
player = new AudioMediaPlayer();
player.createPlayer(filePath, 1);
player.startTransmit(mediaOut);
});
t.Name = ThreadName;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}