Click here to Skip to main content
Click here to Skip to main content

TT Audio Player

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

Introduction

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.

Background

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()
    {
        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);
    }
}

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)
    {
        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;
        }
    }
}

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.

History

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

License

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

Share

About the Author

TowerTurtle

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberShadow_programmer9-May-12 7:12 
GeneralMy vote of 1 Pinmemberocccy22-Dec-09 21:38 
GeneralMy vote of 1 PinmemberMichael90002-Jan-09 16:24 
GeneralHmm PinmemberJefis24-Aug-08 16:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 23 Aug 2008
Article Copyright 2008 by TowerTurtle
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid