How to play IMFMediaEngine background audio while SpeechSynthesizer is playing - c#

I want to play Windows Phone 8's Speech Synthesizer while background audio is playing. But every time, I activate the Speech Synthesizer, the background audio stops playing, and resumes after the synthesizer finishes.
Any suggestions?
Thanks.
Some code snippets below:
Background Audio: using IMFMediaEngine in C++, plays successfully.
Microsoft::WRL::ComPtr<IMFMediaEngine> m_mediaEngine;
m_mediaEngine->Play();
In C# / XAML, I have a XAML page with a button, when I click on it, it plays from WP8's latest Speech classes. Plays the text.
using Windows.Phone.Speech.Synthesis;
public async void SpeakText(string text)
{
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
await speechSynthesizer.SpeakTextAsync(text);
}
I tried putting the SpeakTextAsync through threads, but I guess I'm doing it all wrong, because I still can't get both audio playing at the same time.
private void bubble_Click(object sender, RoutedEventArgs e)
{
App.Manager.SpeakText("hello how are you");
// TRY 1: plays text only, background doesn't play
//new Thread(() =>
//{
// App.Manager.SpeakText(chinese);
//}).Start();
// TRY 2: plays text only, background doesn't play
//Deployment.Current.Dispatcher.BeginInvoke(() =>
//{
// App.Manager.SpeakText(chinese);
//});
// TRY 3: plays text only, background doesn't play
//ThreadPool.QueueUserWorkItem(new WaitCallback(SpeakTextProc));
}
private void SpeakTextProc(Object stateInfo)
{
App.Manager.SpeakText("Hello how are you");
}

On Windows Phone 7, I was able to get two separate audio streams using XNA but seeing how that XNA isn't supported for further development on Windows Phone 8, that's not a great solution for you good luck.

Related

Why is my webcam only taking one frame upon execution via Emgu?

I have some code that takes opens my webcam via Emgu upon loading Form1. The problem I have, though, is that I can only load the first frame.
I have tried to use Webcam() to create a video capture, but it only takes the first frame. If I assign this code to a button, it will take only 1 frame upon each button press. I have tried to create a continuation of the video capture using Webcam2(), but the the picturebox had 1 FPS and would exponentially use more memory overtime.
I know it is not my computer as I was able to use webcam via emgu using its facial recognition viewer (I used the guide at https://www.youtube.com/watch?v=ZSxHyp4OZx0 to test this out). Unfortunately, while I am creating a a similar project, I am using AWS for the facial recognition. Still, I was able to run the Emgu camera viewer without any issues or bad framerates. My Laptop is a 2022 high end gaming laptop.
Here is the code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
showit();
//Application.Idle += Webcam2;
//Webcam();
}
private void Webcam2(object sender, EventArgs e)
{
VideoCapture capture = new VideoCapture();
pictureBox1.Image = capture.QueryFrame().ToBitmap();
}
private void Webcam()
{
VideoCapture capture = new VideoCapture();
pictureBox1.Image = capture.QueryFrame().ToBitmap();
}
}
Any idea what is going on?

How do I prevent audio overlap?

Sorry for the title. I couldn't think of a better one. Also, sorry for my English.
I have these four forms. Forms 2-4 have a button that when pressed will play a specific sound from Form 1. There are times that all buttons from the three forms will be pressed almost the same time which causes the audio played from Form 1 overlap each other. How do I prevent this?
Edit:
When all buttons are pressed, I want the audio to "take turns" on being played. "Audio 2, 3" must wait for "Audio 1" to finish playing before they play, and then Audio 3" must wait as well for "Audio 2" to finish. Each "Audio playback" is composed of a SpeechSynthesizer (played using .SpeakAsync) and a SoundPlayer (played using .PlaySync). There's an internet outage on my ISP so I'm just using my phone. It would be quite hard to manually type the code on my phone so sorry for that.
Hi I did a similar thing with a game I made where I had the problem of sounds playing over the top of eachother. In the method that dealt with the sounds, I did this:
SoundPLayer sound = new SoundPlayer(..);
SoundPLayer anotherSound = new SoundPlayer(..);
public async void gameMethod()
{
sound.Play();
await Task.Delay(400); //length of sound
//some code ...
anotherSound.Play();
}
This worked for me and just used await Task.Delay(..) to add more time around sounds - not sure if this is the way to do this but it might be helpful

Windows Media Player - Hide player after it is done playing

I am creating a game, I want to display a short tutorial video to the player after they have registered. I am using a windows media player control. I don't know how to hide the video after it has finished playing ?
I tried using the following:
WMP.Ctlcontrols.play();
Thread.Sleep(3000);
WMP.Dispose();
I am using the disposing as a way to close down the video. I tried hide and close as well but they close the video before it's finished playing, after 3 seconds.
You can handle PlayStateChange event of control and check if e.newState==1, it means the playing has been stopped. Then you can hide the control.
void axWindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if(e.newState== 1) // Stopped
axWindowsMediaPlayer1.Hide();
}

How to play songs in repeat mode in an UWP app

I have created a UWP app to play certain tracks in the background. Basically by following this link: https://blogs.windows.com/buildingapps/2016/01/13/the-basics-of-background-audio/ .
I want to set the repeat count for certain songs, so if a song has repeat count 10, that song is meant to be repeated 10 times before moving on to the next song in the playlist.
On the Windows phone 8.0 platform, the AudioPlayerAgent had the following event which indicated that the play state has changed. It was easy to override that event and add custom logic to repeat songs.
protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
switch (playState)
{
case PlayState.TrackEnded:
// keep repeating the same track
player.Position = new TimeSpan(0, 0, (int)0);
// add custom logic here..
break;
}
NotifyComplete();
}
What would be an equivalent event in the UWP platform?
So far I have tried the following events on the UWP platform, but to no avail..
BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
BackgroundMediaPlayer.Current.MediaEnded += Current_MediaEnded;
BackgroundMediaPlayer.Current.MediaOpened += Current_MediaOpened;
With Windows 10, version 1607, a new single-process model has been introduced that greatly simplifies the process of enabling background audio.
Media continues to play when your app moves from the foreground to the background. This means that even after the user has minimized your app, returned to the home screen, or has navigated away from your app in some other way, your app can continue to play audio.
Starting with Windows 10, version 1607,the recommended best practice for playing media is to use the MediaPlayer class instead of MediaElement
Play a media file with MediaPlayer
_mediaPlayer = new MediaPlayer();
_mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
_mediaPlayer.Play();
MSDN: Play media in the background
Now your app can manage the playlist or loop settings and use the media player instance to call Play method again.

C# youtube video not playing anymore

Hello I have an application that used to allow me to watch videos from youtube, but now the screen is completely black, I cannot view videos. I am using axmediaplayer and here is my code.
private void Form1_Load(object sender, EventArgs e)
{
// play video in embedded media player
axWindowsMediaPlayer1.URL = "http://www.youtube.com/v/Of9JCZ6zraQ";
}
Is it a change made on youtube or does any one know alternatives? it used to work before but not it is not working any more. does any one know what the problem is please?

Categories

Resources