How to run video with mediaelement in windows phone 8 app - c#

I am making a windows phone app, I want to run the video whenever the button is pressed. I am using mediaelement to run it, here is the code:
XAML:
<MediaElement x:Name="myMediaElement"
Margin="0,40,0,40"
Height="400"
Width="240" />
<Button x:Name="playVideoButton"
Height="80"
Width="200"
Content="Play Video"
Click="playVideoButton_Click"/>
C#:
private void playVideoButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Source = new Uri("ms-appx:///Assets/Video.mp4", UriKind.RelativeOrAbsolute);
myMediaElement.Play();
}
this is the basic code that i am trying to run the video so if this one runs i'll add pause feature as well, but unfortunately its not running.
Please help and thanks in Advance!
Shahrukh

you can refer a Nokia Developer Sample about Video Playback with MediaElement in Windows Phone or you can Use Media Player launcher instead. here is a MSDN Documentation about it. How to use the Media Player launcher for Windows Phone 8

Related

UWP Keyboard Accelerator FN issue

<Button
x:Name="PlayButton"
Click="PlayButton_Click"
Style="{StaticResource MediaControlButtonStyle}">
<Button.Content>
<FontIcon
x:Name="PlayButtonIcon"
FontSize="30"
Glyph="" />
</Button.Content>
<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="F3" />
</Button.KeyboardAccelerators>
</Button>
I want to use F3 to play/pause the music in my UWP app. However, simply pressing F3 doesn't work on my Surface Book 2. I need to press both FN and F3 to make it work. What should I do so that I only need to press F3? The Microsoft builtin UWP app Groove Music plays and pauses functionally with pressing F3 only.
Another question is that how can I still use keyboard accelerator when the window of UWP app is minimized? The keyboard control of Groove still works with its window minimized.
It seems the behavior -- need to press FN or not,is related to the device that installed the app.When I run in PC,I only need to press F3,but it requires FN on the surface.If you still wants to change it,I have a workaround below,you can register Accelerator events on the page (e.g. MainPage).
public MainPage()
{​
this.InitializeComponent();​
Window.Current.Dispatcher.AcceleratorKeyActivated += AccelertorKeyActivedHandle;​
}
private void AccelertorKeyActivedHandle(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{​
if (args.EventType.ToString().Contains("Down"))​
{ ​
if (args.VirtualKey == Windows.System.VirtualKey.F3)​
{​
// do something you want
​
}​
}​
}
When the window of UWP app is minimized,the current window has no focus, so the set shortcuts will not be responded.But the system has its default shortcuts(e.g. Fn + F11) can play or pause media.If you want to respond the system shortcuts,you need to allow backgroundMediaPlayback.You can try the official demo, which can also be controlled by the media button that comes with the keyboard.
Update:
According to the official sample,if you want to use MediaElement to play,you should set MediaPlay and MediaPlaybackList to bind the playback list.In this case, it seems can be controlled by the system default shortcut keys.What actually works is MediaPlayer.For more detailed information, you still need to read the official demo.
XAML:
<MediaElement Name="mediaPlayerElement"
AreTransportControlsEnabled="True" ​
Stretch="UniformToFill" Height="100" Width="400">
Code-behind:
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{​
var source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/xxx"));​
​
// Create a configurable playback item backed by the media source​
var playbackItem = new MediaPlaybackItem(source);​
MediaPlayer player = new MediaPlayer();​
MediaPlaybackList lists = new MediaPlaybackList();​
lists.Items.Add(playbackItem);​
player.Source = lists;​
​
}

How to change source property of MediaElement from code behind in windows store apps

I am developing a windows store app through c#, in which i am using a mediaElement control.
There are some images in the application. I want to change the source on particular image clicks. I hv tried this-
media.Source = new Uri("Assets//XyZ.mp3", UriKind.Relative);
<MediaElement x:Name="media" Source="Assets/Always_Up.mp3"
AutoPlay="True" AreTransportControlsEnabled="True" />

windows phone 8 wav does not run when app is run

I was trying to implement the sample app of running a wav file as shown in the microsoft virtual academy tutorial for beginners. I tried implement same code and ran app but my app does not play sound of wav file. I am running the app on emulator. and following is the code i implemented in xaml file:
<MediaElement x:Name="Quack" HorizontalAlignment="Left" Height="128" Margin="314,218,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100"
Source="/Assets/quack.wav"
AutoPlay="True" Balance="1" Volume="1"/>
following is code in xaml.cs file
private void buttonQuack_Click(object sender, RoutedEventArgs e)
{
textQuack.Text = "Hear the sound";
QuackMediaElement.Play();
}
I just wondered if is there any setting required in emulator in order to enable volume or something

Equivalent to adControl.HasAd in windows phone 8.0

In a Windows Phone 8.1 universal app, I could use this Code for a Pub Center ad.
//code behind:
if(adControl.HasAd)
{
}
//xaml:
<UI:AdControl x:Name="adControl" AutoRefreshIntervalInSeconds="60" ApplicationId="" AdUnitId="" HorizontalAlignment="Left" Height="70" IsAutoRefreshEnabled="True" VerticalAlignment="Top" Width="400" Foreground="Black" Background="Black"/>
But this function isn't avaible for a Silverlight App. Does someone know a equivalent to "adControl.HasAd" in a Windows Phone 8.0 App?
I don't think there is an equivalent in WP8.0. What you can do though is have your own bool and set/unset it with the following two events:
adControl.AdRefreshed:
Raised when the AdControl receives a new ad.
and
adControl.ErrorOccured:
Raised when the AdControl receives a new ad.
If you just want to collapse the advert when no ad is shown you can also use:
adControl.IsAutoCollapseEnabled = true;

Can I have two mediaelements on a single page, one will always play some background music and second will play some sound depending upon user action

I am working on windows store app (using xaml/C#), my question is can I have two media-elements on a single page, one will always play some background music and second will play some other sound depending upon user actions?
I tried it but not working for me, only background music is getting played the other sound is not getting played.
I can't see problem. you can use 2 media elements on the same page. mediaControl1 plays always and mediaControl2 starts to play when user clicks on button.
<Button Width="150" Height="150" Style="{StaticResource ButtonAppStyle}" Click="MainPage_Loaded">
<MediaElement x:Name="mediaControl1" Height="400" Source="Assets/muzika.mp3" RealTimePlayback="True" IsLooping="True" />
<MediaElement x:Name="mediaControl2" Source="Assets/firework1.wav" IsMuted="True"/>
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
mediaControl2.IsMuted = false;
mediaControl2.Play();
}

Categories

Resources