设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2918|回复: 2
打印 上一主题 下一主题

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

[复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
521 小时
注册时间
2011-12-7
帖子
305
跳转到指定楼层
1
发表于 2013-8-3 01:52:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

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


  图片可以,那么声音应该也可以吧?

Lv2.观梦者

梦石
0
星屑
687
在线时间
791 小时
注册时间
2011-10-20
帖子
2394

开拓者

2
发表于 2013-8-3 10:48:31 手机端发表。 | 只看该作者
你见过哪个2d游戏,酷狗啥的bgm长音乐使用是在游戏中干的。。
你可以发现卡住了音乐还在走。。。
所以update在哪,。。知道了吧0。0
我们能做的就是让audio.play
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
62 小时
注册时间
2011-1-28
帖子
35
3
发表于 2013-8-3 15:47:34 | 只看该作者
可以直接用 API 的多媒體接口
載入 播放 什麼的都沒問題
只是 SEEK(就是設定由那裡開始播放/重播也需要用到) 的時候倒是會卡一下
RUBY 代码复制
  1. #==============================================================================
  2. # ■ CacheAudio
  3. #------------------------------------------------------------------------------
  4. #   本模組載入音效,建立並保存音頻物件。為使播放暢順並節省内存,本模組可把指定
  5. #   音效預先讀入,使得程序在需求已存在的音效時能快速讀取。
  6. #==============================================================================
  7.  
  8. $mciSendStringW = Win32API.new("winmm","mciSendStringW",['P','P','I','L'],'L')
  9. $mciGetErrorStringW = Win32API.new("winmm","mciGetErrorStringW",['L','P','I'],'L')
  10. $: << Dir.getwd
  11. module CacheAudio
  12.   #--------------------------------------------------------------------------
  13.   # ● 載入音檔。
  14.   #    filename : Str.文件名
  15.   #    *device  : Str.標識
  16.   #--------------------------------------------------------------------------
  17.   def self.open(filename,device=filename)
  18.     return $mciSendStringW.call("open #{filename} alias #{device}".to_unc,0,0,0)
  19.   end
  20.   #--------------------------------------------------------------------------
  21.   # ● 結束音檔。
  22.   #    device : Str.標識
  23.   #--------------------------------------------------------------------------
  24.   def self.close(device)
  25.     error = $mciSendStringW.call("close #{device}".to_unc,0,0,0)
  26.     raise CacheAudioException.new(error) if error != 0
  27.   end
  28.   #--------------------------------------------------------------------------
  29.   # ● 播放音檔。
  30.   #    device : Str.標識
  31.   #--------------------------------------------------------------------------
  32.   def self.play(device)
  33.     error = $mciSendStringW.call("play #{device}".to_unc,0,0,0)
  34.     raise CacheAudioException.new(error) if error != 0
  35.   end
  36.   #--------------------------------------------------------------------------
  37.   # ● 播放音檔。
  38.   #    device : Str.標識
  39.   #    *from  : Int.位置
  40.   #--------------------------------------------------------------------------
  41.   def self.playfrom(device,from=0)
  42.     error = $mciSendStringW.call("play #{device} from #{from}".to_unc,0,0,0)
  43.     raise CacheAudioException.new(error) if error != 0
  44.   end
  45.   #--------------------------------------------------------------------------
  46.   # ● 停止播放。
  47.   #    device : Str.標識
  48.   #--------------------------------------------------------------------------
  49.   def self.stop(device)
  50.     error = $mciSendStringW.call("stop #{device}".to_unc,0,0,0)
  51.     raise CacheAudioException.new(error) if error != 0
  52.   end
  53.   #--------------------------------------------------------------------------
  54.   # ● 暫停播放。
  55.   #    device : Str.標識
  56.   #--------------------------------------------------------------------------
  57.   def self.pause(device)
  58.     error = $mciSendStringW.call("pause #{device}".to_unc,0,0,0)
  59.     raise CacheAudioException.new(error) if error != 0
  60.   end
  61.   #--------------------------------------------------------------------------
  62.   # ● 恢復播放。
  63.   #    device : Str.標識
  64.   #--------------------------------------------------------------------------
  65.   def self.resume(device)
  66.     error = $mciSendStringW.call("resume #{device}".to_unc,0,0,0)
  67.     raise CacheAudioException.new(error) if error != 0
  68.   end
  69.   #--------------------------------------------------------------------------
  70.   # ● 移動游標。
  71.   #    device    : Str.標識
  72.   #    *position : Int.位置
  73.   #--------------------------------------------------------------------------
  74.   def self.seek(device,position=0)
  75.     position = "start" if position == 0
  76.     position = "end" if position == 0 - 1
  77.     error = $mciSendStringW.call("seek #{device} to #{position}".to_unc,0,0,0)
  78.     raise CacheAudioException.new(error) if error != 0
  79.   end
  80.   #--------------------------------------------------------------------------
  81.   # ● 設置音量。
  82.   #    device : Str.標識
  83.   #    volune : Int.音量
  84.   #--------------------------------------------------------------------------
  85.   def self.setvolume(device,volume)
  86.     volume = [[volume,0].max,1000].min
  87.     error = $mciSendStringW.call("setaudio #{device} volume to #{volume}".to_unc,0,0,0)
  88.     raise CacheAudioException.new(error) if error != 0
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # ● 獲取音量。
  92.   #    device : Str.標識
  93.   #--------------------------------------------------------------------------
  94.   def self.getvolume(device)
  95.     buf = " " * 256
  96.     error = $mciSendStringW.call("status #{device} volume".to_unc,0,0,0)
  97.     raise CacheAudioException.new(error) if error != 0
  98.     return buf.strip.to_utf
  99.   end
  100.   #--------------------------------------------------------------------------
  101.   # ● 漸變音量
  102.   #    device : Str.標識
  103.   #    time   : Int.漸變時間
  104.   #    to     : Int.最後音量
  105.   #--------------------------------------------------------------------------
  106.   def self.gradual(device,time,to)
  107.     Thread.new {
  108.       begin
  109.         unless time <= 0
  110.           volume = getvolume(device).to_f
  111.           change = (to - getvolume(device)).to_f / time / 10
  112.           for t in 0...time * 10
  113.             setvolume(device,volume.to_i)
  114.             volume += change
  115.             sleep 0.08
  116.           end
  117.         end
  118.         setvolume(device,to)
  119.       rescue Exception => ex
  120.         p "淡入或淡出時發生了異常:",ex
  121.       end
  122.     }
  123.   end
  124.   #--------------------------------------------------------------------------
  125.   # ● 設置播放速度。
  126.   #    device : Str.標識
  127.   #    speed  : Int.播放速度
  128.   #--------------------------------------------------------------------------
  129.   def self.setspeed(device,speed)
  130.     speed = [[speed,500].max,1500].min
  131.     error = $mciSendStringW.call("set #{device} speed #{speed}".to_unc,0,0,0)
  132.     raise CacheAudioException.new(error) if error != 0
  133.   end
  134.   #--------------------------------------------------------------------------
  135.   # ● 獲取播放速度。
  136.   #    device : Str.標識
  137.   #--------------------------------------------------------------------------
  138.   def self.getspeed(device)
  139.     buf = " " * 256
  140.     error = $mciSendStringW.call("status #{device} speed".to_unc,0,0,0)
  141.     raise CacheAudioException.new(error) if error != 0
  142.     return buf.strip.to_utf
  143.   end
  144. end
  145.  
  146. #==============================================================================
  147. # ■ CacheAudioException
  148. #------------------------------------------------------------------------------
  149. #  音頻貯藏所模塊異常類。
  150. #==============================================================================
  151.  
  152. class CacheAudioException < Exception
  153.   #--------------------------------------------------------------------------
  154.   # ● 初始化异常类对象
  155.   #    code : 错误代码
  156.   #--------------------------------------------------------------------------
  157.   def initialize(code)
  158.     return super if code.class == String
  159.     @msg = " " * 512
  160.     $mciGetErrorStringW.call(code,@msg,256)
  161.     @msg = "錯誤代碼︰#{code}  #{@msg.to_utf}"
  162.   end
  163.   #--------------------------------------------------------------------------
  164.   # ● 返回异常对象的字符串描述
  165.   #--------------------------------------------------------------------------
  166.   def to_s
  167.     return @msg if @msg
  168.     return super
  169.   end
  170. end

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

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-10-9 23:18

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表