Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Audio Book Player

, 11 Jun 2009
Audio player designed specifically for listening to audio books
abPlayer.zip
Skins
Color
battery.jpg
cancel.jpg
clock.jpg
dark.jpg
exit.jpg
library.jpg
light.jpg
media.jpg
menu.jpg
next.jpg
ok.jpg
play.jpg
prev.jpg
props.jpg
remove.jpg
sleep.jpg
stop.jpg
switch.jpg
vol.jpg
Grayscale
battery.jpg
cancel.jpg
clock.bmp
dark.jpg
exit.jpg
library.jpg
light.jpg
media.jpg
menu.jpg
next.jpg
ok.jpg
play.jpg
prev.jpg
Props.jpg
remove.bmp
sleep.jpg
stop.jpg
switch.jpg
vol.jpg
abPlayer.suo
abPlayer
abPlayer.csproj.user
abPlayer.csproj.vspscc
bin
Release
abPlayer.pdb
abPlayer_exe
OcxControls.dll
OpenNETCF.Windows.Forms.AxHost.dll
Resources
System.Data.SqlServerCe.dll
WMPLib.dll
Properties
DLLs
ComLib.dll
ComLib.PocketPC.asmmeta.dll
OcxControls.dll
OcxControls.PocketPC.asmmeta.dll
OpenNETCF.Windows.Forms.AxHost.dll
OpenNETCF.Windows.Forms.AxHost.PocketPC.asmmeta.dll
WMPLib.dll
Graphics
book.bmp
bookshelf.jpg
check.png
dirclosed.bmp
diropen.bmp
uncheck.png
up.jpg
Submission_Form.zip
abPlayer_files
image001.jpg
image002.jpg
image003.jpg
image004.jpg
themedata.thmx
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;

// cBookStore provides a persistant storage for the player attributes.
// it creates a directory by the name "__abPlayer__" on the largest
// storage card. Under the root directory there are files(F) some with streamed 
// data(S) (some are indicative simply by their existance) and directories (D)
//
// ACTIVEBOOK (F+S)
// SKIN (F+S)
// BOOKS (D)
//      book-name (D)
//          4 book-attributes (F+S)
//          FILES (F+S)
// BOOKSHELVES (D)
//      book-name (F)
//      bookshelf-name (D)
//          book-name (F)

public class cFileSystemPersistantcy : cBasePersistancy
{

    // constants for directory and file names used
    private String ROOT_PATH = "";
    private const String DEFAULT_SKIN = "None";
    private const String BOOKS_PATH = "\\BOOKS";
    private const String BOOKSHELVES_PATH = "\\BOOKSHELVES";
    private const String FILES_PATH = "\\FILES";
    private const String ACTIVE_BOOK_PATH = "\\ACTIVEBOOK";
    private const String SKIN_PATH = "\\SKIN";
    private String[] BOOK_ATTRIBUTE_PATH = {"\\BOOKMARKENTRY", "\\BOOKMARKLOCATION",
                                            "\\VOLUME", "\\SHUFFLE"};
    private int[] BOOK_ATTRIBUTE_DEFAULTS = {0, 0, 40, 0};
    private String pActiveBookName = "";

    public cFileSystemPersistantcy(String assembly)
    {
        ROOT_PATH = GetLargestStorageCard();
        if (ROOT_PATH.Length == 0)
           throw new Exception("No Storage card Found");
        ROOT_PATH += "\\__" + assembly + "__";
        // if root dir not present - create it
        if (!Directory.Exists(ROOT_PATH))
        {
            if (File.Exists(ROOT_PATH))
                File.Delete(ROOT_PATH);
            Directory.CreateDirectory(ROOT_PATH);
            // make directory hidden & system
            DirectoryInfo di = new DirectoryInfo(ROOT_PATH);
            di.Attributes |= (FileAttributes.Hidden | FileAttributes.System);
        }
        // check existance of 2 sub dirs. if not found create them
        if (!Directory.Exists(ROOT_PATH + BOOKS_PATH))
            Directory.CreateDirectory(ROOT_PATH + BOOKS_PATH);
        if (!Directory.Exists(ROOT_PATH + BOOKSHELVES_PATH))
            Directory.CreateDirectory(ROOT_PATH + BOOKSHELVES_PATH);
        // get active book
        FileStream fs = null;
        StreamReader sr = null;
        try
        {
            fs = new FileStream(ROOT_PATH + ACTIVE_BOOK_PATH,
                FileMode.Open, FileAccess.Read);
            sr = new StreamReader(fs);
            pActiveBookName = sr.ReadLine();
        }
        catch { }
        finally
        {
            if (sr != null) sr.Close();
            if (fs != null) fs.Close();
        }
    }
    // get/set active book
    public override String ActiveBookName
    {
        get
        {
            return pActiveBookName;
        }
        set
        {
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                fs = new FileStream(ROOT_PATH + ACTIVE_BOOK_PATH,
                    FileMode.Create, FileAccess.Write);
                sw = new StreamWriter(fs);
                sw.WriteLine(value);
                pActiveBookName = value;
            }
            catch { }
            finally
            {
                if(sw != null) sw.Close();
                if(fs != null) fs.Close();
            }
        }
    }
    // get/set skin
    public override String Skin
    {
        get
        {
            String sk = DEFAULT_SKIN;
            FileStream fs = null;
            StreamReader sr = null;
            try
            {
                fs = new FileStream(ROOT_PATH + SKIN_PATH,
                    FileMode.Open, FileAccess.Read);
                sr = new StreamReader(fs);
                sk = sr.ReadLine();
            }
            catch { }
            finally
            {
                if (sr != null) sr.Close();
                if (fs != null) fs.Close();
            }
            return sk;
        }
        set
        {
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                fs = new FileStream(ROOT_PATH + SKIN_PATH,
                    FileMode.Create, FileAccess.Write);
                sw = new StreamWriter(fs);
                sw.WriteLine(value);
            }
            catch { }
            finally
            {
                if (sw != null) sw.Close();
                if (fs != null) fs.Close();
            }
        }
    }
    // get book poperty
    public override int GetBookProperty(eBookProperty prop, String book)
    {
        if ((book == "") && (ActiveBookName == "")) return 0;
        int value = 0;
        FileStream fs = null;
        StreamReader sr = null;
        try
        {
            String path = ROOT_PATH + BOOKS_PATH + "\\" +
                ((book == "") ? ActiveBookName : book) +
                BOOK_ATTRIBUTE_PATH[(int)prop];
            fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            sr = new StreamReader(fs);
            value = Convert.ToInt32(sr.ReadLine());
        }
        catch { }
        finally
        {
            if(sr != null) sr.Close();
            if(fs != null) fs.Close();
        }
        return value;
    }
    // set a book property
    public override bool SetBookProperty(eBookProperty prop, int value, String book)
    {
        if ((book == "") && (ActiveBookName == "")) return false;
        FileStream fs = null;
        StreamWriter sw = null;
        try
        {
            String path = ROOT_PATH + BOOKS_PATH + "\\" +
                ((book == "") ? ActiveBookName : book) +
                BOOK_ATTRIBUTE_PATH[(int)prop];
            fs = new FileStream(path, FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine(value.ToString());
            return true;
        }
        catch { return false; }
        finally
        {
            if(sw != null) sw.Close();
            if(fs != null) fs.Close();
        }
    }
    // get all bookshelves
    public override String[] Books
    {
        get
        {
            DirectoryInfo[] dirs =
                new DirectoryInfo(ROOT_PATH + BOOKS_PATH).GetDirectories();
            String[] bss = new String[dirs.GetLength(0)];
            for (int i = 0; i < bss.GetLength(0); i++)
                bss[i] = dirs[i].Name;
            Array.Sort(bss);
            return bss;
        }
    }
    // get all bookshelves
    public override String[] Bookshelves
    {
        get
        {
            DirectoryInfo[] dirs = 
                new DirectoryInfo(ROOT_PATH + BOOKSHELVES_PATH).GetDirectories();
            String[] bss = new String[dirs.GetLength(0)];
            for (int i = 0; i < bss.GetLength(0); i++)
                bss[i] = dirs[i].Name;
            Array.Sort(bss);
            return bss;
        }
    }
    // get all books on (in) a bookshelf
    public override String[] GetBookshelfBooks(String bookshelf)
    {
        String[] files =
            Directory.GetFiles(ROOT_PATH + BOOKSHELVES_PATH + "\\" + bookshelf);
        String[] books = new String[files.GetLength(0)];
        for (int i = 0; i < books.GetLength(0); i++)
            books[i] = new FileInfo(files[i]).Name;
        Array.Sort(books);
        return books;
    }
    // get a book's bookshelf
    public override String GetBooksBookshelf(String book)
    {
        if ((book == "") && (ActiveBookName == "")) return "";
        if (File.Exists(ROOT_PATH + BOOKSHELVES_PATH + "\\" + 
            ((book == "")? ActiveBookName: book)))
            return "";
        DirectoryInfo[] dirs = new DirectoryInfo(ROOT_PATH + BOOKSHELVES_PATH).GetDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            if (File.Exists(dir.FullName + "\\" +
                ((book == "") ? ActiveBookName : book)))
                return dir.Name;
        }
        return "";
    }
    // create new bookshelf
    public override bool NewBookshelf(String bookshelf)
    {
        try
        {
            Directory.CreateDirectory(ROOT_PATH + BOOKSHELVES_PATH + "\\" + bookshelf);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        return true;
    }
    // delete a bookshelf
    public override bool DeleteBookshelf(String bookshelf)
    {
        try
        {
            // move all books in the bookshelf to the root node
            String[] files = Directory.GetFiles(ROOT_PATH + BOOKSHELVES_PATH + "\\" + 
                bookshelf);
            foreach (String file in files)
                Directory.Move(file, ROOT_PATH + BOOKSHELVES_PATH + "\\" +
                    new FileInfo(file).Name);
            Directory.Delete(ROOT_PATH + BOOKSHELVES_PATH + "\\" + bookshelf, true);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        return true;
    }
    // set/change a book's bookshelf
    public override bool SetBooksBookshelf(String book, String newBookshelf)
    {
        FileStream fs = null;
        try
        {
            // remove from old place
            String oldBookshelf = GetBooksBookshelf(book);
            if (oldBookshelf.Length == 0)
                File.Delete(ROOT_PATH + BOOKSHELVES_PATH + "\\" + book);
            else
                File.Delete(ROOT_PATH + BOOKSHELVES_PATH + "\\" +
                    oldBookshelf + "\\" + book);
            // put in new place
            String path = (newBookshelf.Length == 0) ?
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + book :
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + newBookshelf + "\\" + book;
            fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        finally
        {
            if(fs != null) fs.Close();
        }
        return true;
    }
    // create new book
    public override bool NewBook(String book, String bookshelf)
    {
        FileStream fs1 = null;
        FileStream fs3 = null;
        StreamWriter sw3 = null;
        try
        {
            // write book in appropriate bookshelf
            String path = (bookshelf.Length == 0) ?
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + book :
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + bookshelf + "\\" + book;
            fs1 = new FileStream(path, FileMode.CreateNew, FileAccess.Write);
            // create the book 
            Directory.CreateDirectory(ROOT_PATH + BOOKS_PATH + "\\" + book);
            // set default values
            for (int i = 0; i < BOOK_ATTRIBUTE_PATH.GetLength(0); i++)
            {
                FileStream fs2 = null;
                StreamWriter sw2 = null;
                try
                {
                    fs2 = new FileStream(ROOT_PATH + BOOKS_PATH + "\\" + book + BOOK_ATTRIBUTE_PATH[i],
                        FileMode.CreateNew, FileAccess.Write);
                    sw2 = new StreamWriter(fs2);
                    sw2.WriteLine((BOOK_ATTRIBUTE_DEFAULTS[i]).ToString());
                }
                catch { }
                finally
                {
                    if (sw2 != null) sw2.Close();
                    if (fs2 != null) fs2.Close();
                }
            }
            // create files place holder
            fs3 = new FileStream(ROOT_PATH + BOOKS_PATH + "\\" + book + FILES_PATH,
                FileMode.CreateNew, FileAccess.Write);
            sw3 = new StreamWriter(fs3);
            sw3.WriteLine("0");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        finally
        {
            if (fs1 != null) fs1.Close();
            if (sw3 != null) sw3.Close();
            if (fs3 != null) fs3.Close();
        }
        return true;
    }
    // get book file count 
    public override int GetBookFileCount(String book)
    {
        if ((book == "") && (ActiveBookName == "")) return 0;
        FileStream fs = null;
        StreamReader sr = null;
        int count = 0;
        try
        {
            String path = (book == "") ?
                ROOT_PATH + BOOKS_PATH + "\\" + ActiveBookName + FILES_PATH :
                ROOT_PATH + BOOKS_PATH + "\\" + book + FILES_PATH;
            fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            sr = new StreamReader(fs);
            count = Convert.ToInt32(sr.ReadLine());
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            if(sr != null) sr.Close();
            if(fs != null) fs.Close();
        }
        return count;
    }
    // get book files 
    public override String[] GetBookFiles(String book)
    {
        if ((book == "") && (ActiveBookName == "")) return new String[0];
        String[] files = new String[0];
        FileStream fs = null;
        StreamReader sr = null;
        try
        {
            String path = (book == "") ?
                ROOT_PATH + BOOKS_PATH + "\\" + ActiveBookName + FILES_PATH :
                ROOT_PATH + BOOKS_PATH + "\\" + book + FILES_PATH;
            fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            sr = new StreamReader(fs);
            int count = Convert.ToInt32(sr.ReadLine());
            files = new string[count];
            for (int i = 0; i < count; i++)
                files[i] = sr.ReadLine();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            if(sr != null) sr.Close();
            if(fs != null) fs.Close();
        }
        return files;
    }
    // set the book files - 
    // we always add the files to existing ones.
    public override bool SetBookFiles(String[] newFiles, String book)
    {
        FileStream fs = null;
        StreamWriter sw = null;
        try
        {
            if ((book == "") && (ActiveBookName == "")) return false;
            String[] oldFiles = GetBookFiles(book);
            // write old & new files.
            String path = (book == "") ?
                ROOT_PATH + BOOKS_PATH + "\\" + ActiveBookName + FILES_PATH :
                ROOT_PATH + BOOKS_PATH + "\\" + book + FILES_PATH;
            fs = new FileStream(path, FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine((oldFiles.GetLength(0) + newFiles.GetLength(0)).ToString());
            foreach (String file in oldFiles)
                sw.WriteLine(file);
            foreach (String file in newFiles)
                sw.WriteLine(file);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        finally
        {
            if(sw != null) sw.Close();
            if(fs != null) fs.Close();
        }
        return true;
    }
    // delete a file entry
    public override bool DeleteBookFile(int ix, String book)
    {
        if ((book == "") && (ActiveBookName == "")) return false; ;
        try
        {
            ArrayList files = new ArrayList();
            files.AddRange(GetBookFiles(book));
            if ((ix < 0) || (ix > (files.Count - 1))) return false;
            files.RemoveAt(ix);
            DeleteBookFiles(book);
            SetBookFiles((String[])files.ToArray(typeof(String)), book);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        return true;
    }
    // delete all book file entries
    public override bool DeleteBookFiles(String book)
    {
        if ((book == "") && (ActiveBookName == "")) return false;
        FileStream fs = null;
        StreamWriter sw = null;
        try
        {
            String path = (book == "") ?
                ROOT_PATH + BOOKS_PATH + "\\" + ActiveBookName + FILES_PATH :
                ROOT_PATH + BOOKS_PATH + "\\" + book + FILES_PATH;
            fs = new FileStream(path, FileMode.Create, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine("0");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        finally
        {
            if(sw != null) sw.Close();
            if(fs != null) fs.Close();
        }
        return true;
    }
    // rename a bookshelf
    public override bool RenameBookShelf(String newName, String bookshelf)
    {
        try
        {
            Directory.Move(ROOT_PATH + BOOKSHELVES_PATH + "\\" + bookshelf,
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + newName);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        return true;
    }
    // rename a book
    public override bool RenameBook(String newName, String book)
    {
        try
        {
            String oldBookShelf = GetBooksBookshelf(book);
            Directory.Move(ROOT_PATH + BOOKS_PATH + "\\" + book,
                ROOT_PATH + BOOKS_PATH + "\\" + newName);
            String path = (oldBookShelf.Length == 0) ?
                ROOT_PATH + BOOKSHELVES_PATH :
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + oldBookShelf;
            Directory.Move(path + "\\" + book, path + "\\" + newName);
            if (pActiveBookName == book)
                ActiveBookName = newName;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        return true;
    }
    // delete a book.
    public override bool DeleteBook(String book)
    {
        // if active book is deleted - no active book anymore
        if (ActiveBookName == book)
            ActiveBookName = "";
        try
        {
            String bookShelf = GetBooksBookshelf(book);
            Directory.Delete(ROOT_PATH + BOOKS_PATH + "\\" + book, true);
            String path = (bookShelf.Length == 0)?
                ROOT_PATH + BOOKSHELVES_PATH :
                ROOT_PATH + BOOKSHELVES_PATH + "\\" + bookShelf;
            File.Delete(path + "\\" + book);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return false;
        }
        return true;
    }
}

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)

Share

About the Author

brochpirate

Israel Israel
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 11 Jun 2009
Article Copyright 2008 by brochpirate
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid