Fix some bug of media player

This commit is contained in:
daleclack 2023-09-24 16:37:56 +08:00
parent d15466c8d4
commit 30f229d95a
2 changed files with 99 additions and 32 deletions

View File

@ -3,20 +3,20 @@
#include "MyMediaPlayer.h"
#include <cstdio>
#include <cstring>
#include <vector>
#define lyrics_max_length 1024
#define timestamp_length 11
static FILE *lyrics_file = NULL;
static gboolean line_read = FALSE, lyrics_updated = FALSE;
static char current_lyrics[lyrics_max_length];
void update_lyrics(MyMediaPlayer *player)
{
// Get position between filename and extension
int point_pos;
char *current_filename = my_media_player_get_filename(player);
g_print("%s\n", current_filename);
for (int i = strlen(current_filename) - 1; i > 0; i--)
{
if (current_filename[i] == '.')
@ -45,30 +45,83 @@ void update_lyrics(MyMediaPlayer *player)
lyrics_updated = TRUE;
}
static void get_substr(char *src, char *dest, size_t start, size_t end)
static void get_substr(char *src, char *dest, size_t start,
size_t end)
{
// Copy string to the end
if(strlen(src) == 0 || strlen(src) == timestamp_length)
if (strlen(src) == 0 || strlen(src) == start + 1)
{
return ;
return;
}
for (int i = 0; i < end; i++)
{
dest[i] = src[i + start];
dest[i] = src[i + start + 1];
}
dest[end] = '\0';
}
static size_t get_lrc_timestamp_end(const char *lrc_line)
{
size_t timestamp_end;
// Find the end of timestamp
for (int i = 0; i < strlen(lrc_line); i++)
{
if (lrc_line[i] == ']')
{
timestamp_end = i;
}
}
return timestamp_end;
}
static gint64 get_lrc_line_timestamp(const char *lyrics_line, size_t timestamp_length)
{
static gint64 lyric_time = -1;
// Minutes
gint64 lyric_min = (lyrics_line[1] - '0') * 10 +
(lyrics_line[2] - '0');
// Seconds
gint64 lyric_sec = (lyrics_line[4] - '0') * 10 +
(lyrics_line[5] - '0');
// Millseconds
gint64 lyric_micro;
switch (timestamp_length)
{
case 9:
lyric_micro = (lyrics_line[7] - '0') * 100 +
(lyrics_line[8] - '0') * 10;
break;
case 10:
lyric_micro = (lyrics_line[7] - '0') * 100 +
(lyrics_line[8] - '0') * 10 +
(lyrics_line[9] - '0');
break;
}
if (lyric_micro > 500)
{
lyric_micro += 1000;
}
lyric_time = lyric_micro + lyric_sec * 1000 +
lyric_min * 60 * 1000;
return lyric_time;
}
static void get_lyrics(gint64 curr_time, gboolean playing, MyMediaPlayer *player)
{
char lyrics_line[lyrics_max_length];
char label_string[lyrics_max_length];
static gint64 lyric_time = -1;
static size_t timestamp_length;
static gint64 lyric_time;
// g_print("%ld\n", curr_time);
// Get lyrics data
if (lyrics_file != NULL)
{
// g_print("Lrc file load successful\n");
if (playing && !line_read)
{
// Get lyrics time
@ -76,27 +129,32 @@ static void get_lyrics(gint64 curr_time, gboolean playing, MyMediaPlayer *player
fflush(lyrics_file);
// Some lrc files has empty lines
if(strlen(lyrics_line) == 0){
if (strlen(lyrics_line) == 0)
{
line_read = TRUE;
return;
}
// g_print("%s\n", lyrics_line);
gint64 lyric_min = (lyrics_line[1] - '0') * 10 +
(lyrics_line[2] - '0');
gint64 lyric_sec = (lyrics_line[4] - '0') * 10 +
(lyrics_line[5] - '0');
lyric_time = (lyrics_line[7] - '0') * 100 +
(lyrics_line[8] - '0') * 10 +
(lyrics_line[9] - '0') +
lyric_sec * 1000 +
lyric_min * 1000 * 60;
// Not a number
while (lyrics_line[1] < '0' || lyrics_line[1] > '9')
{
line_read = FALSE;
return;
}
// Get timestamp length
timestamp_length = get_lrc_timestamp_end(lyrics_line);
g_print("%s\n", lyrics_line);
lyric_time = get_lrc_line_timestamp(lyrics_line, timestamp_length);
// Remove time stamp
get_substr(lyrics_line, current_lyrics, timestamp_length,
strlen(lyrics_line) - timestamp_length);
line_read = TRUE;
}
// g_print("%ld\n", lyric_time);
if (curr_time / 100 == lyric_time / 100 && line_read || lyric_time == 0)
{
// Since a new line is read and time match, load lyrics
@ -114,6 +172,20 @@ static void get_lyrics(gint64 curr_time, gboolean playing, MyMediaPlayer *player
}
}
static gboolean get_media_playing(gint64 curr_time)
{
static gint64 tmp_time = -1;
if (curr_time == tmp_time)
{
return FALSE;
}
else
{
tmp_time = curr_time;
return TRUE;
}
}
static void get_media_stream_status(MyMediaPlayer *player,
GtkMediaStream *stream, gint64 timestamp)
{
@ -124,22 +196,12 @@ static void get_media_stream_status(MyMediaPlayer *player,
update_lyrics(player);
}
if (timestamp == 1)
if (get_media_playing(timestamp))
{
// Reset status when the media playing
lyrics_updated = FALSE;
}
}
gboolean get_media_playing(gint64 curr_time)
{
static gint64 tmp_time = -1;
if(curr_time == tmp_time){
return FALSE;
}else{
tmp_time = curr_time;
return TRUE;
}
// g_print("%d\n", lyrics_updated);
}
gboolean lyric_time_func(gpointer data)

View File

@ -96,8 +96,13 @@ static void column_view_activated(GtkColumnView *self, gint position, MyMediaPla
// Mark the player is ready and update current file name
player->music_loaded = TRUE;
strncpy(player->current_filename, file_name, strlen(file_name));
char *filename1 = player->current_filename;
strncpy(filename1, file_name, strlen(file_name));
filename1[strlen(file_name)] = '\0';
g_object_unref(music_file);
// Force update lyrics file
update_lyrics(player);
}
}