查看: 3866|回复: 2

[已经解决] 【预读】 声音文件 ?

[复制链接]
梦石
0
星屑
50
在线时间
521 小时
注册时间
2011-12-7
回帖
148
发表于 2013-8-3 01:52:21 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 Ceopal 于 2013-8-3 18:09 编辑


  图片可以,那么声音应该也可以吧?
梦石
0
星屑
752
在线时间
800 小时
注册时间
2011-10-20
回帖
2257

开拓者

发表于 2013-8-3 10:48:31 来自手机 | 显示全部楼层
你见过哪个2d游戏,酷狗啥的bgm长音乐使用是在游戏中干的。。
你可以发现卡住了音乐还在走。。。
所以update在哪,。。知道了吧0。0
我们能做的就是让audio.play
回复

使用道具 举报

梦石
0
星屑
50
在线时间
62 小时
注册时间
2011-1-28
回帖
24
发表于 2013-8-3 15:47:34 | 显示全部楼层
可以直接用 API 的多媒體接口
載入 播放 什麼的都沒問題
只是 SEEK(就是設定由那裡開始播放/重播也需要用到) 的時候倒是會卡一下
  1. #==============================================================================
  2. # ■ CacheAudio
  3. #------------------------------------------------------------------------------
  4. #   本模組載入音效,建立並保存音頻物件。為使播放暢順並節省内存,本模組可把指定
  5. #   音效預先讀入,使得程序在需求已存在的音效時能快速讀取。
  6. #==============================================================================

  7. $mciSendStringW = Win32API.new("winmm","mciSendStringW",['P','P','I','L'],'L')
  8. $mciGetErrorStringW = Win32API.new("winmm","mciGetErrorStringW",['L','P','I'],'L')
  9. $: << Dir.getwd
  10. module CacheAudio
  11.   #--------------------------------------------------------------------------
  12.   # ● 載入音檔。
  13.   #    filename : Str.文件名
  14.   #    *device  : Str.標識
  15.   #--------------------------------------------------------------------------
  16.   def self.open(filename,device=filename)
  17.     return $mciSendStringW.call("open #{filename} alias #{device}".to_unc,0,0,0)
  18.   end
  19.   #--------------------------------------------------------------------------
  20.   # ● 結束音檔。
  21.   #    device : Str.標識
  22.   #--------------------------------------------------------------------------
  23.   def self.close(device)
  24.     error = $mciSendStringW.call("close #{device}".to_unc,0,0,0)
  25.     raise CacheAudioException.new(error) if error != 0
  26.   end
  27.   #--------------------------------------------------------------------------
  28.   # ● 播放音檔。
  29.   #    device : Str.標識
  30.   #--------------------------------------------------------------------------
  31.   def self.play(device)
  32.     error = $mciSendStringW.call("play #{device}".to_unc,0,0,0)
  33.     raise CacheAudioException.new(error) if error != 0
  34.   end
  35.   #--------------------------------------------------------------------------
  36.   # ● 播放音檔。
  37.   #    device : Str.標識
  38.   #    *from  : Int.位置
  39.   #--------------------------------------------------------------------------
  40.   def self.playfrom(device,from=0)
  41.     error = $mciSendStringW.call("play #{device} from #{from}".to_unc,0,0,0)
  42.     raise CacheAudioException.new(error) if error != 0
  43.   end
  44.   #--------------------------------------------------------------------------
  45.   # ● 停止播放。
  46.   #    device : Str.標識
  47.   #--------------------------------------------------------------------------
  48.   def self.stop(device)
  49.     error = $mciSendStringW.call("stop #{device}".to_unc,0,0,0)
  50.     raise CacheAudioException.new(error) if error != 0
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # ● 暫停播放。
  54.   #    device : Str.標識
  55.   #--------------------------------------------------------------------------
  56.   def self.pause(device)
  57.     error = $mciSendStringW.call("pause #{device}".to_unc,0,0,0)
  58.     raise CacheAudioException.new(error) if error != 0
  59.   end
  60.   #--------------------------------------------------------------------------
  61.   # ● 恢復播放。
  62.   #    device : Str.標識
  63.   #--------------------------------------------------------------------------
  64.   def self.resume(device)
  65.     error = $mciSendStringW.call("resume #{device}".to_unc,0,0,0)
  66.     raise CacheAudioException.new(error) if error != 0
  67.   end
  68.   #--------------------------------------------------------------------------
  69.   # ● 移動游標。
  70.   #    device    : Str.標識
  71.   #    *position : Int.位置
  72.   #--------------------------------------------------------------------------
  73.   def self.seek(device,position=0)
  74.     position = "start" if position == 0
  75.     position = "end" if position == 0 - 1
  76.     error = $mciSendStringW.call("seek #{device} to #{position}".to_unc,0,0,0)
  77.     raise CacheAudioException.new(error) if error != 0
  78.   end
  79.   #--------------------------------------------------------------------------
  80.   # ● 設置音量。
  81.   #    device : Str.標識
  82.   #    volune : Int.音量
  83.   #--------------------------------------------------------------------------
  84.   def self.setvolume(device,volume)
  85.     volume = [[volume,0].max,1000].min
  86.     error = $mciSendStringW.call("setaudio #{device} volume to #{volume}".to_unc,0,0,0)
  87.     raise CacheAudioException.new(error) if error != 0
  88.   end
  89.   #--------------------------------------------------------------------------
  90.   # ● 獲取音量。
  91.   #    device : Str.標識
  92.   #--------------------------------------------------------------------------
  93.   def self.getvolume(device)
  94.     buf = " " * 256
  95.     error = $mciSendStringW.call("status #{device} volume".to_unc,0,0,0)
  96.     raise CacheAudioException.new(error) if error != 0
  97.     return buf.strip.to_utf
  98.   end
  99.   #--------------------------------------------------------------------------
  100.   # ● 漸變音量
  101.   #    device : Str.標識
  102.   #    time   : Int.漸變時間
  103.   #    to     : Int.最後音量
  104.   #--------------------------------------------------------------------------
  105.   def self.gradual(device,time,to)
  106.     Thread.new {
  107.       begin
  108.         unless time <= 0
  109.           volume = getvolume(device).to_f
  110.           change = (to - getvolume(device)).to_f / time / 10
  111.           for t in 0...time * 10
  112.             setvolume(device,volume.to_i)
  113.             volume += change
  114.             sleep 0.08
  115.           end
  116.         end
  117.         setvolume(device,to)
  118.       rescue Exception => ex
  119.         p "淡入或淡出時發生了異常:",ex
  120.       end
  121.     }
  122.   end
  123.   #--------------------------------------------------------------------------
  124.   # ● 設置播放速度。
  125.   #    device : Str.標識
  126.   #    speed  : Int.播放速度
  127.   #--------------------------------------------------------------------------
  128.   def self.setspeed(device,speed)
  129.     speed = [[speed,500].max,1500].min
  130.     error = $mciSendStringW.call("set #{device} speed #{speed}".to_unc,0,0,0)
  131.     raise CacheAudioException.new(error) if error != 0
  132.   end
  133.   #--------------------------------------------------------------------------
  134.   # ● 獲取播放速度。
  135.   #    device : Str.標識
  136.   #--------------------------------------------------------------------------
  137.   def self.getspeed(device)
  138.     buf = " " * 256
  139.     error = $mciSendStringW.call("status #{device} speed".to_unc,0,0,0)
  140.     raise CacheAudioException.new(error) if error != 0
  141.     return buf.strip.to_utf
  142.   end
  143. end

  144. #==============================================================================
  145. # ■ CacheAudioException
  146. #------------------------------------------------------------------------------
  147. #  音頻貯藏所模塊異常類。
  148. #==============================================================================

  149. class CacheAudioException < Exception
  150.   #--------------------------------------------------------------------------
  151.   # ● 初始化异常类对象
  152.   #    code : 错误代码
  153.   #--------------------------------------------------------------------------
  154.   def initialize(code)
  155.     return super if code.class == String
  156.     @msg = " " * 512
  157.     $mciGetErrorStringW.call(code,@msg,256)
  158.     @msg = "錯誤代碼︰#{code}  #{@msg.to_utf}"
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # ● 返回异常对象的字符串描述
  162.   #--------------------------------------------------------------------------
  163.   def to_s
  164.     return @msg if @msg
  165.     return super
  166.   end
  167. end
复制代码
  1. #==============================================================================
  2. # ■ String
  3. #------------------------------------------------------------------------------
  4. #   字串類。
  5. #==============================================================================

  6. class String
  7.   #--------------------------------------------------------------------------
  8.   # ● 返回自身是否全部為美國資訊交換標準代碼的字符。
  9.   # ‧ Bol
  10.   #--------------------------------------------------------------------------
  11.   def ascii?
  12.     self.each_byte{|x| return false if x >= 128}
  13.     return true
  14.   end
  15.   #--------------------------------------------------------------------------
  16.   # ● 返回自身刪除位元組順序記號後的字串。
  17.   # ‧ Str
  18.   #--------------------------------------------------------------------------
  19.   def delete_bom
  20.     return self[3..0 - 1] if self[0,3] == "#{(239).chr}#{(187).chr}#{(191).chr}"
  21.     return self
  22.   end
  23.   @@MultiByteToWideChar = Win32API.new("kernel32", "MultiByteToWideChar", ['I', 'L', 'P', 'I', 'P', 'I'], 'I')
  24.   @@WideCharToMultiByte = Win32API.new("kernel32", "WideCharToMultiByte", ['I', 'L', 'P', 'I', 'P', 'I', 'P', 'P'], 'I')
  25.   #--------------------------------------------------------------------------
  26.   # ● 返回自身轉碼為"unicode"後的字串。如自身編碼非"UTF-8",經運算後,字串
  27.   #    可能出現亂碼。
  28.   # ‧ Str
  29.   #--------------------------------------------------------------------------
  30.   def to_unc #unc=>unicode
  31.     len = @@MultiByteToWideChar.call(65001,0,self,0 - 1,0,0) << 1
  32.     buf = " " * len
  33.     @@MultiByteToWideChar.call(65001,0,self,0 - 1,buf,len)
  34.     return buf
  35.   end
  36.   #--------------------------------------------------------------------------
  37.   # ● 返回自身轉碼為"UTF-8"後的字串。如自身編碼非"unicode",經運算後,字串
  38.   #    可能出現亂碼。
  39.   # ‧ Str
  40.   #--------------------------------------------------------------------------
  41.   def to_utf #utf=>UTF-8
  42.     len = @@WideCharToMultiByte.call(65001,0,self,0 - 1,0,0,0,0)
  43.     buf = " " * len
  44.     @@WideCharToMultiByte.call(65001,0,self,0 - 1,buf,len,0,0)
  45.     buf.slice!(0 - 1,1)
  46.     return buf
  47.   end
  48. end
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部