Start over when playing when already playing.

This commit is contained in:
Pim Kunis 2020-10-10 21:50:48 +02:00
parent 4992bd8cdc
commit 12e17d261f

View file

@ -38,8 +38,7 @@ defmodule MIDITools.Player do
start_time: epoch, start_time: epoch,
duration: 0, duration: 0,
synth: synth, synth: synth,
repeat: false, repeat: false
playing: false
}} }}
end end
@ -48,16 +47,15 @@ defmodule MIDITools.Player do
{:reply, :ok, %{state | schedule: schedule, schedule_left: schedule, duration: duration}} {:reply, :ok, %{state | schedule: schedule, schedule_left: schedule, duration: duration}}
end end
def handle_call(:play, _from, %{playing: true} = state) do def handle_call(:play, _from, %{timer: timer, schedule: schedule} = state) do
{:reply, {:error, :already_started}, state} if timer != nil do
end Process.cancel_timer(timer, info: false)
end
def handle_call(:play, _from, %{schedule: schedule} = state) do
start_time = Timex.now() start_time = Timex.now()
timer = start_timer(schedule, start_time) timer = start_timer(schedule, start_time)
{:reply, :ok, {:reply, :ok, %{state | timer: timer, start_time: start_time, schedule_left: schedule}}
%{state | timer: timer, start_time: start_time, schedule_left: schedule, playing: true}}
end end
def handle_call({:set_repeat, repeat}, _from, state) do def handle_call({:set_repeat, repeat}, _from, state) do
@ -66,7 +64,7 @@ defmodule MIDITools.Player do
def handle_call(:stop_playing, _from, %{timer: timer} = state) do def handle_call(:stop_playing, _from, %{timer: timer} = state) do
Process.cancel_timer(timer, info: false) Process.cancel_timer(timer, info: false)
{:reply, :ok, %{state | timer: nil, playing: false}} {:reply, :ok, %{state | timer: nil}}
end end
@impl true @impl true
@ -93,7 +91,7 @@ defmodule MIDITools.Player do
end end
def handle_info(:end, state) do def handle_info(:end, state) do
{:noreply, %{state | playing: false, timer: nil}} {:noreply, %{state | timer: nil}}
end end
# Private functions # Private functions