Locating and reading a file stored locally - c#

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();

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.

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");

How to play the remote audio file using MediaElement in xaml

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.

How to play swf file in silverlight?

I am tring to play a .swf in silverlight5 page.I am using the ListBox control or Image Control
Or Any Idea To Play swf File in Silvarlight
<Image Grid.Row="0" Name="bottom_video" Height="80" Source="{Binding VodeoUrl,Mode=OneWay}" Margin="0,20,0,0" />
You might be able use the the Flash ActiveX control that is automatically installed with the flash player itself on every windows machine but if it works it'd still comes with a lot of limitations as stated here another problem will be the "Airspace" problem.
Another way is to convert the SWF to XAML using this. But I found quite a lot of swf files that couldn't be converted. Especially if there is a lot of ActionScript or dynamic loading of content involved.
You can see in the code below how to use Iframe on Silverlight:
<Grid x:Name="LayoutRoot">
<HyperlinkButton Content="HyperlinkButton" Height="23" HorizontalAlignment="Left" Margin="44,20,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="100" TargetName="" Click="hyperlinkButton1_Click" />
</Grid>
code behind:
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e) {
HtmlDocument _document = HtmlPage.Document;
HtmlElement iframe = _document.CreateElement("IFRAME");
iframe.SetAttribute("src", "http://localhost:52878/TestForm.aspx");
iframe.SetStyleAttribute("position", "absolute");
iframe.SetStyleAttribute("top", "100px");
iframe.SetStyleAttribute("left", "200px");
HtmlElement body = (HtmlElement)_document.GetElementsByTagName("BODY")[0];
body.AppendChild(iframe);
HtmlDocument _document = HtmlPage.Document;
HtmlElement iframe = _document.CreateElement("IFRAME");
iframe.SetAttribute("src", "http://localhost:52878/TestForm.aspx");
iframe.SetStyleAttribute("position", "absolute");
iframe.SetStyleAttribute("top", "100px");
iframe.SetStyleAttribute("left", "200px");
HtmlElement body = (HtmlElement)_document.GetElementsByTagName("BODY")[0];
body.AppendChild(iframe);
}
I've got the answer for how to play an swf file in Silverlight 5
<WebBrowser Grid.Row="1" VerticalAlignment="Top" Source="{Binding VodeoUrl,Mode=OneWay}" Height="280" Width="280"/>
This is impossible. Silverlight does not play swf files. If it is just a video file without any interaction, you might be able to convert the video to something that Silverlight can play. Here is some information about the supported formats:
http://msdn.microsoft.com/en-us/library/cc189080%28v=vs.95%29.aspx

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