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();
}
string location;
OpenFileDialog songLoad = new OpenFileDialog();
Microsoft.DirectX.AudioVideoPlayback.Audio music;
public void button1_Click(object sender, EventArgs e)
{
songLoad.Filter = "MP3 (*.mp3)|*.mp3|" +
"WMA (*.wma)|*.wma|" +
"WAV (*.wav)|*.wav";
if (songLoad.ShowDialog() != DialogResult.OK)
{
}
else
{
location = songLoad.FileName;
label1.Text = location;
music = new Audio(location);
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();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = true;
timer1.Start();
button4.Enabled = true;
}
else
{
MessageBox.Show("No Song Loaded", "Error");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (hScrollBar1.Value < hScrollBar1.Maximum)
{
hScrollBar1.Value++;
}
if (hScrollBar1.Value == hScrollBar1.Maximum)
{
timer1.Stop();
hScrollBar1.Value = 0;
button3.Enabled = false;
button4.Enabled = false;
button2.Enabled = true;
music.Stop();
if (checkBox1.Checked == true)
{
music.Play();
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)
{
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:
private void trackBar1_Scroll(object sender, EventArgs e)
{
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.