Thread: Media Player
View Single Post
Old Dec 10th, 2005, 7:36 PM   #9
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
#==============================================================================
# ** Audio
#==============================================================================
class Audio
  WAV = Win32API.new('winmm.dll', 'PlaySoundA', 'pll', 'l') 
  MCIExecute = Win32API.new("winmm", "mciExecute", ["P"], "B")
  MCISendString = Win32API.new('winmm','mciSendString','%w(p,p,l,p)','V')
  GetDevCaps = Win32API.new('winmm', 'auxGetDevCapsA', %w(l p l), 'l')
  GetVolume = Win32API.new('winmm', 'auxGetVolume', %w(l p), 'l')
  SetVolume = Win32API.new('winmm', 'auxSetVolume', %w(l l), 'l')
  GetNumDev = Win32API.new('winmm', 'auxGetNumDevs', %w(), 'l')
  AC = [0, 0, 0, 0, 0, 0].pack("L*")
  Volume = [0, 0].pack("L*")
  DIR = "Audio"
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :filename
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(filename = nil)
    dir = Dir.entries(DIR)
    @filename = rand(100).to_s
    unless filename.nil?
      dir.each{|file| 
      if file.include?(filename)
        @filename = file
        break
      end}
      @filename = filename if filename.include?('.')
      audio_open(@filename)
      audio_play()
    end
  end
  #--------------------------------------------------------------------------
  # * Audio Open
  #--------------------------------------------------------------------------
  def audio_open(filename)
    MCISendString.call("open "+DIR+"/"+filename+" alias "+@filename,0,0,0)
  end
  #--------------------------------------------------------------------------
  # * Audio Play
  #--------------------------------------------------------------------------
  def audio_play(filename = @filename)
    MCISendString.call("play " +filename+" from 0",0,0,0)
  end
  #--------------------------------------------------------------------------
  # * Audio Stop
  #--------------------------------------------------------------------------
  def audio_stop(filename = @filename)
    MCISendString.call("stop " + filename,0,0,0)
  end
  #--------------------------------------------------------------------------
  # * Audio Close
  #--------------------------------------------------------------------------
  def audio_close(filename = @filename)
    MCISendString.call("close "+ filename,0,0,0) 
  end
  #--------------------------------------------------------------------------
  # * Audio Pause
  #--------------------------------------------------------------------------
  def audio_pause(filename = @filename)
    MCISendString.call("pause "+ filename,0,0,0) 
  end
  #--------------------------------------------------------------------------
  # * Audio Resume
  #--------------------------------------------------------------------------
  def audio_resume(filename = @filename)
    #MCISendString.call("resume "+ filename,0,0,0) 
    MCISendString.call("play " +filename+"",0,0,0)
  end
  #--------------------------------------------------------------------------
  # * Audio Repeat
  #--------------------------------------------------------------------------
  def audio_repeat(filename = @filename)
    MCISendString.call("repeat "+ filename,0,0,0) 
  end
  #--------------------------------------------------------------------------
  # * Audio Seek
  #--------------------------------------------------------------------------
  def audio_seek(seek,filename = @filename)
    MCISendString.call("seek "+ filename + " to "+seek.to_s,0,0,0) 
  end
  #--------------------------------------------------------------------------
  # * Audio Rewind
  #--------------------------------------------------------------------------
  def audio_rewind(filename = @filename)
    MCISendString.call("rewind "+ filename,0,0,0) 
  end
  #--------------------------------------------------------------------------
  # * Audio Playback
  #--------------------------------------------------------------------------
  def audio_playback(filename = @filename)
    if audio_status == "stop".split(//)
      audio_play
    end
  end
  #--------------------------------------------------------------------------
  # * Audio Status
  #--------------------------------------------------------------------------
  def audio_status(filename = @filename)
    status = " " * 255
    MCISendString.call("status "+@filename+" mode",status,255,0)
    true_status = status.unpack("aaaa")
    return true_status
  end
end

#==============================================================================
# ** Movie
#==============================================================================
class Movie
  FindWindow = Win32API.new('user32', 'FindWindow', %w(p p), 'l')
  MCISEND = Win32API.new('winmm','mciSendString','%w(p,p,l,l)','V')
  Message = Win32API.new('user32','SendMessage','%w(l,l,l,l)','V')
  SystemMetric = Win32API.new('user32','GetSystemMetrics','%w(l)','L')
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :hwnd
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(movie, window = nil)
    @filename = Dir.entries("Movies")
    for i in 0...@filename.size
      if @filename[i].include?(movie)
        name = @filename[i]
        break
      end
    end
    @movie_name = "Movies/"+name
    @window = window
    main
  end
  #--------------------------------------------------------------------------
  # * Main
  #--------------------------------------------------------------------------
  def main
    @hwnd = FindWindow.call(nil,@window)
    MCISEND.call("open \""+@movie_name+"\" alias FILE style 1073741824 parent " + @hwnd.to_s,0,0,0)
    width = SystemMetric.call(0)
    if @width == 640
      Graphic.update
      sleep(1)
      Graphic.update
      sleep(1)
      Graphic.update
      sleep(1)
    end
    status = " " * 255
    MCISEND.call("play FILE",0,0,0)
    loop do
      sleep(0.1)
      Message.call(@hwnd.to_i,11,0,0)
      Message.call(@hwnd.to_i,11,1,0)
      MCISEND.call("status FILE mode",status,255,0)
      true_status = status.unpack("aaaa")
      break if true_status == "stop".split(//)
    end
    MCISEND.call("close FILE",0,0,0)
  end
end
the code contain a class for audio and movie, hope it helps
Master is offline   Reply With Quote