Click here to Skip to main content
13,350,708 members (72,602 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 23 Aug 2008

TT Audio Player

, 23 Aug 2008
Rate this:
Please Sign up or sign in to vote.
An audio player made by Tower Turtle productions.


This is a simple audio player that has a loop option, volume control, and a seek bar. It took forever to make it. It uses DirectX.AudioVideoPlayback, so you need DirectX9 for this software to work.


I got most of the help from other AudioVideoPlayback articles on CodeProject.

Using the code

Mostly, when you want to open a song, this code runs, opening an OpenFileDialog and setting the location of the sound file to the file opened by the 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()

    // 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)
        // 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);

Next, you would need the Play button to start the playback. I created a timer so that an event happens every second during playback, during which event the scroll bar value will go up by 1.

public void button2_Click(object sender, EventArgs e)
    if (music != null)
        // 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

        // Enables the stop button
        button4.Enabled = true;
        // 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
    if (hScrollBar1.Value == hScrollBar1.Maximum)
        // Stop the timer if song is over
        // 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

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

            // Uses the same functions of the play button
            button2.Enabled = false;
            button3.Enabled = true;
            button4.Enabled = true;

You will need this code if you want your horizontal scrollbar to fast-forward and scan through the song.

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 is more of the unimportant ones, but this one will change the volume according to the value of a trackbar:

// 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;

Point of interest

One of the most challenging things was setting the scrollbar to change and setting a maximum for the size of the song. I have tried a time counter and a playlist, but they didn't turn out too good. They will probably be good in later versions, this is just beta1.


If you want to find future updates, please go to this link.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Shadow_programmer9-May-12 7:12
memberShadow_programmer9-May-12 7:12 
GeneralMy vote of 1 Pin
occcy22-Dec-09 21:38
memberocccy22-Dec-09 21:38 
GeneralMy vote of 1 Pin
Michael90002-Jan-09 16:24
memberMichael90002-Jan-09 16:24 
GeneralHmm Pin
Jefis24-Aug-08 16:30
memberJefis24-Aug-08 16:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 23 Aug 2008
Article Copyright 2008 by TowerTurtle
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid