diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index c019e05..1eaa80f 100644 --- a/lib/dxruby_sdl/sound.rb +++ b/lib/dxruby_sdl/sound.rb @@ -21,7 +21,7 @@ def initialize(filename) end end - def_delegators :@sound, :play, :set_volume + def_delegators :@sound, :play, :set_volume, :stop alias_method :setVolume, :set_volume @@ -39,6 +39,10 @@ def play def set_volume(volume, time = 0) raise NotImplementedError, 'Sound#set_volume(volume, time) with MIDI' end + + def stop + SDL::Mixer.halt_music + end end private_constant :Music @@ -65,6 +69,10 @@ def set_volume(volume, time = 0) end @wave.set_volume(volume) end + + def stop + SDL::Mixer.halt(@last_played_channel) + end end private_constant :Wave end diff --git a/spec/lib/dxruby_sdl/sound_spec.rb b/spec/lib/dxruby_sdl/sound_spec.rb index e3ebea5..bd879c7 100644 --- a/spec/lib/dxruby_sdl/sound_spec.rb +++ b/spec/lib/dxruby_sdl/sound_spec.rb @@ -2,6 +2,16 @@ require 'spec_helper' describe DXRubySDL::Sound, '音を表すクラス' do + shared_context 'WAVE file', wave: true do + let(:path) { fixture_path('sound.wav') } + let(:sound) { DXRubySDL::Sound.new(path) } + end + + shared_context 'MIDI file', midi: true do + let(:path) { fixture_path('bgm.mid') } + let(:sound) { DXRubySDL::Sound.new(path) } + end + describe '.new' do shared_context '.new' do subject { DXRubySDL::Sound.new(fixture_path(filename)) } @@ -25,10 +35,7 @@ end describe '#play' do - context 'WAVE形式のファイルの場合' do - let(:path) { fixture_path('sound.wav') } - let(:sound) { DXRubySDL::Sound.new(path) } - + context 'WAVE file', wave: true do subject { sound.play } it 'SDL::Mixer.play_channelを呼び出す' do @@ -62,10 +69,7 @@ end end - context 'MIDI形式のファイルの場合' do - let(:path) { fixture_path('bgm.mid') } - let(:sound) { DXRubySDL::Sound.new(path) } - + context 'MIDI file', midi: true do subject { sound.play } it 'SDL::Mixer.play_musicを呼び出す' do @@ -76,4 +80,40 @@ end end end + + describe '#stop' do + context 'WAVE file', wave: true do + let(:path) { fixture_path('sound.wav') } + let(:sound) { DXRubySDL::Sound.new(path) } + + subject { sound.stop } + + before do + allow(SDL::Mixer).to receive(:halt) + sound.play + subject + end + + describe SDL::Mixer do + it { expect(SDL::Mixer).to have_received(:halt).with(0).once } + end + end + + context 'MIDI file', midi: true do + let(:path) { fixture_path('bgm.mid') } + let(:sound) { DXRubySDL::Sound.new(path) } + + subject { sound.stop } + + before do + allow(SDL::Mixer).to receive(:halt_music) + sound.play + subject + end + + describe SDL::Mixer do + it { expect(SDL::Mixer).to have_received(:halt_music).with(no_args).once } + end + end + end end