Click here to Skip to main content
15,885,278 members
Articles / Mobile Apps / Windows Mobile

Audio Book Player

Rate me:
Please Sign up or sign in to vote.
4.86/5 (36 votes)
11 Jun 2009CPOL6 min read 197.5K   3.5K   84  
Audio player designed specifically for listening to audio books
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace abPlayer
{
    public partial class frmProps : Form
    {
        public bool Dirty = false;
        private frmLibrary Library = null;
        private cPlayer Player = null;
        private cASUS639DeviceSpecifics DeviceSpecifics = null;
        public frmMediaProps MediaProps = null;

        public frmProps(frmLibrary lib, cPlayer ply, cASUS639DeviceSpecifics dvc)
        {
            InitializeComponent();

            Library = lib;
            Player = ply;
            DeviceSpecifics = dvc;
            MediaProps = new frmMediaProps();
        }

        public new DialogResult ShowDialog()
        {
            try
            {
                // set current timeline value
                hscTime.Maximum = Player.Duration + hscTime.LargeChange - 1;
                hscTime.Value = Player.Location;
                // set playlist index
                hscEntry.Maximum = Player.PlaylistCount + hscEntry.LargeChange - 1;
                hscEntry.Value = Player.PlayingIndex + 1;
                // set shuffle flag
                chkShuffle.Checked = Player.Shuffle;
                // reflect values in associated text boxes
                edtTime.Text = Player.TimeToString(hscTime.Value);
                edtEntry.Text = hscEntry.Value.ToString();
                lblName.Text = new FileInfo(Player.PlaylistFile(hscEntry.Value - 1)).Name;
                hscVol.Value = DeviceSpecifics.Volume;
                edtVol.Text = hscVol.Value.ToString();
                if (Player.TotalDuration == 0)
                {
                    edtTotals.Text = edtRemain.Text = "";
                }
                else
                {
                    edtTotals.Text = Player.TotalDurationSoFarString +
                        " / " + Player.TotalDurationString;
                    edtRemain.Text = "(" + Player.TotalDurationRemainString +")";
                }
                Dirty = false;
                return base.ShowDialog();
            }
            catch
            {
                return DialogResult.Cancel;
            }
        }

        private void picCancel_Click(object sender, EventArgs e)
        {
            Library.FileSelector.FlashControl((Control)sender);
            DialogResult = DialogResult.Cancel;
        }

        private void picOK_Click(object sender, EventArgs e)
        {
            Library.FileSelector.FlashControl((Control)sender);
            DialogResult = DialogResult.OK;
        }
        // timestamp changed - reflect value in text box and 
        // flag so it will be commited and carried out
        private void hscTime_ValueChanged(object sender, EventArgs e)
        {
            edtTime.Text = Player.TimeToString(hscTime.Value);
            Dirty = true;
        }
        // playlist file index changed
        private void hscEntry_ValueChanged(object sender, EventArgs e)
        {
            edtEntry.Text = hscEntry.Value.ToString();
            Dirty = true;
            // if this is not the active media - we don't know the file duration
            // so we prevent the user from modifying it
            if (hscEntry.Value != (Library.BookStore.GetBookProperty(eBookProperty.BOOKMARK_ENTRY, "") + 1))
            {
                int d = Player.FileDuration(hscEntry.Value - 1);
                hscTime.Value = 0;
                if (d == -1)
                    hscTime.Maximum = 0;
                else
                    hscTime.Maximum = d + hscTime.LargeChange - 1;
            }
            else
            {
                hscTime.Maximum = Player.Duration + hscTime.LargeChange - 1;
                hscTime.Value = Player.Location;
            }
            edtTime.Text = Player.TimeToString(hscTime.Value);
            try { lblName.Text = new FileInfo(Player.PlaylistFile(hscEntry.Value - 1)).Name; }
            catch { lblName.Text = ""; }
        }
        // delete a playlist entry, optionally remove also
        // from actual storage
        private void picRemove_Click(object sender, EventArgs e)
        {
            Library.FileSelector.FlashControl((Control)sender);
            if (Player.PlaylistCount < 2) return;
            int oldEntry = hscEntry.Value - 1;
            String file = Player.PlaylistFile(oldEntry);
            DialogResult rslt = MessageBox.Show("Delete File From Storage: " + file,
                "Remove Item", MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3);
            if (rslt == DialogResult.Cancel) return;
            Player.Stop();
            hscEntry.Maximum -= 1;
            hscEntry.Value = Player.RemoveAt(oldEntry) + 1;
            edtEntry.Text = hscEntry.Value.ToString();
            Library.BookStore.DeleteBookFile(oldEntry, "");
            Dirty = true;
            hscTime.Maximum = 0;
            hscTime.Value = 0;
            edtTime.Text = Player.TimeToString(hscTime.Value);
            lblName.Text = new FileInfo(Player.PlaylistFile(hscEntry.Value - 1)).Name;
            if (rslt == DialogResult.Yes)
            {
                try
                {
                    File.Delete(file);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File System Error : " + ex.Message);
                }
            }
        }
        // volume level changed - show value in associated text box, apply to
        // player and commit to persistant storage
        private void hscVol_ValueChanged(object sender, EventArgs e)
        {
            Dirty = true;
            edtVol.Text = hscVol.Value.ToString();
        }

        private void chkShuffle_CheckStateChanged(object sender, EventArgs e)
        {
            Dirty = true;
        }

        private void picMediaProps_Click(object sender, EventArgs e)
        {
            Library.FileSelector.FlashControl((Control)sender);
            MediaProps.ShowDialog(Player.PlaylistFile(hscEntry.Value - 1));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions