Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   An Audio and Video class for ruby (http://www.programmingforums.org/showthread.php?t=9002)

Master Mar 23rd, 2006 11:33 AM

An Audio and Video class for ruby
 
This code can be used for both audio and video file. Please give credit for using it. you can modify the code as much as you want. as you can see you edit it so that the files are used from any folder by changing this variables

DIR = "Audio"
@movie_name = "Movies/"+name

To use the audio, you simply do
myAudio = Audio.new() #create an instance
myAudio = Audio.new(filename) #you can use the file with and without the extension name
if you use decide to use the instance without the filename, you can simple call myAudio.audio_open(filename) and the myAudio.audio_play()


:

#==============================================================================
# ** 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')
  AUXCAPS_VOLUME = 0x1 #supports volume control
  AUXCAPS_LRVOLUME = 0x2 #separate left-right volume control
  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"
      audio_play
    end
  end
  #--------------------------------------------------------------------------
  # * Audio Length
  #--------------------------------------------------------------------------
  def audio_length(filename = @filename)
    status = " " * 30
    MCISendString.call("status "+@filename+" length",status,255,0)
    true_status = status.unpack("*v")
    return true_status.to_s
  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.to_s
  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


Dietrich Mar 24th, 2006 4:38 AM

What is required for Win32API?

Master Mar 24th, 2006 6:44 AM

Nothing is required, once you install ruby on your computer. it has the win32api. I am sorry to put the line for api ^_^. just add this above the code.
require "Win32API"

hopes that help

Arevos Mar 24th, 2006 6:49 AM

Quote:

Originally Posted by Master
Nothing is required

Except, presumably, a Windows OS ;)


All times are GMT -5. The time now is 12:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC