.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > How to play mp3 stream using MeidaElement class in WPF projcet?
Ask a questionAsk a question
 

AnswerHow to play mp3 stream using MeidaElement class in WPF projcet?

  • Monday, November 02, 2009 2:49 AMthinc123321 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I wonder that MediaElement (from WPF) components are capable of playing and buffering mp3 streams.

    I need play mp3 memory stream(not mp3 file) using MediaElement of WPF ,would you please tell me how to do it .

    thank a lot

Answers

  • Wednesday, November 04, 2009 9:08 AMHua ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello thinc123321,

      Here is a way we can choose.

      Save the memory stream in a temp file.

      Set the Source of MediaElement to the temp file.
      
      Sample code:

                //MemoryStream st;
    
    
                long length = st.Length;
    
                byte [] data = new byte[length];
    
                st.Read(data, 0, (int)length);
    
                FileStream fs = new FileStream("TempPlayingMedia.mp3", FileMode.Create);
    
                fs.Write(data, 0, (int)length);
    
                fs.Flush();
    
                fs.Close();
    
                instMedia.LoadedBehavior = MediaState.Manual;
    
                instMedia.Source = new Uri("TempPlayingMedia.mp3", UriKind.Relative);
    
                instMedia.Play();
    
    

      
      Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help

All Replies

  • Wednesday, November 04, 2009 9:08 AMHua ChenMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello thinc123321,

      Here is a way we can choose.

      Save the memory stream in a temp file.

      Set the Source of MediaElement to the temp file.
      
      Sample code:

                //MemoryStream st;
    
    
                long length = st.Length;
    
                byte [] data = new byte[length];
    
                st.Read(data, 0, (int)length);
    
                FileStream fs = new FileStream("TempPlayingMedia.mp3", FileMode.Create);
    
                fs.Write(data, 0, (int)length);
    
                fs.Flush();
    
                fs.Close();
    
                instMedia.LoadedBehavior = MediaState.Manual;
    
                instMedia.Source = new Uri("TempPlayingMedia.mp3", UriKind.Relative);
    
                instMedia.Play();
    
    

      
      Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help