akodeplayer.cpp 4.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/***************************************************************************
    copyright            : (C) 2004 by Allan Sandfeld Jensen
    email                : kde@carewolf.com
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <config.h>

17
#ifdef HAVE_AKODE
18

19 20 21 22
#include <kdebug.h>

#include <qfile.h>

23 24
#include <akode/player.h>
#include <akode/decoder.h>
25 26 27 28 29 30 31 32 33 34

#include "akodeplayer.h"

using namespace aKode;

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

aKodePlayer::aKodePlayer() : Player(),
35 36
                             m_player(0),
                             m_volume(1.0)
37 38 39 40 41 42 43 44 45
{}

aKodePlayer::~aKodePlayer()
{
    delete m_player;
}

void aKodePlayer::play(const FileHandle &file)
{
Laurent Montel's avatar
Laurent Montel committed
46
    kDebug( 65432 ) << k_funcinfo << endl;
47

Allan Sandfeld Jensen's avatar
Allan Sandfeld Jensen committed
48
    if (file.isNull()) { // null FileHandle file means unpause
49
        if (paused())
50
            m_player->resume();
51 52 53 54
        else
            stop();
        return;
    }
55

56
    QString filename = file.absFilePath();
57

Laurent Montel's avatar
Laurent Montel committed
58
    kDebug( 65432 ) << "Opening: " << filename << endl;
59 60 61 62 63 64

    if (m_player)
        m_player->stop();
    else {
        m_player = new aKode::Player();
        m_player->open("auto");
65
        m_player->setVolume(m_volume);
66 67
    }

Allan Sandfeld Jensen's avatar
Allan Sandfeld Jensen committed
68
    if (m_player->load(filename.local8Bit().data()))
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        m_player->play();

}

void aKodePlayer::pause()
{
    if (m_player)
        m_player->pause();
}

void aKodePlayer::stop()
{
    if (m_player) {
        m_player->stop();
        m_player->unload();
    }
}

void aKodePlayer::setVolume(float volume)
{
89 90
    m_volume = volume;

91
    if (m_player)
92
        m_player->setVolume(m_volume);
93 94 95 96
}

float aKodePlayer::volume() const
{
97
    return m_volume;
98 99 100 101 102 103 104 105
}

/////////////////////////////////////////////////////////////////////////////////
// m_player status functions
/////////////////////////////////////////////////////////////////////////////////

bool aKodePlayer::playing() const
{
106
    if (m_player && m_player->decoder() && m_player->state() == aKode::Player::Playing)
107 108 109 110 111 112 113
        return !m_player->decoder()->eof();
    else
        return false;
}

bool aKodePlayer::paused() const
{
114
    return m_player && (m_player->state() == aKode::Player::Paused);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

int aKodePlayer::totalTime() const
{
    if (m_player) {
        Decoder *d = m_player->decoder();
        if (d)
            return d->length() / 1000;
    }
    return -1;
}

int aKodePlayer::currentTime() const
{
    if (m_player) {
        Decoder *d = m_player->decoder();
        if (d)
            return d->position() / 1000;
    }
    return -1;
}

int aKodePlayer::position() const
{
    if (m_player) {
        Decoder *d = m_player->decoder();
        if (d && d->length())
            return (d->position()*1000)/(d->length());
        else
            return -1;
    }
    else
        return -1;
}

/////////////////////////////////////////////////////////////////////////////////
// m_player seek functions
/////////////////////////////////////////////////////////////////////////////////

void aKodePlayer::seek(int seekTime)
{
    // seek time in seconds?
    if (m_player)
        m_player->decoder()->seek(seekTime*1000);
}

void aKodePlayer::seekPosition(int position)
{
    // position unit is 1/1000th
    if (m_player)
        m_player->decoder()->seek((position * m_player->decoder()->length())/1000);
}

#include "akodeplayer.moc"
169 170

#endif
171 172

// vim: set et sw=4 tw=0 sta: