I want to use MediaPlayer class to play .flv file int UWP app. Here are some test code is't very easy, but it doesn't work. If I play .mp4 file, it's OK, what have to do to play .flv file?
namespace mediaPlayer
{
public sealed partial class MainPage : Page
{
private MediaPlayer player = null;
public MainPage()
{
this.InitializeComponent();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("http://10.160.72.72/vod/1987.flv "));
player = mediaPlayer.MediaPlayer;
player.Play();
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
player.Pause();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
player.Dispose();
}
}
}
I don't think it's possible. MediaPlayer can't play .flv format. Read this link:
I would suggest you convert it to different format: https://msdn.microsoft.com/en-us/library/windows/apps/hh986969.aspx
One possible way is this Player Framework. Haven't tried it out, but it should play .flv format.
Hope it helps!
You could use FFMpegInterop. It isn't easy to set up but the Github page for it and articles online could help you get it up and running, I used it for a project in the past and it has worked for me.
Related
I'm trying to get the Media Player to play my audio files, but it always returns the error "System.UriFormatException", even though I have provided the right path.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private MediaPlayer m_mediaPlayer;
public void Play(string filename)
{
m_mediaPlayer = new MediaPlayer();
m_mediaPlayer.Open(new Uri(filename));
m_mediaPlayer.Play();
}
private void m_btnSound_Click(object sender, EventArgs e)
{
Play("Sound.wav");
}
}
I want to make 2 button. 1 of them to play music and another one to stop the music. But i make just the first one. How i make the stop music button ?
private void Play()
{
string soundfile = #"D:\song.wav";
var sound = new System.Media.SoundPlayer(soundfile);
sound.Play();
}
private void button1_Click(object sender, EventArgs e)
{
Play();
}
The SoundPlayer class you're using has a method to stop playback, just like it has Play. The problem is that you're not storing the instance of SoundPlayer that's playing, so you don't have a reference to it.
Try storing the sound variable as a class-level member. Then you can call it from a different method as well:
private SoundPlayer _player;
private void Play()
{
string soundFile = #"D:\Song.wav";
_player = new System.Media.SoundPlayer(soundFile);
_player.Play();
}
private void Stop()
{
if (_player != null)
{
_player.Stop();
}
}
This way the SoundPlayer is a shared member for all class methods. Note, though, the != null check, which makes sure that calling Stop before Play won't crash.
i am trying to use a WP speech library i got from codeplex, now the code is fine with no errors, but there are no voice output!!
using wpSpeech;
namespace TalkForMe
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SpeechTTS spk;
spk = new SpeechTTS ( textBox1.Text);
spk.SpeakLanguage = "en";
spk.SpeakText(textBox1.Text);
}
}
}
Your code has a very small mistake. You created a reference for SpeakTTS but didn't allocate any memory for it. try like this and try.
SpeechTTS spk=new SpeakTTS();
I am creating a WP7 application that requires various sound effects to be played (on button press) over looped background music. The background music is initiated by pressing Button 1 and loops fine. When I press button3 (triggers a sound effect), the sound effect overlays on the background music fine on first press. However, when I press button3 again, the background music stops. I cannot figure out why this might be happening!? I have pasted the relevant portions of code below. Would appreciate any help.
public partial class MainPage : PhoneApplicationPage
{
SoundEffect soundEffect;
Stream soundfile;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
{
SoundEffectInstance instance = soundEffect.CreateInstance();
instance.IsLooped = true;
FrameworkDispatcher.Update();
instance.Play();
}
}
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
effect.Play();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
soundfile = TitleContainer.OpenStream("BackgroundMusic.wav");
soundEffect = SoundEffect.FromStream(soundfile);
LoopClip(soundEffect);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
}
This should work if you are always working with Instances so change your code to this and it should clear up the problem:
public partial class MainPage : PhoneApplicationPage
{
SoundEffectInstance loopedSound = null;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
loopedSound = soundEffect.CreateInstance();
loopedSound.IsLooped = true;
loopedSound.Play();
}
public void PlaySound(string soundFile)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(soundFile, UriKind.Relative)).Stream);
SoundEffectInstance instance = sound.CreateInstance();
instance.Play();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(#"BackgroundMusic.wav", UriKind.Relative)).Stream);
LoopClip(sound);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
The above example assumes your sound files are set with Build Action = Content and are in the top level directory.
You will need to play each sound from a separate thread.
What seems to be happening here is that the different Play method calls are interfering with each other since they are in the same thread.
Try just putting the background music in a separate thread and see if that solves the problem you mention in the question. If so, split the others out as well.
I'm looking for solution to add snapping/sticky windows functionallity (winamp-like) to existing WPF application. Same thing as it was asked here, just I need it for WPF.
It doesn't have to have docking function, just to snap to border of other windows inside same application and edge of screen (including taskbar) if possible. Preferably open source solution.
Thanks
Here is the Solution you actually asked for:
Let's say we have 2 Xaml windows named MainWindow and Window2:
MainWindow:
Window2 windows2;
public void RealodPos()
{
if (windows2 == null) { windows2 = new Window2(this); this.Top = 300; }
windows2.Top = this.Top;
windows2.Left = this.Width + this.Left - 15;
windows2.Show();
}
private void Window_Activated(object sender, EventArgs e)
{
RealodPos();
}
private void SizeChenged(object sender, SizeChangedEventArgs e)
{
RealodPos();
}
private void LocationChange(object sender, EventArgs e)
{
RealodPos();
}
Window2:
public partial class Window2 : Window
{
MainWindow Firstwin;
public Window2(MainWindow FirstWindow)
{
InitializeComponent();
Firstwin = FirstWindow;
}
void RealodPos()
{
this.Top = Firstwin.Top;
this.Left = Firstwin.Width + Firstwin.Left - 15;
}
private void Window_Activated(object sender, EventArgs e)
{
RealodPos();
}
private void Window_LocationChanged(object sender, EventArgs e)
{
RealodPos();
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
RealodPos();
}
}
My suggestion as a Software engineer:
Hint1: I don't know where you will use this but it's better to convert it to a reusable component that is not hardcoded with only 2 windows.
Hint2: Convert the
public Window2(MainWindow FirstWindow)
's MainWindow argument to a Window class formant to have a more flexible pointer for reusing it in the other applications.
Here is my suggested Solution for pro-WPF developers:
instead of doing this in that way you can make your own customized windows on XAML and use UserControls instead of other windows that you need.
Thanks for reading, please ask me if you want anything else or if you need the code as a project file.
The WPF Docking Library may provide some of what you are looking for, but I'm unsure if it works on the entire screen or just on your application window.