⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playerpage.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <KCeasy.h>
#pragma hdrstop

#include <math.h>
#include "PlayerPage.h"
#include "config.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TPlayerForm *PlayerForm;
//---------------------------------------------------------------------------

__fastcall TPlayerForm::TPlayerForm(TComponent* Owner)
:   TForm(Owner),
    Dragging(false),
    MPlayer(NULL)
{
    TranslateComponent(this);

    // create MPlayer
    MPlayer = new TMPlayer(DisplayPanel);
    MPlayer->Parent = DisplayPanel;

    // register events
    MPlayer->OnKeyPress = MPlayerKeyPress;
    MPlayer->OnStateChange = MPlayerStateChange;
    MPlayer->OnPositionChange = MPlayerPositionChange;
    MPlayer->OnDisplayPopup = MPlayerDisplayPopup;

    // used to intercept WM_HSCROLL of track bar
    OldCtrlPanelProc = CtrlPanel->WindowProc;
    CtrlPanel->WindowProc = CtrlPanelProc;

    // disable all UI elemets
    ClosePlayback();
    VolumeTrackBar->Enabled = false;
    VolumeTrackBar->Position = 0;
    MuteBtn->Enabled = false;
    VisualizationBtn->Enabled = false;
    ZoomMnu->Enabled = false;
}

__fastcall TPlayerForm::~TPlayerForm()
{
    delete MPlayer;
}

void __fastcall TPlayerForm::Release()
{
    // save volume and mute
    if(MPlayer && MPlayer->IsInitialized())
        Config->SetValueInt("player/volume", MPlayer->GetVolume() * 100);
    if(MPlayer && MPlayer->IsInitialized())
        Config->SetValueInt("player/mute", MPlayer->IsMuted());

    TForm::Release();
}
//---------------------------------------------------------------------------

bool __fastcall TPlayerForm::EngineCallback(TCallbackInfo* CbInfo)
{
    switch(CbInfo->Code) {
    case CbcStateChange:
        if (Engine->IsOffline()) {
            MPlayer->Stop();
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TPlayerForm::LoadMPlayer()
{
    if(MPlayer->IsInitialized())
        return true;

    if(!MPlayer->Initialize(Config->GetValueInt("player/vlc_logging"))) {
        // TRANSLATOR: Error shown on player page when VLC cannot be started.
        DisplayPanel->Caption = _("The Media Player (VLC) could not be started.\n"
                                  "This is most likely because you didn't install it with {AppName}.\n"
                                  "To fix this download the full installer and reinstall {AppName}.");
        MPlayer->Visible = false;
        // show error box
        MessageDlg(DisplayPanel->Caption.c_str(),mtError,TMsgDlgButtons() << mbOK,0);
        return false;
    }
    // set saved volume and mute
    MPlayer->SetVolume((double)Config->GetValueInt("player/volume") / 100);
    MPlayer->Mute(Config->GetValueInt("player/mute"));

    // set volume slider to current volume
    VolumeTrackBar->Enabled = true;
    VolumeTrackBar->Position = (double)VolumeTrackBar->Max * MPlayer->GetVolume();
    MuteBtn->Enabled = true;
    MuteBtn->Down = MPlayer->IsMuted();
    VisualizationBtn->Enabled = true;
    VisualizationBtn->Down = Config->GetValueInt("player/visualization");
    ZoomMnu->Enabled = true;

    return true;
}
//---------------------------------------------------------------------------

bool TPlayerForm::PlayFile(const string& File)
{
    if(!LoadMPlayer())
        return false;

    MPlayer->Stop();
    OpenFile = File;

    if(!MPlayer->Play(OpenFile.c_str())) {
        // TRANSLATOR: Message box when file cannot be played in integrated player.
        AnsiString Error = AnsiString::Format(_("Unable to play file \"%s\""),
                           ARRAYOFCONST((OpenFile.c_str())));
        MessageDlg(Error,mtError,TMsgDlgButtons() << mbOK,0);
        ClosePlayback();
        return false;
    }

    return true;
}

bool TPlayerForm::PlayStream(const string& Url)
{
    if(!LoadMPlayer())
        return false;

    MPlayer->Stop();
    OpenFile = Url;

    if(!MPlayer->PlayNetwork(OpenFile.c_str())) {
        // TRANSLATOR: Message box when file cannot be streamed in integrated player.
        AnsiString Error = AnsiString::Format(_("Unable to stream file \"%s\""),
                           ARRAYOFCONST((OpenFile.c_str())));
        MessageDlg(Error,mtError,TMsgDlgButtons() << mbOK,0);
        ClosePlayback();
        return false;
    }

    return true;
}

void TPlayerForm::StopPlayback()
{
    MPlayer->Stop();
}

void TPlayerForm::ClosePlayback()
{
    MPlayer->Stop();

    OpenFile = "";

    Dragging = false;
    MPlayerPositionChange(NULL,0,0);
    
    PlayBtn->Enabled = false;
    PauseBtn->Enabled = false;
    StopBtn->Enabled = false;
    TimeStatic->Caption = "00:00";
    TitleStatic->Caption = "";
    SeekTrackBar->Enabled = false;
    // main form
    MainForm->PlayBtn->Enabled = false;
    MainForm->PauseBtn->Enabled = false;
    MainForm->StopBtn->Enabled = false;
    MainForm->TimeLabel->Caption = "";
    MainForm->TitleLabel->Caption = "";
    MainForm->PlayProgressBar->Enabled = false;
    // display popup
    PlayMnu->Enabled = false;
    PauseMnu->Enabled = false;
    StopMnu->Enabled = false;
}

const string& TPlayerForm::GetOpenFile()
{
    return OpenFile;
}

void TPlayerForm::SeekDrag(double Position)
{
    char buf[64];
    unsigned int LenSec = MPlayer->GetDuration() / 1000;
    double PosSec = Position * LenSec;

    Dragging = true;
    // CtrlPanel
    wsprintf(buf,"%02d:%02d",(unsigned int)PosSec/60,((unsigned int)PosSec)%60);
    TimeStatic->Caption = buf;
    SeekTrackBar->Position = Position * SeekTrackBar->Max;
    // MainForm
    wsprintf(buf,"%02d:%02d / %02d:%02d",(unsigned int)PosSec/60,((unsigned int)PosSec)%60,(unsigned int)LenSec/60,((unsigned int)LenSec)%60);
    MainForm->TimeLabel->Caption = buf;
    MainForm->PlayProgressBar->Position = Position * MainForm->PlayProgressBar->Max;
}

void TPlayerForm::SeekSet(double Position, bool Commit)
{
    if(Commit) {
        // CtrlPanel
        SeekTrackBar->Position = Position * SeekTrackBar->Max;
        SeekTrackBar->SelEnd = SeekTrackBar->Position;
        // MainForm
        MainForm->PlayProgressBar->Position = Position * MainForm->PlayProgressBar->Max;
        MPlayer->SetPosition(Position);
    }

    Dragging = false;
}

bool TPlayerForm::SeekIsDragging()
{
    return Dragging;
}

//---------------------------------------------------------------------------

void __fastcall TPlayerForm::MPlayerStateChange(TObject* Sender,
      TMPlayerState NewState)
{
    switch(NewState) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -