| 赞 | 0 |
| VIP | |
| 好人卡 | |
| 积分 | 1 |
| 经验 | |
| 最后登录 | 2014-9-27 |
| 在线时间 | 62 小时 |
- 梦石
- 0
- 星屑
- 50
- 在线时间
- 62 小时
- 注册时间
- 2011-1-28
- 回帖
- 24
|
发表于 2013-8-3 15:47:34
|
显示全部楼层
可以直接用 API 的多媒體接口
載入 播放 什麼的都沒問題
只是 SEEK(就是設定由那裡開始播放/重播也需要用到) 的時候倒是會卡一下
- #==============================================================================
- # ■ CacheAudio
- #------------------------------------------------------------------------------
- # 本模組載入音效,建立並保存音頻物件。為使播放暢順並節省内存,本模組可把指定
- # 音效預先讀入,使得程序在需求已存在的音效時能快速讀取。
- #==============================================================================
- $mciSendStringW = Win32API.new("winmm","mciSendStringW",['P','P','I','L'],'L')
- $mciGetErrorStringW = Win32API.new("winmm","mciGetErrorStringW",['L','P','I'],'L')
- $: << Dir.getwd
- module CacheAudio
- #--------------------------------------------------------------------------
- # ● 載入音檔。
- # filename : Str.文件名
- # *device : Str.標識
- #--------------------------------------------------------------------------
- def self.open(filename,device=filename)
- return $mciSendStringW.call("open #{filename} alias #{device}".to_unc,0,0,0)
- end
- #--------------------------------------------------------------------------
- # ● 結束音檔。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.close(device)
- error = $mciSendStringW.call("close #{device}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 播放音檔。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.play(device)
- error = $mciSendStringW.call("play #{device}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 播放音檔。
- # device : Str.標識
- # *from : Int.位置
- #--------------------------------------------------------------------------
- def self.playfrom(device,from=0)
- error = $mciSendStringW.call("play #{device} from #{from}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 停止播放。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.stop(device)
- error = $mciSendStringW.call("stop #{device}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 暫停播放。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.pause(device)
- error = $mciSendStringW.call("pause #{device}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 恢復播放。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.resume(device)
- error = $mciSendStringW.call("resume #{device}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 移動游標。
- # device : Str.標識
- # *position : Int.位置
- #--------------------------------------------------------------------------
- def self.seek(device,position=0)
- position = "start" if position == 0
- position = "end" if position == 0 - 1
- error = $mciSendStringW.call("seek #{device} to #{position}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 設置音量。
- # device : Str.標識
- # volune : Int.音量
- #--------------------------------------------------------------------------
- def self.setvolume(device,volume)
- volume = [[volume,0].max,1000].min
- error = $mciSendStringW.call("setaudio #{device} volume to #{volume}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 獲取音量。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.getvolume(device)
- buf = " " * 256
- error = $mciSendStringW.call("status #{device} volume".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- return buf.strip.to_utf
- end
- #--------------------------------------------------------------------------
- # ● 漸變音量
- # device : Str.標識
- # time : Int.漸變時間
- # to : Int.最後音量
- #--------------------------------------------------------------------------
- def self.gradual(device,time,to)
- Thread.new {
- begin
- unless time <= 0
- volume = getvolume(device).to_f
- change = (to - getvolume(device)).to_f / time / 10
- for t in 0...time * 10
- setvolume(device,volume.to_i)
- volume += change
- sleep 0.08
- end
- end
- setvolume(device,to)
- rescue Exception => ex
- p "淡入或淡出時發生了異常:",ex
- end
- }
- end
- #--------------------------------------------------------------------------
- # ● 設置播放速度。
- # device : Str.標識
- # speed : Int.播放速度
- #--------------------------------------------------------------------------
- def self.setspeed(device,speed)
- speed = [[speed,500].max,1500].min
- error = $mciSendStringW.call("set #{device} speed #{speed}".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- end
- #--------------------------------------------------------------------------
- # ● 獲取播放速度。
- # device : Str.標識
- #--------------------------------------------------------------------------
- def self.getspeed(device)
- buf = " " * 256
- error = $mciSendStringW.call("status #{device} speed".to_unc,0,0,0)
- raise CacheAudioException.new(error) if error != 0
- return buf.strip.to_utf
- end
- end
- #==============================================================================
- # ■ CacheAudioException
- #------------------------------------------------------------------------------
- # 音頻貯藏所模塊異常類。
- #==============================================================================
- class CacheAudioException < Exception
- #--------------------------------------------------------------------------
- # ● 初始化异常类对象
- # code : 错误代码
- #--------------------------------------------------------------------------
- def initialize(code)
- return super if code.class == String
- @msg = " " * 512
- $mciGetErrorStringW.call(code,@msg,256)
- @msg = "錯誤代碼︰#{code} #{@msg.to_utf}"
- end
- #--------------------------------------------------------------------------
- # ● 返回异常对象的字符串描述
- #--------------------------------------------------------------------------
- def to_s
- return @msg if @msg
- return super
- end
- end
复制代码- #==============================================================================
- # ■ String
- #------------------------------------------------------------------------------
- # 字串類。
- #==============================================================================
- class String
- #--------------------------------------------------------------------------
- # ● 返回自身是否全部為美國資訊交換標準代碼的字符。
- # ‧ Bol
- #--------------------------------------------------------------------------
- def ascii?
- self.each_byte{|x| return false if x >= 128}
- return true
- end
- #--------------------------------------------------------------------------
- # ● 返回自身刪除位元組順序記號後的字串。
- # ‧ Str
- #--------------------------------------------------------------------------
- def delete_bom
- return self[3..0 - 1] if self[0,3] == "#{(239).chr}#{(187).chr}#{(191).chr}"
- return self
- end
- @@MultiByteToWideChar = Win32API.new("kernel32", "MultiByteToWideChar", ['I', 'L', 'P', 'I', 'P', 'I'], 'I')
- @@WideCharToMultiByte = Win32API.new("kernel32", "WideCharToMultiByte", ['I', 'L', 'P', 'I', 'P', 'I', 'P', 'P'], 'I')
- #--------------------------------------------------------------------------
- # ● 返回自身轉碼為"unicode"後的字串。如自身編碼非"UTF-8",經運算後,字串
- # 可能出現亂碼。
- # ‧ Str
- #--------------------------------------------------------------------------
- def to_unc #unc=>unicode
- len = @@MultiByteToWideChar.call(65001,0,self,0 - 1,0,0) << 1
- buf = " " * len
- @@MultiByteToWideChar.call(65001,0,self,0 - 1,buf,len)
- return buf
- end
- #--------------------------------------------------------------------------
- # ● 返回自身轉碼為"UTF-8"後的字串。如自身編碼非"unicode",經運算後,字串
- # 可能出現亂碼。
- # ‧ Str
- #--------------------------------------------------------------------------
- def to_utf #utf=>UTF-8
- len = @@WideCharToMultiByte.call(65001,0,self,0 - 1,0,0,0,0)
- buf = " " * len
- @@WideCharToMultiByte.call(65001,0,self,0 - 1,buf,len,0,0)
- buf.slice!(0 - 1,1)
- return buf
- end
- end
复制代码 |
|