How to play the remote audio file using MediaElement in xaml - c#

The audio is playing well when giving the local audio file as the source for the MediaElement.
<MediaElement Grid.Row="1" Stretch="Uniform" Name="Player" Margin="0,93,0,0" Source="Assets/test.mp3" />
But it is not working when try to play the remote audio file.
In the MainPage.xaml.cs
Player.Source = new Uri("http://fileraja.com/tamil/A/Alaipayuthey/Pachchai_Nirame-VmusiQ.Com.mp3", UriKind.RelativeOrAbsolute);
Please give me a solution. How to set the source for the MediaElement for a remote audio?

I Used to add MediaElement to for ex. MainPage.xaml as:
<MediaElement Name="MediaContent" AutoPlay="True"/>
and in MainPage.cs Constructor or OnNavigateTo i Use:
MediaContent.Source = new Uri("Ur Uri String Here", UriKind.Absolute);
if you don't want to auto play it in page loading just set AutoPlay to False, and play it on button click -for ex.- or any other event like that:
MediaContent.Play();
if it doesn't work try to use:
MediaFailed, MediaOpened, MediaEnded ,CurrentStateChanged .. events to keep track on why it doesn't work.

Related

Cannot play a relative mp4 video in a MediaElement WPF

I'm trying to play a mp4 video in my media element in WPF.
The mp4 video is in a folder in my solution.
I tried diffrent things but the only way it works is when i put the full path to the video in the uri. What am i doing wrong?
XAML
<MediaElement x:Name="VideoDice" Grid.Row="2" Grid.Column="2" LoadedBehavior="Manual" MediaEnded="VideoDice_MediaEnded" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
C#
VideoDice.Source = new Uri("DiceMovies/Dice_2.mp4", UriKind.Relative);
VideoDice.Height = 500;
VideoDice.Width = 500;
VideoDice.Play();
In your project for the Dice_2.mp4 file it's necessary to set Copy to Output Directory property to Copy if newer. Looks like your .mp4 file does not copied to the output directory automatically.

Problems figuring out playing wav file

The task should be simple. I have an Image with a "Tapped" even handler, and i want to play a wav file when it is clicked.
<Image x:Name="Snd_abort_1" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Source="Assets/Tiles/A/abort_1.png" Tapped="Snd_abort_1_Tapped"></Image>
private void Snd_abort_1_Tapped(object sender, TappedRoutedEventArgs e)
{
MediaElement myMediaElement = new MediaElement();
myMediaElement.AutoPlay = false;
myMediaElement.Source = new Uri("ms-appx:///Assets/Sounds/DukeNukem/abort.wav");
myMediaElement.Play();
}
For some particular reason this does not work.
I tried to debug the click and that fires but no error, no sound.
I have tried both in emulator and on my Lumia 930 device.
What am i missing out?
You don't need to prefix your files with ms-appx:, just do
myMediaElement.Source = new Uri("Assets/Sounds/DukeNukem/abort.wav", UriKind.Relative);
myMediaElement.Play();
Unfortunately, though it is correct. This code-behind approach will still not work. Because the myMediaElement is not part of the VisualTree (because you made it dynamically).
To fix this, just add it to the root Grid/Panel
myMediaElement.Source = new Uri("Assets/Sounds/DukeNukem/abort.wav", UriKind.Relative);
this.ContentPanel.Children.Add(myMediaElement);
myMediaElement.Play();
Now it should play nicely with everything.

Playing HLS (m3u8 playlist) on Windows Phone 8.1

A friend and I have tried to get the video player on Windows Phone 8.1 to play a m3u8 stream, but we've been unavailable to succeed.
What we've tried:
We've tried with playerframework.codeplex.com (Microsoft Player Framework), but it was unable to load the file.
We also tried with Windows Phone Streaming Media (https://phonesm.codeplex.com/), but we were unable to as much as use this one as we couldn't make sense of their documentation on how we actually had to load the file?
Is there anybody who have worked with this kind of files before? I understand that m3u8 is not natively supported by Windows Phone 8.1
Download the player framework, consume the following DLL's:
Add the player to your xaml:
xmlns:mmppf="using:Microsoft.PlayerFramework"
xmlns:smmedia="using:SM.Media.MediaPlayer"
<mmppf:MediaPlayer IsFullScreenVisible="True" IsFullScreenEnabled="True" IsFullScreen="False" CurrentStateChanged="mPlayer_CurrentStateChanged" x:Name="mPlayer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsFastForwardEnabled="False" IsInfoEnabled="False" IsLive="True" IsMoreEnabled="False" IsRewindEnabled="False" IsRightTapEnabled="False" IsScrubbingEnabled="False" IsSeekEnabled="False" IsSkipBackEnabled="False" IsSkipAheadEnabled="False" IsReplayEnabled="False" IsTimelineVisible="False" IsTimeElapsedVisible="False" IsTimeRemainingVisible="False" RequestedTheme="Dark">
<mmppf:MediaPlayer.Plugins>
<smmedia:StreamingMediaPlugin />
</mmppf:MediaPlayer.Plugins>
</mmppf:MediaPlayer>
Then set your stream VIA code - or XAML if the URL never changes.
#Mahesh Vemuri asked what if he has error that says StreamingMediaPlugin is not available or not found in namespace, here is my work around:
XAML:
xmlns:PlayerFramework="using:Microsoft.PlayerFramework"
<PlayerFramework:MediaPlayer Name="player"
Source="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
AudioCategory="BackgroundCapableMedia"
IsAudioSelectionVisible="True">
<PlayerFramework:MediaPlayer.Plugins>
</PlayerFramework:MediaPlayer.Plugins>
</PlayerFramework:MediaPlayer>
And in your .xaml.cs file you simply do this:
SM.Media.MediaPlayer.StreamingMediaPlugin asd = new SM.Media.MediaPlayer.StreamingMediaPlugin();
player.Plugins.Add(asd);
player.Source = new Uri("address-to-m3u8");
It worked for me since "default" way didn't. Hope it helps someone else, too.
you can add them from xaml or cs. First add reference.
XAML
xmlns:local="clr-namespace:Microsoft.PlayerFramework;assembly=Microsoft.PlayerFramework"
xmlns:smmedia="clr-namespace:SM.Media.MediaPlayer;assembly=SM.Media.MediaPlayer.WP8"
<local:MediaPlayer Name="player"
HorizontalContentAlignment="Stretch"
AutoPlay="True"
Volume="0.7"
Source="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
IsPlayPauseVisible="True">
<local:MediaPlayer.Plugins>
<smmedia:StreamingMediaPlugin />
</local:MediaPlayer.Plugins>
</local:MediaPlayer>
XAML & CS
xmlns:local="clr-namespace:Microsoft.PlayerFramework;assembly=Microsoft.PlayerFramework"
<local:MediaPlayer Name="player"
HorizontalContentAlignment="Stretch"
AutoPlay="True"
Volume="0.7"
IsPlayPauseVisible="True">
</local:MediaPlayer>
SM.Media.MediaPlayer.StreamingMediaPlugin asd = new SM.Media.MediaPlayer.StreamingMediaPlugin();
player.Plugins.Add(asd);
player.Source = new Uri("http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8");

Locating and reading a file stored locally

I have a metro app and I have added an existing file into a folder inside solution explorer. I have read the resources docs on msdn but can't get it figured out.
My sound file is located in Assets\SFX\Standard.wav, so how would I locate and play this file using MediaElement in my app from codebehind?
I am not sure why you would want to use MediaElement, perhaps you could try SoundPlayer?
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "\Assets\SFX\Standard.wav";
player.Play();
Edit with MediaElement:
<MediaElement Name="TehSoundz" IsLooping="False" AutoPlay="False" Height="0" Width="0" Source="\Assets\SFX\Standard.wav" />
Codebehind:
TehSoundz.Play();
TehSoundz.Stop();

WinRT C# Play a viddler video using MediaElement

I am trying to play a Viddler video in my application but i cant get it to work.
Here is my simple code:
XAML:
< MediaElement VerticalAlignment="Center" Visibility="Visible"
HorizontalAlignment="Center"
Name="myMediaElement" Height="350"
Width="640" />
And my c#:
myMediaElement = new MediaElement();
Uri url = new Uri("http://www.viddler.com/embed/5b17d44f/");
myMediaElement.Source = url;
myMediaElement.Play();
Any help would be great! When i arrive on the page, nothing happens, the application does not break it just does nothing....
Edit:
In the end I have just decided to call the http://www.viddler.com/embed/5b17d44f/ url into a Webview, its not the best idea but it works.
Dont create a new MediaElemet. You already have one created (XAML).
Set AutoPlay="True" in your XAML
Edit: Play works only if the media is already loaded.

Categories