none
C#实现播发器淡入淡出 RRS feed

  • 问题

  • 想要实现淡入淡出功能,有个listview里有歌曲和需要淡入淡出,切入切出的时间。然后实现淡入淡出,还有切入切出,一开始播放就播放歌曲的副歌部分,副歌放完立即停止播放。
    private void timer1_Tick(object sender, EventArgs e)
            {
                string t = DateTime.Now.ToLongTimeString().ToString();
                this.textBox1.Text = t.ToString();
                for (int i = 0; i < listView1.Items.Count; i++)

                    if (this.textBox1.Text == this.listView1.Items.SubItems[1].Text)
                    {

                        axWindowsMediaPlayer1.URL = listView1.Items.SubItems[3].Text;

                        axWindowsMediaPlayer1.Ctlcontrols.play();

                     //  this.timer1.Enabled = false;
                        timer2.Enabled = true; 

                     }
            }   

            }
    //下面想实现淡入淡出,根本不行啊
             private void timer2_Tick_1(object sender, EventArgs e)
            {
                for (int x = 0; x < listView1.Items.Count; x++)
                if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    if (axWindowsMediaPlayer1.settings.volume > 70)

                        axWindowsMediaPlayer1.settings.volume = axWindowsMediaPlayer1.settings.volume - 1;

                    if (axWindowsMediaPlayer1.settings.volume < 0)

                        axWindowsMediaPlayer1.settings.volume = axWindowsMediaPlayer1.settings.volume + 1;



                     timer2.Enabled =false ;

                }
            }
    //下面想实现切入
             private void timer3_Tick(object sender, EventArgs e)
             {
                 for (int y= 0; y< listView1.Items.Count; y++)
                     if (listView1.Items[y].SubItems[4].Text != null)
                     if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
                         {

                             axWindowsMediaPlayer1.Ctlcontrols.currentPosition = Convert.ToDouble(listView1.Items[y].SubItems[4].Text);
                             timer3.Enabled = false;
                             timer4.Enabled = true;
                         }
             }

            private double ToDouble(string p)
            {
                throw new NotImplementedException();
            }
    //下面想实现切出
            private void timer4_Tick(object sender, EventArgs e)
            {
                for (int z = 0; z < listView1.Items.Count; z++)
                     if (listView1.Items[z].SubItems[5].Text != null)
                         if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition ==Convert .ToDouble ( listView1.Items[z].SubItems[5].Text))
                         {    
                             axWindowsMediaPlayer1.Ctlcontrols.stop();
                             timer4.Enabled = false;

                         }
    2014年5月6日 15:19

全部回复

  • 你好:

    请问你开发的是Windows Forms应用程序吗? 你是想实现声音逐渐变小的功能是吧?

    我在项目中添加了对WindowsMediaPlayer这个COM组件作了一个简单的测试,是可以做到这点的,下面是一个简单的测试代码:

    private void btnOpenMP3_Click(object sender, EventArgs e)
            {
                axWindowsMediaPlayer1.URL = @"D:\test.mp3";
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    if (axWindowsMediaPlayer1.settings.volume > 0)
                    {
                        axWindowsMediaPlayer1.settings.volume = axWindowsMediaPlayer1.settings.volume - 1;
                    }
                    else
                    {
                        timer1.Enabled = false;
                    }
                }
            }
    
            private void btnVolumeDown_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
            }

    我觉得应该是你的代码逻辑有问题,你在timer2的tick事件中直接将计时器禁用掉了,即使不禁用掉,当音量在0到70之间的时候,你的代码里面并没有作什么处理。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月7日 6:14
    版主
  • 非常感谢你的细则回答,我还要实现的是播放到一个特定的时间,比如播放了50秒,然后就停止播放,listView1.Items[i].SubItems[5].Text,这里listview里第六列就是指定的时间,但是没有实现停止播放,该如何改代码呢
                        if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition == Convert.ToDouble(listView1.Items[i].SubItems[5].Text))
                        {
                            
                            axWindowsMediaPlayer1.Ctlcontrols.stop();
                        }
    2014年5月7日 14:27
  • 可以实现这个功能,我测试了下是没有问题的,再添加一个timer,在tick事件里面添加这段代码,在视频或者音频开始播放的时候启动timer,如果侦测到播放位置达到指定值那么播放器就会停止播放。我把测试代码再贴一次。

    我怀疑你没有认真调试过,你在Visual Studio里面通过单调试就可以发现问题所在。

    private void btnOpenMP3_Click(object sender, EventArgs e)
            {
                axWindowsMediaPlayer1.URL = @"D:\test.mp3";
                timer2.Enabled = true;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    if (axWindowsMediaPlayer1.settings.volume > 0)
                    {
                        axWindowsMediaPlayer1.settings.volume = axWindowsMediaPlayer1.settings.volume - 1;
                    }
                    else
                    {
                        timer1.Enabled = false;
                    }
                }
            }
    
            private void btnVolumeDown_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
            }
    
    
            private void timer2_Tick(object sender, EventArgs e)
            {
                if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition >= 10)
                {
                    axWindowsMediaPlayer1.Ctlcontrols.stop();
    
                    timer2.Enabled = false;
                }
            }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月8日 2:29
    版主
  •   private void timer1_Tick(object sender, EventArgs e)
            {
                string t = DateTime.Now.ToString("HH:mm:ss");
                label2.Text = t.ToString();
          for (int i = 0; i < listView1.Items.Count; i++)
                   if(label2.Text == this.listView1.Items[i].SubItems[1].Text)
                    {
                        axWindowsMediaPlayer1.URL = listView1.Items[i].SubItems[3].Text;
                        axWindowsMediaPlayer1.Ctlcontrols.play();
                        axWindowsMediaPlayer1.Ctlcontrols.currentPosition  = Convert.ToDouble(listView1.Items[i].SubItems[4].Text);
                  if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition == Convert.ToDouble(listView1.Items[i].SubItems[5].Text))
                    {
    
                        axWindowsMediaPlayer1.Ctlcontrols.stop();
                    }
                     
                     
                   }
            }   
          

    listview里SubItems[3]列是url,SubItems[4]和SubItems[5]里面就是对应的时间,50,60之类的时间。第10行实现的切入,后面那个if语句不能实现切出
    2014年5月8日 12:42
  • 你有没有调试过?如果不能实现切入的话有报错吗?报错信息能贴出来吗?

    后面那个if语句里面的代码有没有执行?代码执行到哪一句出现问题的?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月9日 2:02
    版主
  • 没有报错,就是if语句没有执行,换成下面这个也没有停止

    if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition >= 10) { axWindowsMediaPlayer1.Ctlcontrols.stop();

    2014年5月9日 11:47
  •  private void timer1_Tick(object sender, EventArgs e)
            {
                string t = DateTime.Now.ToString("HH:mm:ss");
                label2.Text = t.ToString();
                int d1;
                d1 = Convert.ToInt16(axWindowsMediaPlayer1.Ctlcontrols.currentPosition);
               for (int i = 0; i < listView1.Items.Count; i++)
                   if(label2.Text == this.listView1.Items[i].SubItems[1].Text)
                    {
                        axWindowsMediaPlayer1.URL = listView1.Items[i].SubItems[3].Text;
                        axWindowsMediaPlayer1.Ctlcontrols.play();
    }
    }
    

    2014年5月23日 5:16
  •     private void button2_Click(object sender, EventArgs e)

            {

               timer1.Enabled = true;

               if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)

               {

                   //暂停播放

                   axWindowsMediaPlayer1.Ctlcontrols.pause();

                              

               }

                     else if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)

                {

                    //开始播放

                    axWindowsMediaPlayer1.Ctlcontrols.play();

                     }

           }

        private void button2_Click(object sender, EventArgs e)
            {
               timer1.Enabled = true;
               if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
               {
                   //暂停播放
                   axWindowsMediaPlayer1.Ctlcontrols.pause();
                              
               }
                     else if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
                {
                    //开始播放
                    axWindowsMediaPlayer1.Ctlcontrols.play();
    
                     }
           }
    

    2014年5月23日 5:18
  •  private void timer2_Tick_1(object sender, EventArgs e)
            {
               
                if(axWindowsMediaPlayer1.playState== WMPLib.WMPPlayState.wmppsPlaying)
                {
                    
                    if(axWindowsMediaPlayer1.settings.volume<40)
                        
                            axWindowsMediaPlayer1.settings.volume +=1;

                        }
    2014年5月23日 5:29
  •  private void timer2_Tick_1(object sender, EventArgs e)
            {
               
                if(axWindowsMediaPlayer1.playState== WMPLib.WMPPlayState.wmppsPlaying)
                {
                    
                    if(axWindowsMediaPlayer1.settings.volume<40)
                        
                            axWindowsMediaPlayer1.settings.volume +=1;
    
                        }

    2014年5月23日 5:29
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    
    namespace Player
    {
        public partial class Form2 : Form
        {
         SqlConnection  con=new SqlConnection ("server=LEE;database=Player;uid=sa;pwd=3968064");
    
            public Form2()
            {
                InitializeComponent();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                con.Open();
                using (SqlCommand command = new SqlCommand("INSERT INTO P_1 " + "VALUES (@Time, @Name,@Path,@Hook,@HookOut,@Fade)", con))
                {
                    // Add the parameters for the InsertCommand.
                    command.Parameters.Add("@Time", SqlDbType.VarChar, 50, "Time").Value = this.textBox1.Text;
                    command.Parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = this.textBox2.Text;
                    command.Parameters.Add("@Path", SqlDbType.VarChar, 100, "Path").Value = this.textBox3.Text;
                    //command.Parameters.Add("@Fide", SqlDbType.VarChar, 50, "Fide").Value = this.textBox6.Text;
                    command.Parameters.Add("@Hook", SqlDbType.VarChar, 50, "Hook").Value = this.textBox4.Text;
                    command.Parameters.Add("@HookOut", SqlDbType.VarChar, 50, "HookOut").Value = this.textBox5.Text;
                    command.Parameters.Add("@Fade", SqlDbType.VarChar, 50, "Fade").Value = this.textBox6.Text;
                    command.ExecuteNonQuery();
                    MessageBox.Show("以保存");
                }
                //f1.show
                con.Close();
                this.Close(); 
    
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                this.AcceptButton = button2;
                this.CancelButton = button3;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.textBox3.Text = this.openFileDialog1.FileName;
                    string file = this.openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.LastIndexOf("."));//去掉了后缀名
                    int index = file.LastIndexOf("\\") + 1;
                    this.textBox2.Text = file.Substring(index);//去掉了路径
                }
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    }
    

    2014年5月23日 5:32
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Collections;
    using System.Data.OleDb;
    using System.IO;
    using System.Diagnostics;
    
    
    
    namespace Player
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button3_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.Show();
            }
                 
            private void button1_Click(object sender, EventArgs e)
            {
                
                //建立数据连接
                SqlConnection con = new SqlConnection("server=LEE;database=Player;uid=sa;pwd=3968064");
                con.Open();
                //打开数据集
                SqlCommand com = new SqlCommand("select * from P_1", con);
                SqlDataReader dr = com.ExecuteReader();
                listView1.View = View.Details;
                //listView1.View = View.Tile;
                 listView1.FullRowSelect = true;
                //清空listview中的数据
                listView1.Items.Clear();           
                //对数据集中的数据记录进行遍历,在遍历中添加记录到列表中
    
                while (dr.Read())
                {
                    ListViewItem lv;
                    lv = new ListViewItem(dr[0].ToString());
                    lv.SubItems.Add(dr[1].ToString());
                    lv.SubItems.Add(dr[2].ToString());
                    //增加字段表示FaldeIn/FadeOut
                    lv.SubItems.Add(dr[3].ToString());
                    //增加字段表示HookIn/HookOut
                    lv.SubItems.Add(dr[4].ToString());
                    lv.SubItems.Add(dr[5].ToString());
                    lv.SubItems.Add(dr[6].ToString());
    
                    //MessageBox.Show(dr[2].ToString());
                    listView1.Items.Add(lv);
                } //关闭数据集、关闭数据连接
    
                dr.Close();
                con.Close();
            }
            private void button2_Click(object sender, EventArgs e)
            {
               timer1.Enabled = true;
               if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
               {
                   //暂停播放
                   axWindowsMediaPlayer1.Ctlcontrols.pause();
                   //按钮图片修改为播放
                  // btnPlay.Image = Image.FromFile("Images\\play.png");
                 
               }
                     else if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPaused)
                {
                    //开始播放
                    axWindowsMediaPlayer1.Ctlcontrols.play();
    
                     }
    
    
               }
            
            private void Form1_Load(object sender, EventArgs e)
            {
                this.AcceptButton = button2;
                this.CancelButton = button3;
               
             
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                string t = DateTime.Now.ToString("HH:mm:ss");
                label2.Text = t.ToString();
                int d1;
                d1 = Convert.ToInt16(axWindowsMediaPlayer1.Ctlcontrols.currentPosition);
                //textBox1.Text = Convert.ToString(d1);
                for (int i = 0; i < listView1.Items.Count; i++)
                   if(label2.Text == this.listView1.Items[i].SubItems[1].Text)
                    {
                        axWindowsMediaPlayer1.URL = listView1.Items[i].SubItems[3].Text;
                        axWindowsMediaPlayer1.Ctlcontrols.play();
                        axWindowsMediaPlayer1.Ctlcontrols.currentPosition  = Convert.ToDouble(listView1.Items[i].SubItems[4].Text);
    
                       if (listView1.Items[i].SubItems[6].Text =="1")
                        {
                            axWindowsMediaPlayer1.settings.volume = 0;
                            timer2.Enabled = true;
                            timer3.Enabled = true;
                        }
                      // string s= "axWindowsMediaPlayer1 .Ctlcontrols .currentPosition ";
                      // int m=2;
                      //string  strleft=s.Substring (0.m);
    
    
                       if ( axWindowsMediaPlayer1 .Ctlcontrols .currentPosition >=60)
                    {
    
                        axWindowsMediaPlayer1.Ctlcontrols.stop();
                    }
                      
                     //  timer1.Enabled = false;
                      //  if(axWindowsMediaPlayer1 .Ctlcontrols .currentPosition =)
                     
                   }
            }   
          
             private void timer2_Tick_1(object sender, EventArgs e)
            {
               
                if(axWindowsMediaPlayer1.playState== WMPLib.WMPPlayState.wmppsPlaying)
                {
                    
                    if(axWindowsMediaPlayer1.settings.volume<40)
                        
                            axWindowsMediaPlayer1.settings.volume +=1;
    
                        }
                  // timer2.Enabled = false;
                }
           
            private double ToDouble(string p)
            {
                throw new NotImplementedException();
            }
    
               
                private void listView1_SelectedIndexChanged(object sender, EventArgs e)
                {
    
                }
    
                private void button5_Click(object sender, EventArgs e)
                {
                    if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
                    {
                        //暂停播放
                        axWindowsMediaPlayer1.Ctlcontrols.stop();
                        //按钮图片修改为播放
                        // btnPlay.Image = Image.FromFile("Images\\play.png");
    
                    }
                }
    
                
    
                private void label2_Click(object sender, EventArgs e)
                {
    
                }
    
                private void timer3_Tick(object sender, EventArgs e)
                {
                    if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition >= 50)
                    {
    
                        axWindowsMediaPlayer1.Ctlcontrols.stop();
                    }
                }
    
              
                    
               
          }
      }
    
            
        
    

    2014年5月23日 5:33
  •  private void button1_Click(object sender, EventArgs e)

            {

               

                //建立数据连接

                SqlConnection con = new SqlConnection("server=LEE;database=Player;uid=sa;pwd=3968064");

                con.Open();

                //打开数据集

                SqlCommand com = new SqlCommand("select * from P_1", con);

                SqlDataReader dr = com.ExecuteReader();

                listView1.View = View.Details;

                //listView1.View = View.Tile;

                 listView1.FullRowSelect = true;

                //清空listview中的数据

                listView1.Items.Clear();          

                //对数据集中的数据记录进行遍历,在遍历中添加记录到列表中

                while (dr.Read())

                {

                    ListViewItem lv;

                    lv = new ListViewItem(dr[0].ToString());

                    lv.SubItems.Add(dr[1].ToString());

                    lv.SubItems.Add(dr[2].ToString());

                    //增加字段表示FaldeIn/FadeOut

                    lv.SubItems.Add(dr[3].ToString());

                    //增加字段表示HookIn/HookOut

                    lv.SubItems.Add(dr[4].ToString());

                    lv.SubItems.Add(dr[5].ToString());

                    lv.SubItems.Add(dr[6].ToString());

                    //MessageBox.Show(dr[2].ToString());

                    listView1.Items.Add(lv);

                } //关闭数据集、关闭数据连接

                dr.Close();

                con.Close();

            }

            }

     private void button1_Click(object sender, EventArgs e)
            {
                
                //建立数据连接
                SqlConnection con = new SqlConnection("server=LEE;database=Player;uid=sa;pwd=3968064");
                con.Open();
                //打开数据集
                SqlCommand com = new SqlCommand("select * from P_1", con);
                SqlDataReader dr = com.ExecuteReader();
                listView1.View = View.Details;
                //listView1.View = View.Tile;
                 listView1.FullRowSelect = true;
                //清空listview中的数据
                listView1.Items.Clear();           
                //对数据集中的数据记录进行遍历,在遍历中添加记录到列表中
    
                while (dr.Read())
                {
                    ListViewItem lv;
                    lv = new ListViewItem(dr[0].ToString());
                    lv.SubItems.Add(dr[1].ToString());
                    lv.SubItems.Add(dr[2].ToString());
                    //增加字段表示FaldeIn/FadeOut
                    lv.SubItems.Add(dr[3].ToString());
                    //增加字段表示HookIn/HookOut
                    lv.SubItems.Add(dr[4].ToString());
                    lv.SubItems.Add(dr[5].ToString());
                    lv.SubItems.Add(dr[6].ToString());
    
                    //MessageBox.Show(dr[2].ToString());
                    listView1.Items.Add(lv);
                } //关闭数据集、关闭数据连接
    
                dr.Close();
                con.Close();
            }
    
            }
    

    2014年5月23日 5:46
  •  private void button2_Click(object sender, EventArgs e)
            {
                con.Open();
                using (SqlCommand command = new SqlCommand("INSERT INTO P_1 " + "VALUES (@Time, @Name,@Path,@Hook,@HookOut,@Fide)", con))
                {
                    // Add the parameters for the InsertCommand.
                    command.Parameters.Add("@Time", SqlDbType.VarChar, 50, "Time").Value = this.textBox1.Text;
                    command.Parameters.Add("@Name", SqlDbType.VarChar, 50, "Name").Value = this.textBox2.Text;
                    command.Parameters.Add("@Path", SqlDbType.VarChar, 100, "Path").Value = this.textBox3.Text;
                    //command.Parameters.Add("@Fide", SqlDbType.VarChar, 50, "Fide").Value = this.textBox6.Text;
                    command.Parameters.Add("@Hook", SqlDbType.VarChar, 50, "Hook").Value = this.textBox4.Text;
                    command.Parameters.Add("@HookOut", SqlDbType.VarChar, 50, "HookOut").Value = this.textBox5.Text;
                    command.Parameters.Add("@Fide", SqlDbType.VarChar, 50, "HookOut").Value = this.textBox6.Text;
                    command.ExecuteNonQuery();
                    MessageBox.Show("OK");
                }
                //f1.show
                con.Close();
                this.Close(); 
      }             
    

    2014年5月23日 5:47
  •  axWindowsMediaPlayer1.Ctlcontrols.currentPosition  = Convert.ToDouble(listView1.Items[i].SubItems[4].Text

    2014年5月23日 9:16
  • 请问上面这些代码是你自己找到的解决办法吗?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年5月26日 7:47
    版主