65.9K
CodeProject 正在变化。 阅读更多。
Home

TT 音频播放器

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.57/5 (5投票s)

2008年8月23日

CPOL

1分钟阅读

viewsIcon

49906

downloadIcon

2477

由 Tower Turtle Productions 制作的音频播放器。

引言

这是一个简单的音频播放器,具有循环选项、音量控制和进度条。制作它花费了很长时间。它使用 DirectX.AudioVideoPlayback,因此您需要 DirectX9 才能使此软件正常工作。

背景

我从 CodeProject 上其他 AudioVideoPlayback 文章中获得了大部分帮助。

使用代码

通常,当您想要打开一首歌时,这段代码会运行,打开一个 OpenFileDialog 并将声音文件的位置设置为 OpenFileDialog 打开的文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;

namespace TT_Audio_Player
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    // Create a string to set the location of the sound file
    string location;

    // Create an OpenFileDialog to find the sound file
    OpenFileDialog songLoad = new OpenFileDialog();

    // Create a Directx AudioVideoPlayback to play the sound
    Microsoft.DirectX.AudioVideoPlayback.Audio music;

    public void button1_Click(object sender, EventArgs e)
    {
        // Set the OpenFileDialog's filter
        songLoad.Filter = "MP3 (*.mp3)|*.mp3|" +
                          "WMA (*.wma)|*.wma|" +
                          "WAV (*.wav)|*.wav";

    if (songLoad.ShowDialog() != DialogResult.OK)
    {
    }
    else
    {
        // Change the string to the path of the sound file
        location = songLoad.FileName;
        label1.Text = location;
        // Refresh the Directx.AudioVideoPlayback
        // so that the sound file path equals the string
        music = new Audio(location);
        // Tell the scroll bar maximunm to equal
        // the sound file's duration in seconds
        hScrollBar1.Maximum = Convert.ToInt32(music.Duration);
    }
}

接下来,您需要“播放”按钮来开始播放。我创建了一个计时器,以便在播放期间每秒发生一次事件,在此事件期间,滚动条的值将增加 1。

public void button2_Click(object sender, EventArgs e)
{
    if (music != null)
    {
        music.Play();
        // Disables the open button so that you
        // can't change the song while it's playing
        button1.Enabled = false;

        // Disables the play button so that you
        // can't restart the song on accident
        button2.Enabled = false;

        // Enables the pause button 
        button3.Enabled = true;

        // Starts the timer
        timer1.Start();

        // Enables the stop button
        button4.Enabled = true;
    }
    else
    {
        // If nothing is loaded in the sound path, then it will show a message box.
        MessageBox.Show("No Song Loaded", "Error");
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    if (hScrollBar1.Value < hScrollBar1.Maximum)
    {
        // If the scrollbar hasn't reached it's maximum yet, it's value will go up
        hScrollBar1.Value++;
    }
    if (hScrollBar1.Value == hScrollBar1.Maximum)
    {
        // Stop the timer if song is over
        timer1.Stop();
        // Resets the scroll bar
        hScrollBar1.Value = 0;
        // Disables the Pause button
        button3.Enabled = false;
        // Disables the Stop Button
        button4.Enabled = false;
        // Enables the play button
        button2.Enabled = true;
        // Stops the music
        music.Stop();

        if (checkBox1.Checked == true)
        {
            // If loop is checked, play song again
            music.Play();

            // Uses the same functions of the play button
            button2.Enabled = false;
            button3.Enabled = true;
            timer1.Start();
            button4.Enabled = true;
        }
    }
}

如果您希望您的水平滚动条快进和扫描歌曲,您将需要这段代码。

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    // When you scroll the scrollbar, this will set
    // the current position of the sound to the value 
    // of the scrollbar.
    music.CurrentPosition = hScrollBar1.Value;
}

这更多的是一些不重要的部分,但这段代码会根据滑块的值来改变音量。

// This will happen when the trackbar scrolls
private void trackBar1_Scroll(object sender, EventArgs e)
{
    // Sets the volume of the sound to the value of the trackbar
    music.Volume = trackBar1.Value;
}

关注点

最具挑战性的事情之一是将滚动条设置为改变并为歌曲的大小设置最大值。我尝试过时间计数器和播放列表,但它们效果不佳。它们可能在后续版本中会更好,这只是 beta1 版本。

历史

如果您想查找未来的更新,请访问 此链接

© . All rights reserved.