Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2006, 10:33 AM   #1
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
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
Master is offline   Reply With Quote
Old Mar 24th, 2006, 3:38 AM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
What is required for Win32API?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 24th, 2006, 5:44 AM   #3
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
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
Master is offline   Reply With Quote
Old Mar 24th, 2006, 5:49 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Master
Nothing is required
Except, presumably, a Windows OS
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:46 PM.

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