Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Number of songs = 100
total duration = 24 hrs
total Play duration = 14 hrs
each song fixed play duration = 1 min

I want to play the song for total duration it will be achieved by playing each song for the fixed play duration
and taking pauses or skipping to another song. The problem is how to decide when to pause and for how much time
it will pause and when to skip, there will be randomness to pause and skip. SO that overall play pause and skip
becomes uniform.

* Songs can play multiple time, meaning if 100 songs had been played, it will again start from 0 all the way down to 100 and again start from front.

////////
Here
I have taken some fixed parameters:

number of songs = 10
total duration = 24 mins
total Play duration = 14 mins
each song fixed play duration = 1 sec
*************


But the problem is I'm not getting desired result.

What I have tried:

Python
import random

# fixed parameters
#------------------------------
num_songs = 10
total_duration = 24*60 
fixed_song_duration = 10
target_play_duration = 14*60
#------------------------------

min_play_duration = target_play_duration - 60 
max_play_duration = target_play_duration + 60 

songs = [f"song{i}" for i in range(1, num_songs+1)]

play_duration = 0
pause_duration = 0
total_play_duration = 0

while total_play_duration < total_duration:
    for song in songs:
        # if total_play_duration > total_duration:
        #     break
        print(f"Now Playing: {song}")
        play_duration += fixed_song_duration
        total_play_duration += fixed_song_duration
        print(f"Song ended. Total play duration: {total_play_duration:.2f} seconds. {total_play_duration/60:.2f} minutes.\n")

        skip_next = random.random() < 0.3
        if skip_next:
            print("Playing the next song.(skipped pause)")
            continue


        if total_play_duration < min_play_duration:
            take_break = random.random() < 0.4
        elif total_play_duration > max_play_duration:
            take_break = random.random() < 0.9
        else:
            take_break = random.random() < 0.7

        if take_break:
            break_duration = random.uniform(0.3*60, 1*60)
            pause_duration += break_duration
            total_play_duration += break_duration
            print(f"[!] Pausing for {break_duration:.2f} seconds.")
            print(f"[+] Total play duration: {total_play_duration:.2f} seconds. {total_play_duration/60:.2f} minutes\n")
        else:
            print(f"Playing the next song.")

print(f"Played for: {play_duration/60} minutes.")
print(f"Paused for: {pause_duration/60} minutes.")
Posted
Updated 1-May-23 1:58am
v2
Comments
Richard MacCutchan 1-May-23 3:56am    
"But the problem is I'm not getting desired result."
Well unless you explain what result you get against what actual result you expect, we cannot do much to help.
[no name] 1-May-23 11:02am    
Random and respecting fixed play for a specific time are mutually exclusive.
agajjsjs 26-Sep-23 3:15am    
great

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900