none
Where Can Be Problem ???? (I will be crazy..) RRS feed

  • Question

  • Hi everyone,

     

    I have a problem..

     

     

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="SDKSample.MediaElementExample" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignWidth="220" SizeToContent="WidthAndHeight">
    
     <Window.Resources>
      <Storyboard x:Key="Board" CurrentTimeInvalidated="storyboard_CurrentTimeInvalidated">
       <MediaTimeline Source="c:\\test\\video1.mp4" Storyboard.TargetName="media"/>
      </Storyboard>
     </Window.Resources>
    
     <StackPanel Margin="10">
      <MediaElement Name="media" MediaOpened="media_MediaOpened"/>
      <Button Name="btnn" Content="Play sound" >
       <Button.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click">
         <EventTrigger.Actions>
          <BeginStoryboard Name="mediaStoryboard" Storyboard="{StaticResource Board}"/>
         </EventTrigger.Actions>
        </EventTrigger>
       </Button.Triggers>
      </Button>
      <Button Content="Button" Height="24" Name="button1" Width="111" Click="button1_Click" />
     </StackPanel>
    
    </Window>
    
    


     

    I want to create this button dinamically but I cant convert it to c# code..

     

    This is my code

     

     

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    
    namespace SDKSample
    {
    
     public partial class MediaElementExample : Window
     {
    
      Clock storyboardClock = null;
      bool IsMediaOpened = false;
      int counter = 0;
      MediaTimeline mediaTime;
      Storyboard board;
      MediaElement mediaElement;
      BeginStoryboard beginStory;
      public MediaElementExample () 
      {
       board = new Storyboard();
       mediaTime = new MediaTimeline();
       mediaElement= new MediaElement();
       beginStory = new BeginStoryboard();
       
    
       NameScope.SetNameScope(mediaElement,new NameScope());
    
    
     
       mediaElement.MediaOpened += new RoutedEventHandler(media_MediaOpened);
       mediaElement.RegisterName("media", mediaElement);
       
       board.Name ="storyBoard";
       board.CurrentTimeInvalidated += new EventHandler(storyboard_CurrentTimeInvalidated);
       mediaTime.Source = new Uri("c:\\test\\video1.mp4");
       Storyboard.SetTargetName(mediaTime,"media");
        
       board.Children.Add( mediaTime);
      }
      private void storyboard_CurrentTimeInvalidated(object sender, EventArgs e)
      {
       storyboardClock = (Clock)sender;
       
    
       if (IsMediaOpened && storyboardClock.CurrentTime != null)
       {
       //  sliderPosition.Value = storyboardClock.CurrentTime.Value.TotalSeconds;
       }
      
      }
    
      private void media_MediaOpened(object sender, RoutedEventArgs e)
      {
      
       this.IsMediaOpened = true;
      }
    
    
    
      private void button1_Click(object sender, RoutedEventArgs e)
      {
    
    
       TriggerBase a = btnn.Triggers[0];
       Button btn = (Button)sender;
       EventTrigger evt = new EventTrigger();
       TriggerAction action = beginStory;
    
       evt.RoutedEvent = e.RoutedEvent;
       evt.Actions.Add(action);
       btn.Triggers.Add(evt);
       board.Begin(mediaElement);
      }
    
      
    
      
    
     }
    }
    
    


    I want to play media with button1 click event.. But there is nothing..

    Please, Where is my mistake?? I am trying to solve this problem over 5 hours.. :)

     

    Thank you

     


    Thursday, June 23, 2011 3:51 PM

Answers

All replies

  • still alive :))
    Friday, June 24, 2011 12:15 AM
  • I don't know C#, and sorry if this doesnt help, but what is the "media" your putting in your parameters. And, just curious, is the code above part of the same window?

    Hope this helps



    Friday, June 24, 2011 1:43 AM
  • Thank you for answer,

    I think, media is a refference name of element in XAML. But I couldnt adapted it to C# code.. Actually, I dont know how can I use ...

    Friday, June 24, 2011 7:42 AM
  • What is the purpose of creating the button dynamicly ? 

    And apart from that, what is the purpose of the media player ?


    Developing is part of being a developer.
    Friday, June 24, 2011 8:41 AM
  • Actually,I dont need dinamic buttons. When form load I will call some method and follow 

    private void storyboard_CurrentTimeInvalidated(object sender, EventArgs e)


    I need changing of duration. Because I'll calculate total duration and after calculate 20% of total duration, and this time,I will start my fade in-out effect. My purpose is this... I done it to use timer, but its not good way.. I some event and to catch it...

     


    Friday, June 24, 2011 8:46 AM
  • I looked at your example, and could not make it out really.. So i used an old example and added a MediaDuration calculator, that you can use for your given purpose.

     

    <Window x:Class="WpfApplication1.Window2"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:WpfApplication1"
     Title="Window2" Height="300" Width="300"
     Background="Black">
     
     <Grid Name="MainGrid">
     
     <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition Height="30"/>
     </Grid.RowDefinitions>
     <MediaElement 
      Name="mElement"
      MouseDown="stateButton_MouseDown"
      Stretch="Fill"
      VerticalAlignment="Stretch"
      HorizontalAlignment="Stretch"
      MediaOpened="mElement_MediaOpened" 
      MediaFailed="MediaElement_MediaFailed"
      MediaEnded="mElement_MediaEnded"
      LoadedBehavior="Manual"
      UnloadedBehavior="Stop"/>
    
     <StackPanel
      Grid.Row="1"
      Orientation="Horizontal"
      HorizontalAlignment="Stretch">
      <Label Name="stateButton" 
       Content="Play"
       Foreground="White"
       FontWeight="Bold"
       FontSize="16"
       Width="75"
       MouseDown="stateButton_MouseDown"/>
      <Label
      Content="Stop"
      Foreground="White"
      FontWeight="Bold"
      FontSize="16"
      Width="75"
      MouseDown="stateButton_MouseDown"/>
     </StackPanel>
     </Grid>
    
    </Window>
    
    

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    namespace WpfApplication1
    {
     /// <summary>
     /// Interaction logic for Window2.xaml
     /// </summary>
     public partial class Window2 : Window
     { 
     public Window2()
     {
      InitializeComponent();
    
      Title = Name;
      if(System.IO.File.Exists(@"C:\video.wmv"))
      mElement.Source = new Uri(@"C:\video.wmv", UriKind.RelativeOrAbsolute);
      PlayElement();
     }
    
     //Opened Stopped Failed
     private void mElement_MediaOpened(object sender, RoutedEventArgs e)
     {
      stateButton.Content = "Pause";
      MessageBox.Show("Length Measured in Seconds\nMedia Length: " + mElement.NaturalDuration.TimeSpan.TotalSeconds
      + "\nPercentage(20%): " + CalculateDuration(20));
     }
     private void mElement_MediaEnded(object sender, RoutedEventArgs e)
     {
      mElement.Visibility = System.Windows.Visibility.Hidden;
      stateButton.Content = "Play";
      SetStatusLabel("Media Ended...");
     }
     private void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
     {
      mElement.Visibility = System.Windows.Visibility.Hidden;
      stateButton.Content = "Play";
      SetStatusLabel("Failed to load...");
     }
    
     private void stateButton_MouseDown(object sender, MouseButtonEventArgs e)
     {
      if (((UIElement)sender).GetType() == typeof(Label))
      switch (((Label)sender).Content.ToString())
      {
       case "Play":
       stateButton.Content = "Pause";
       PlayElement();
       break;
       case "Pause":
       stateButton.Content = "Play";
       PauseElement();
       break;
       case "Stop":
       StopElement();
       break;
      }
      else
      {
      if (stateButton.Content.ToString() == "Play")
      {
       stateButton.Content = "Pause";
       PlayElement();
      }
      else
      {
       stateButton.Content = "Play";
       PauseElement();
      }
      }
     }
    
     //Play Stop Pause
     private void PlayElement()
     {
      SetStatusLabel(null);
      mElement.Visibility = System.Windows.Visibility.Visible;
      mElement.Play();
     }
     private void StopElement()
     {
      mElement.Stop();
      mElement.Visibility = System.Windows.Visibility.Hidden;
      stateButton.Content = "Play";
      SetStatusLabel("Media Stopped...");
     }
     private void PauseElement()
     {
      mElement.Pause();
     }
    
     //Set Background Label
     void SetStatusLabel(string s)
     {
      for (int i = 0; i < MainGrid.Children.Count; ++i)
      {
      if (MainGrid.Children[i] is Label)
       MainGrid.Children.RemoveAt(i);
      }
    
      MainGrid.Children.Add(
      new Label
      {
      Content = s,
      Foreground = new SolidColorBrush(Colors.LightGray),
      HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
      VerticalAlignment = System.Windows.VerticalAlignment.Center,
      FontSize = 44
      });
     }
    
     //Calculate MediaDuration
     double CalculateDuration(int ReturnedPercentage)
     {
      return mElement.NaturalDuration.TimeSpan.TotalSeconds / 100 * ReturnedPercentage;
     }
     }
    }
    
    

    I hope this will be a good assistance in achieving your goal.
    If you have any questions regarding, do not hesitate to ask ! :) 

     

    NOTE: This example does not contain the animation part of fading in and out. But it can probably be implemented
              easily. So if you are unable to do so, just say so !  

     


    Developing is part of being a developer.

    Friday, June 24, 2011 9:14 AM
  • Thank you SharpAspirant, But it s not what I look.. Because you just calculate duration one time. I need current time and without timer. Like media changing event. But your function calculates when media opened. I need some event to calculate current time at run time

    I hope I explained it now.

    By the way, I solved my problem

    I added this kode         x:Name="mainWind"

     

     

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      x:Class="SDKSample.MediaElementExample" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignWidth="220" SizeToContent="WidthAndHeight"

            x:Name="mainWind"

    ....

     

     

    and after

     

     

     mainWind.mediaTime = mediaTime;

                board = mainWind.Resources["Board"] as Storyboard;

     

     

    i can run video wtih this code. But still 

    <MediaTimeline Source="c:\\test\\video1.mp4" Storyboard.TargetName="media"/>

    this mediatimeline actives. I dont understand why. I created some mediaTime object and assign path of video but it doesnt work

     

     

          board = new Storyboard();
    
         board.CurrentTimeInvalidated += new EventHandler(storyboard_CurrentTimeInvalidated);
    
          mediaTime.Source = new Uri("c:\\test\\video3.wmv");
    
          mediaElement.Source = new Uri("c:\\test\\video3.wmv");
      
    
         board.Children.Add( mediaTime);
    

    how can I change xaml code

    Friday, June 24, 2011 9:28 AM
  • Hi,

    I'm wondering if you want to implement a media player through WPF.

    If you do, you might reference the following link : A simple WPF media player with media item list.

    By the way, would you please discribe your problem more exactly?

    I'd like to help, but I could not figure out what the storyboard in your sample code for.

    I guess, you storyboard is intended to display the time of the media file which is playing, if so, you might try to implement it through the MediaElement.Position property.

     

    May this information is helpful to you.

     

     


    Ouch Liu
    Welcome to visit by blog: Ouch@點部落
    Welcome to join the Designer x Developer group on Facebook: Facebook 設計x程式 社團
    Friday, June 24, 2011 9:57 AM
  • Thank you for attentin. Okay I will explain what I want

     

    I have two media elements. And I want to fade in-out effect between these elements. 

    For example,

    first media element's opacity is 1, second one is 0

    media of first element duration is 4:00  When this media is in 3:45 I want to descrease value of opacity of first media element  and also want to increase value of opacity of second media element. In this case,It will be like fade out  (first media) , fade in (second media) effect.

    Therefore, I need current time of first media to calculate "3:45 " I did it to use timer, but I dont want to use timer. I found MediaTimeLine for it, but I cant use it ....

     

    I hope now its more exactly what I mean

     

    Thank you

    Friday, June 24, 2011 10:05 AM
  • Okay I solved this problem:) Thanks who answered me :) I will share my code on monday, It is in another comp. 

    Friday, June 24, 2011 4:36 PM
  • Hi ertan2002,

    Glad to see you have worked it out! It would be appreciated if you could post your solution here for members who may have similiar issues.


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 10:07 AM
    Moderator
  • Tuesday, June 28, 2011 10:09 AM