Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I'm trying to create a media element from code on a button press.
Ive created a xaml media element which works fine but when i try to create a media element from c# code it will not work.

Here is my Xaml.

mediaelement name="meXAML" loadedbehavior="Manual"




And the code behind

public partial class MainWindow : Window
    {
        MediaElement meC;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnCode_Click(object sender, RoutedEventArgs e)
        {
            meC = new MediaElement();
            meC.LoadedBehavior = MediaState.Manual;
            meC.Source = new Uri("C:/Music/10cc/10cc - Rubber Bullets.mp3", UriKind.Relative);
            meC.Play();
        }
        private void btnXaml_Click(object sender, RoutedEventArgs e)
        {
            meXAML.Source = new Uri("C:/Music/10cc/10cc - Rubber Bullets.mp3", UriKind.Relative);
            meXAML.Play();
        }
        
    }


As far as I can see both media elements are set the same only using different methods.

The XAML media element plays fine but the code generated one will not.

Any help will be greatly appreciated.
Posted
Updated 30-Jul-10 7:52am
v4

Your manual media element is not part of your page. Are you looking for a popup media element ? Also, your Uri is not relative, it's absolute. Relative would mean that the path you give does not have an absolute root, but navigates from the folder your app is in.
 
Share this answer
 
Thank you very much.

i modified the code to add the mediaelement to a stack panel in my page.

private void btnCode_Click(object sender, RoutedEventArgs e)
        {
            meC = new MediaElement();
            meC.LoadedBehavior = MediaState.Manual;
            mystackpanel.Children.Add(meC);
            meC.Source = new Uri("C:/Music/10cc/10cc - Rubber Bullets.mp3", UriKind.Absolute);
            meC.Play();
        }


I'm still new to WPF and I was stuck on this one for a bit.

Thanks again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900