Skip to content

TMediaPlayerDemo 项目分析

项目信息

文件结构

TMediaPlayerDemo/
├── Extractor_Icon.ico
├── Project1.py
├── Project1.xml
├── Unit1.py
├── Unit1.pydfm
├── Unit1.sct
├── Unit1.sfm
├── 错位时空 - 艾辰.mp3

主程序文件: Project1.py

from glcl import *
from Unit1 import *

def main():
    Application.Initialize()
    Application.Title = 'Project1'
    Application.MainFormOnTaskbar = True
    Application.Icon.LoadFromFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Extractor_Icon.ico"))
    MainForm = Form1(Application)
    MainForm.Show()
    FreeConsole()
    Application.Run()
    MainForm.Free()

if __name__ == '__main__':
    main()

单元文件: Unit1.py

import os
from glcl import *

class Form1(Form):

    def __init__(self, owner):
        self.Button1 = Button(self)
        self.MediaPlayer1 = MediaPlayer(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click

        # 创建 TMediaPlayer 控件用于播放音频
        self.media_player = MediaPlayer(self)  # 创建 TMediaPlayer 实例
        self.media_player.SetBounds(10, 10, 380, 50)  # 设置 TMediaPlayer 的位置和大小
        self.media_player.Parent = self  # 将 TMediaPlayer 设置为窗体的子控件

        # 创建播放按钮
        self.play_button = Button(self)  # 创建 TButton 实例
        self.play_button.Caption = "播放"  # 设置按钮的显示文本
        self.play_button.SetBounds(10, 70, 80, 30)  # 设置按钮的位置和大小
        self.play_button.OnClick = self.play_media  # 连接按钮点击事件到处理函数
        self.play_button.Parent = self  # 将按钮设置为窗体的子控件

        # 创建停止按钮
        self.stop_button = Button(self)  # 创建另一个 TButton 实例
        self.stop_button.Caption = "停止"  # 设置按钮的显示文本
        self.stop_button.SetBounds(100, 70, 80, 30)  # 设置按钮的位置和大小
        self.stop_button.OnClick = self.stop_media  # 连接按钮点击事件到处理函数
        self.stop_button.Parent = self  # 将按钮设置为窗体的子控件

        # 设置媒体文件路径(请根据实际路径修改)
        self.media_player.FileName = "错位时空 - 艾辰.mp3"  # 设置要播放的音频文件路径

    # 定义播放媒体的处理函数
    def play_media(self, Sender):
        self.media_player.Open()  # 打开媒体文件
        self.media_player.Play()  # 开始播放音频

    # 定义停止媒体的处理函数
    def stop_media(self, Sender):
        self.media_player.Stop()  # 停止播放音频

    def Button1Click(self, Sender):
        # 设置媒体文件路径(请根据实际路径修改)
        self.MediaPlayer1.FileName = "错位时空 - 艾辰.mp3"  # 设置要播放的音频文件路径
        self.MediaPlayer1.Open()  # 打开媒体文件
        self.MediaPlayer1.Play()  # 开始播放音频

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 238
  ClientWidth = 382
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object MediaPlayer1: TMediaPlayer
    Left = 24
    Top = 160
    Width = 253
    Height = 30
    DoubleBuffered = True
    ParentDoubleBuffered = False
    TabOrder = 0
  end
  object Button1: TButton
    Left = 288
    Top = 160
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml
  • 错位时空 - 艾辰.mp3

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • MediaPlayer1 (TMediaPlayer)
  • Button1 (TButton)

技术特点

  • 包含3个GUI组件
  • 定义3个事件处理

窗体属性

  • caption: Button1
  • height: 238
  • width: 382

代码分析

导入的模块: - import os - from glcl import *

定义的类: - Form1

定义的方法: - init - play_media - stop_media - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.play_button.OnClick = self.play_media # 连接按钮点击事件到处理函数 - self.stop_button.OnClick = self.stop_media # 连接按钮点击事件到处理函数