Click here to Skip to main content
6,630,586 members and growing! (19,497 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate License: The Code Project Open License (CPOL)

TT Audio Player

By TowerTurtle

An audio player made by Tower Turtle productions.
C# (C# 1.0, C# 2.0, C# 3.0), Windows, .NET, DirectX, Dev
Posted:23 Aug 2008
Views:13,307
Bookmarked:18 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.60 Rating: 2.00 out of 5
1 vote, 50.0%
1

2

3
1 vote, 50.0%
4

5

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)

About the Author

TowerTurtle


Member

Location: United States United States

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberMichael900016:24 2 Jan '09  
GeneralRe: My vote of 1 PinmemberTowerTurtle7:16 28 Jan '09  
GeneralHmm PinmemberJefis16:30 24 Aug '08  
GeneralRe: Hmm Pinmembertowerturtle5:28 25 Aug '08  
GeneralRe: Hmm Pinmembertowerturtle5:35 25 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Aug 2008
Editor: Smitha Vijayan
Copyright 2008 by TowerTurtle
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project