在 qb64 中加载音频文件时可能加载条或百分比

Posted

技术标签:

【中文标题】在 qb64 中加载音频文件时可能加载条或百分比【英文标题】:Loading bar or percentage possible while loading audio file in qb64 【发布时间】:2019-02-13 20:09:50 【问题描述】:

这是我目前拥有的

    PRINT "Loading..." 
soundHandle& = _SNDOPEN("Music.mp3") 'opens the sound file
_SNDPLAYFILE "Music.mp3", , .25 'plays the sound file

我希望它在加载时增加一个百分比,如果可能的话,

【问题讨论】:

【参考方案1】:

由于我不知道要加载的文件的大小,我只能提供:

REM sample to display approx. percent while loading file
CLS
PRINT "Loading..."
PRINT "000%";
ON TIMER(1) GOSUB DisplayPercent
TIMER ON
soundHandle& = _SNDOPEN("Music.mp3") 'opens the sound file
_SNDPLAYFILE "Music.mp3", , .25 'plays the sound file
DO WHILE INKEY$="":LOOP 'wait while sound plays
TIMER OFF 'turn off timer after entire file played
END
DisplayPercent:
P = P + 10
IF P <= 100 THEN
    FOR L = 1 TO 4
        PRINT CHR$(29); " "; CHR$(29);
    NEXT
    PRINT RIGHT$("000" + MID$(STR$(P), 2), 3); "%";
END IF
RETURN

【讨论】:

【参考方案2】:

好的,我已经设法在 QB64 v1.2 中获得了正在播放的 .mp3 文件的百分比:

REM sample to display approx. percent while loading/playing file.
CLS
PRINT "Loading..."
t1 = _FREETIMER
soundHandle& = _SNDOPEN("music.mp3", "LEN") 'opens the sound file
IF soundHandle& = 0& THEN PRINT "Error opening file": END
x& = _SNDLEN(soundHandle&) 'get length of file in seconds
PRINT "Length:"; x&; "seconds."
PRINT "000%";
x! = x& / 100 'calculate percent
IF x! = 0! THEN x! = 1!
ON TIMER(t1, x!) GOSUB DisplayPercent
TIMER(t1) ON
_SNDPLAY soundHandle&
DO UNTIL INKEY$ <> ""
    _LIMIT 50
    x% = _SNDPLAYING(soundHandle&) 'check when file has finished playing
    IF x% = 0 THEN EXIT DO
LOOP
_SNDCLOSE (soundHandle&)
TIMER(t1) OFF
TIMER(t1) FREE
END
DisplayPercent:
P = P + 1
IF P > 100 THEN RETURN
FOR L = 1 TO 4
    PRINT CHR$(29); " "; CHR$(29);
NEXT
PRINT RIGHT$("000" + MID$(STR$(P), 2), 3); "%";
RETURN

【讨论】:

我使用的是QB64 v1.2 只要歌曲一直在播放。它不加载歌曲,只获取长度并将其分成百分比.. 我应该更具体一些,感谢您所做的工作,但是有没有办法显示它加载的百分比? 你放的第一个按照我需要的方式完成,但是当它完成加载到转到menu时我需要它,我需要在goto menu:之后添加goto menu:PRINT RIGHT$("000" = MID$9STR$(p), 2), 3); "%": 启动计时器后,您可以转到菜单:任何您想要的地方。【参考方案3】:

另一个在捕获键盘输入和调用菜单时播放 .mp3 文件的示例:子例程:(并在标题栏上显示百分比);

REM sample to display approx. percent in title bar while playing file
CLS
COLOR 15
PRINT "Playing..."
t1 = _FREETIMER
Filename$ = "music.mp3"
soundHandle& = _SNDOPEN(Filename$, "LEN") 'opens the sound file
IF soundHandle& = 0& THEN PRINT "Error opening file": END
x& = _SNDLEN(soundHandle&) 'get length of file in seconds
COLOR 14
PRINT "Length:"; x&; "seconds."
_TITLE "000%"
x! = x& / 100 'calculate percent
IF x! = 0! THEN x! = 1!
ON TIMER(t1, x!) GOSUB DisplayPercent
TIMER(t1) ON
_SNDPLAY soundHandle&
Playing = -1
DO
    _LIMIT 50
    x$ = INKEY$
    IF x$ = CHR$(27) THEN EXIT DO
    IF LEN(x$) THEN GOSUB menu
    IF Playing THEN
        x% = _SNDPLAYING(soundHandle&) 'check when file has finished playing
        IF x% = 0 THEN
            Playing = 0
            COLOR 14
            PRINT "Music playback finished.."
            _SNDCLOSE (soundHandle&)
            TIMER(t1) OFF
            TIMER(t1) FREE
        END IF
    END IF
LOOP
IF Playing THEN
    _SNDSTOP soundHandle&
    _SNDCLOSE (soundHandle&)
    TIMER(t1) OFF
    TIMER(t1) FREE
END IF
COLOR 7
END
' trap input and do stuff here:
menu:
COLOR 15
IF LEN(x$) = 2 THEN
    PRINT "You pressed: 0"; ASC(RIGHT$(x$, 1))
ELSE
    PRINT "You pressed:"; ASC(x$)
END IF
RETURN
' display filename and percent on title bar
DisplayPercent:
P = P + 1
IF P > 100 THEN P = 101: RETURN ' check overflow
_TITLE "Playing " + Filename$ + "  -  " + RIGHT$("000" + MID$(STR$(P), 2), 3) + "%"
RETURN

【讨论】:

以上是关于在 qb64 中加载音频文件时可能加载条或百分比的主要内容,如果未能解决你的问题,请参考以下文章

使用 Linkage 从库中加载音频文件时出错

无法在 GlowCode x64 中加载符号

当枢轴选择更改时,如何在枢轴项目中加载图像后加载音频文件

如何获取 HTML5 视频中加载的当前片段百分比?

在 python 中从 zip 加载音频文件时出错

在 Elementor 中加载 youtube 视频时暂停音频播放器