zoukankan      html  css  js  c++  java
  • 学习wpf播放视频音频的两种不同方法

    Window1.xaml代码

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
              <Button Content="播放歌曲" Height="23" Click="button1_Click" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Name="button1"
    Width="75" />

            <Button Content="播放歌曲2" Height="23" Click="button2_Click" HorizontalAlignment="Right" Margin="0,12,54,0" Name="button2" VerticalAlignment="Top" Width="75" />
       
         <MediaElement Name="McMediaElement"  LoadedBehavior="Manual"/> <MediaElement Name="McMediaElement" LoadedBehavior="Manual"/> </Grid> </Window>

     Window1.xaml.cs代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                //用MediaPlayer类进行播放
                MediaPlayer player = new MediaPlayer();
                player.Open(new Uri(Environment.CurrentDirectory + "\\music.mp3", UriKind.Relative));
                VideoDrawing aVideoDrawing = new VideoDrawing();
                aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
                aVideoDrawing.Player = player;
     
                player.Play(); 
            }
    
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                //用MediaElement控件进行播放
                McMediaElement.Source = new Uri(Environment.CurrentDirectory + "\\music.mp3");
                McMediaElement.Play(); 
            }
        }
    }
    其中Environment.CurrentDirectory表示和项目生成的exe同一目录,请将视频或音频文件copy至此目录
    

     注意哦:按这样做下来音乐播放不了,呵呵,把开你的文件夹找到WPFvideo\bin\Debug

    把歌曲放到里面就可以播放了,

    转自http://www.cnblogs.com/prolifes/articles/1372586.html

  • 相关阅读:
    LeetCode_35.搜索插入位置
    LeetCode_349.两个数组的交集
    LeetCode_344.反转字符串
    LeetCode_34.在排序数组中查找元素的第一个和最后一个位置
    LeetCode_303.区域和检索
    LeetCode_3.无重复字符的最长子串
    LeetCode_292.Nim 游戏
    LeetCode_283.移动零
    LeetCode_27.移除元素
    LeetCode_268.丢失的数字
  • 原文地址:https://www.cnblogs.com/lilo202/p/2478667.html
Copyright © 2011-2022 走看看