临时2017-6-19 00:02:03

说明(2017-6-19 00:02:18):

视频转PPT,与github有点冲突,先把代码存起来:

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;
using System.IO;
using System.Windows.Threading;

namespace _10视频转换
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //先定义一个字典,存储图片名字和时间
        private Dictionary<int, string> imageAndTime = new Dictionary<int, string>();
        //定义一个泛型存放时间,用于拖动进度条时前后比较
        List<int> imageTimes = new List<int>();
        //private string imagePath = @"E:\AppServ\www\github\MyWork\trunk\VideoToAudio\VideoToAudio\bin\Debug\";
        private string imagePath = @"F:\MyWork\MyWork\trunk\VideoToAudio\VideoToAudio\bin\Debug\img\";
        //音乐总时间
        private double totalTime;

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 主窗体加载啦
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //获取目录下所有图片名字列表并计算时间,存入字典
            GetImages();
            //显示图片
            //存储所有时间
            foreach (var item in imageAndTime)
            {
                imageTimes.Add(item.Key);
            }
            //测试时间存进来没有
            //foreach (var item in imageTimes)
            //{
            //    Console.WriteLine(item);
            //}
            //string imageName = null;
            //string imagePath = @"E:\AppServ\www\github\MyWork\trunk\VideoToAudio\VideoToAudio\bin\Debug\" + imageName;
            //image1.Source = new BitmapImage(new Uri(@"E:\AppServ\www\github\MyWork\trunk\VideoToAudio\VideoToAudio\bin\Debug\1.jpg", UriKind.RelativeOrAbsolute));
            image1.Source = new BitmapImage(new Uri(imagePath + imageAndTime.Values.First(), UriKind.RelativeOrAbsolute));

            //大爷的!直接窗体加载就能自动播放!
            myMusic.Play();

        }

        /// <summary>
        /// 定义计时器,WPF居然没有timer这个控件!
        /// </summary>
        DispatcherTimer timer = new DispatcherTimer();
        /// <summary>
        /// 悲剧的手写timer事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_tick(object sender, EventArgs e)
        {
            //游标跟随播放时间移动
            //totalTime = myMusic.NaturalDuration.TimeSpan.TotalSeconds;
            if (myMusic.Position.TotalSeconds == totalTime)
            {
                myMusic.Stop();
                //播放完,游标还没跳到开始,按钮就变成“播放”了,阻断进程也不行。
                //System.Threading.Thread.Sleep(2000);
                btnPlayOrPause.Content = "播放";
            }
            else
            {
                //进度条位置等于音乐播放到的位置
                slider1.Value = myMusic.Position.TotalSeconds;
                //显示进度时间,后期要进行计算,符合时间格式
                lbTime.Content = "00:" + Math.Floor(myMusic.Position.TotalSeconds);
            }

            //图片跟随播放时间变换
            int bingoTime = (int)Math.Floor(slider1.Value);
            if (imageAndTime.ContainsKey(bingoTime))
            {
                image1.Source = new BitmapImage(new Uri(imagePath + imageAndTime[bingoTime], UriKind.RelativeOrAbsolute));
            }
            //foreach (var item in imageAndTime)
            //{
            //if (item.Key == Math.Floor(slider1.Value))
            //    {
            //        image1.Source = new BitmapImage(new Uri(imagePath + item.Value, UriKind.RelativeOrAbsolute));
            //    }
            //}
        }

        /// <summary>
        /// 播放器加载啦
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void myMusic_MediaOpened(object sender, RoutedEventArgs e)
        {
            //myMusic.Source = new Uri(@"F:\MyWork\MyWork\trunk\VideoToAudio\VideoToAudio\bin\Debug\LoveStory.mp3", UriKind.RelativeOrAbsolute);
            //获取歌曲总时长
            totalTime = myMusic.NaturalDuration.TimeSpan.TotalSeconds;
            slider1.Maximum = totalTime;
            //下面3行是抄的,大意为:设置计时器时间间隔为1秒,把上面的timer_tick方法添加进timer.Tick事件,计时器启动
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timer_tick);
            timer.Start();
            Console.WriteLine(totalTime);
        }

        /// <summary>
        /// 获取文件夹里所有图片名,暂时使用字符串截取,后期加上正则识别。
        /// 换了一个方法,直接获得文件名。
        /// 使用字典存储图片名和时间,返回这个字典。
        /// </summary>
        private void GetImages()
        {

            //string imagePath = @"E:\MyWork\My\10视频转换\bin\Debug";
            //string[] files = Directory.GetFiles(imagePath, "*.jpg");
            //List<string> imageNames = new List<string>();
            //foreach (var file in files)
            //{
            //    imageNames.Add(file.Substring(file.Length-10,10));

            //}
            //foreach (var imageName in imageNames)
            //{
            //    Console.WriteLine(imageName);
            //}
            //下面的方法可以直接获取文件名,不用正则或字符串处理。
            DirectoryInfo folder = new DirectoryInfo(imagePath);
            FileInfo[] imageNames = folder.GetFiles("*.jpg");
            //获取秒数,存入imageTimes集合。后来发现不好,采用了字典。
            //List<int> imageTimes = new List<int>();
            //foreach (FileInfo file in imageNames)
            //{
            //    if (file.Name.Length == 10)
            //    {
            //        int min = Convert.ToInt32(file.Name.Substring(0, 2));
            //        int sec = Convert.ToInt32(file.Name.Substring(2, 2));
            //        imageTimes.Add(min * 60 + sec);
            //    }
            //}

            //测试,输出每个图片文件名的秒数
            //foreach (var imageTime in imageTimes)
            //{
            //    Console.WriteLine(imageTime);
            //}
            //需要把图片名和对应时间作为键值对存入json,例如{"001580.jpg":15, "010425":64}
            //算了用字典吧!
            //Dictionary<string, int> imageAndTime = new Dictionary<string, int>();
            foreach (var imageName in imageNames)
            {
                //筛选一下,10个字符的才是图片名字的正确格式
                if (imageName.Name.Length == 10)
                {
                    int min = Convert.ToInt32(imageName.Name.Substring(0, 2));
                    int sec = Convert.ToInt32(imageName.Name.Substring(2, 2));
                    int imageTime = min * 60 + sec;
                    imageAndTime.Add(imageTime, imageName.Name);
                }
            }
            //return imageAndTime;
            //输出字典测试一下
            //foreach (var item in imageAndTime)
            //{
            //    Console.WriteLine(item.Key);
            //    Console.WriteLine(item.Value);
            //}

        }

        /// <summary>
        /// 拖动进度条,改变播放时间,保留一句代码,拖动时有用。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            myMusic.Position = TimeSpan.FromSeconds(slider1.Value);
            ////拖动进度条的时候,判断该显示哪张图片,两个时间之间{"001580.jpg":15,"001974.jpg":19}

            //foreach (var item in imageAndTime)
            //{
            //    if (item.Value == Math.Floor(slider1.Value))
            //    {
            //        image1.Source = new BitmapImage(new Uri(imagePath + item.Key, UriKind.RelativeOrAbsolute));
            //    }
            //    //此处有问题,提示索引不能为负或超出索引。slider_valuechange事件,是不是自己动也算?不仅仅是拖动?
            //    //else
            //    //{
            //    //    for (int i = 0; i < imageTimes.Count; i++)
            //    //    {
            //    //        if (Math.Floor(slider1.Value) < imageTimes[i])
            //    //        {
            //    //            MessageBox.Show(imageTimes[i - 1].ToString());
            //    //        }
            //    //    }
            //    //}

            //}
            //slider1.Value = myMusic.Position.TotalSeconds;
            //myMusic.Position = slider1.Value;
            //slider1.AddHandler();
        }

        /// <summary>
        /// 鼠标在slider上按下,处理播放进度的正确方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void slider1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point p = Mouse.GetPosition(slider1);
            int pX = Convert.ToInt32(p.X);
            //MessageBox.Show(pX.ToString());
            slider1.Value = pX / slider1.Width * slider1.Maximum;
            myMusic.Position = TimeSpan.FromSeconds(slider1.Value);
            //拖动进度条的时候,判断该显示哪张图片,两个时间之间{"001580.jpg":15,"001974.jpg":19}

            //Dictionary<string, int> imageAndTime的格式为{"001580.jpg":"15", "001974.jpg":"19"}

            //不需要在点击的时候判断是否正好等于图片的时间,在timer里已经判断了。
            //if (item.Value == Math.Floor(slider1.Value))
            //{
            //    image1.Source = new BitmapImage(new Uri(imagePath + item.Key, UriKind.RelativeOrAbsolute));
            //}
            //只需要判断范围
            for (int i = 1; i < imageTimes.Count; i++)
            {
                //i要从1开始,不然i-1会超出索引
                if ((Math.Floor(slider1.Value) < imageTimes[i]) && (Math.Floor(slider1.Value) > imageTimes[i - 1]))
                {
                    image1.Source = new BitmapImage(new Uri(imagePath + imageAndTime[imageTimes[i - 1]], UriKind.RelativeOrAbsolute));

                    //foreach (var item in imageAndTime)
                    //{
                    //    if (item.Value == imageTimes[i - 1])
                    //    {
                    //        image1.Source = new BitmapImage(new Uri(imagePath + item.Key, UriKind.RelativeOrAbsolute));
                    //    }
                    //}
                }
                else if (Math.Floor(slider1.Value) < imageTimes[0])
                {
                    image1.Source = new BitmapImage(new Uri(imagePath + imageAndTime.Values.First(), UriKind.RelativeOrAbsolute));
                }
            }

        }

        /// <summary>
        /// 播放和暂停按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlayOrPause_Click(object sender, RoutedEventArgs e)
        {
            //要将loadedBehavior和unloadedBehavior设为manual手动
            //如果设为开始就自动播放,播放和暂停按钮就不管用了。已解决!
            if (btnPlayOrPause.Content.ToString() == "播放")
            {
                myMusic.Play();
                btnPlayOrPause.Content = "暂停";
            }
            else
            {
                myMusic.Pause();
                btnPlayOrPause.Content = "播放";

            }

        }

        /// <summary>
        /// 音量条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //貌似音量最高也就10了
            myMusic.Volume = slider2.Value;
        }

    }
}

XAML:

<Window x:Class="_10视频转换.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid Height="285" Name="grid1" Width="503">
        <!--<Image Height="86" HorizontalAlignment="Left" Margin="68,168,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="377" Source="bin\debug\1.jpg"/>-->
        <Image Height="107" HorizontalAlignment="Left" Margin="12,10,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="474" />
        <MediaElement Height="48" HorizontalAlignment="Left" Margin="40,123,0,0" Name="myMusic" VerticalAlignment="Top" Width="271" MediaOpened="myMusic_MediaOpened" UnloadedBehavior="Manual" DataContext="{Binding}" LoadedBehavior="Manual" Source="F:\MyWork\MyWork\trunk\VideoToAudio\VideoToAudio\bin\Debug\LoveStory.mp3" />
        <Button Content="暂停" Height="31" HorizontalAlignment="Left" Margin="40,231,0,0" Name="btnPlayOrPause" VerticalAlignment="Top" Width="49" Click="btnPlayOrPause_Click" />
        <Slider Height="25" HorizontalAlignment="Left" Margin="40,186,0,0" Name="slider1" VerticalAlignment="Top" Width="365" ValueChanged="slider1_ValueChanged" PreviewMouseLeftButtonDown="slider1_PreviewMouseLeftButtonDown" />
        <Label Height="24" HorizontalAlignment="Left" Margin="421,188,0,0" Name="lbTime" VerticalAlignment="Top" Width="65" Content="00:00" />
        <Slider Height="23" HorizontalAlignment="Left" Margin="340,238,0,0" Name="slider2" VerticalAlignment="Top" Width="100" ValueChanged="slider2_ValueChanged" Value="5" Maximum="20" />
    </Grid>
</Window>
时间: 2024-10-06 14:18:21

临时2017-6-19 00:02:03的相关文章

Hexagon PPM COADE TANK 2017 SP1 v9.00.01 1CD

Autodesk.EAGLE.Premium.v9.30.Win64 1CD CeAS-srl.Retaining.Wall.2017.v1.0.0 1CD DATAKIT 2019.1 Import-Export Plugins for SolidWorks 2010-2019 Win64 1CD DATAKIT CrossManager 2019.1 Build 2019-01-14 Win64 1CD Leica Infinity v3.0.1 Win64 1CD OkMap.Deskto

shell 如何生成一个序列 01 02 03 04 05

seq 命令介绍 用途: seq - print a sequence of numbers 语法: seq [OPTION]... LAST seq [OPTION]... FIRST LAST seq [OPTION]... FIRST INCREMENT LAST 常用选项 -s, --separator=STRING use STRING to separate numbers (default: \n) -w, --equal-width equalize width by paddi

【第三组】冲刺会议 2017.7.19

冲刺会议 日期:2017.7.19 开发小组:Geomestry 冲刺经理:程立智 成员: 程立智 李明伦 郑昊 蔡镇泽 温志成 汪涵 成员:程立智 完成工作:完善全局设置 所遇问题: 夜间模式设置双击有bug 下一步工作:重构代码 成员:李明伦 完成工作:收集关卡信息素材 所遇问题:还没有将素材实例化 下一步工作:将素材实例化 成员:郑昊 完成工作:找logo素材 所遇问题:无 下一步工作:继续实现UI大小自适应 成员:蔡镇泽 完成工作:完成了绘制直线自动机 所遇问题: 自动机实现有难度,编写

2017.12.19 2周2次课

2017.12.19二周第二次课 2.10 环境变量PATH echo用来输出PATH的值.PATH前面的$是变量的前缀符号 1.你的命令在上述几个目录里面,在输入命令时就不用输入绝对路径,直接使用.因为在输入命令时,系统会在上述的几个目录里去寻找这个命令.当然也可以输入绝对路径 Copy命令[ls]到目录tmp下,并改名[ls2],二者实现的功能一样 直接输入命令[ls2],确不行.因为命令[ls2]不在"/usr/local/sbin:/usr/local/bin:/usr/sbin:/us

学习进度2020.02.03

学习进度2020.02.03 https://www.runoob.com/w3cnote/android-tutorial-linearlayout.html https://www.runoob.com/w3cnote/android-tutorial-relativelayout.html 今天学习下Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLa

python3 2017.3.19

今天弄了一个晚上没弄出来一个小东西,只弄出来了写追加,而且还是笨方法,起码死不掉那种. 1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms 10 timeout client 50000ms 11 timeout server 50000ms 12 option

新手程序员 工作日志 2017.7.25.18:02 关于改bug网页的使用

责人这里看哪个是自己名下的bug  点进去 点bug对应的ID号进入 综合部-赵鑫 2017/7/25 17:40:47 改完bug修改下bug的状态  附加意见那里可以增加自己的意见 可写也可不写  完成后 点击储存变更

[铁一中OI模拟赛]2017.8.19 Day1

T1 小Z的情书 题目链接 思考: 题目主要难度在于旋转后的位置如何,在手写了样例之后不难发现规律. #include <cstdio> #include <cstring> #define up(a,b,c) for(register int c=a;c<=b;++c) #define down(a,b,c) for(register int c=a;c>=b;--c) const int Maxn=1005; int n; bool Map[Maxn][Maxn],

PAT 甲级 A1006 (2019/02/03)

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 struct TNode{ 6 char id[20]; 7 int hh, mm, ss; 8 }temp, in, out; 9 bool Judge(TNode temp,TNode in){ 10 if(temp.hh != in.hh) return temp.hh > in.hh; 1