Jump to content

Problem with dxGetTextWidth Name of Song


Looney;3

Recommended Posts

Hey I need Help with Scripting

please :D

ERROR dxGetTextWidth

d769b676500ac2bc7569015871785f67o.jpg

local screenX, screenY = guiGetScreenSize() 
local animState = "close" 
  setTimer(outputChatBox,1000*60*0,0,"#005588[info]:#ffffff Name Song - #232323Command:#0066EE \"names\"", 255, 255, 255, true) 
  setTimer(outputChatBox,1000*60*0,0,"#005588[info]:#FFFFFFStart and Stop Radio #232323Command:#0066EE \"song\" #FFFFFFor Press #0066EE\"i\"", 255, 255, 255, true) 
  
function Start() 
  outputChatBox("#005588[info]:#ffffff Name Song - #232323Command:#0066EE \"names\"", 255, 255, 255, true) 
  outputChatBox("#005588[info]:#FFFFFFStart and Stop Radio #232323Command:#0066EE \"song\" #FFFFFFor Press #0066EE\"i\"", 255, 255, 255, true) 
  start = getTickCount() 
  animState = "open" 
  rend() 
  zeneindit() 
end 
addEventHandler("onClientResourceStart", resourceRoot, Start) 
  
function zeneindit() 
  radio = playSound("http://stream.dubbase.fm:7004", true) 
  outputChatBox("#005588[Radio]:#ffffff On", 255, 255, 255, true) 
end 
  
function mimegy() 
  meta = getSoundMetaTags(radio) 
  outputChatBox("#005588[Radio]:#ffffff Now Play: " .. meta.stream_title, 255, 255, 255, true) 
end 
addCommandHandler("names", mimegy) 
  
function RendStart() 
  meta = getSoundMetaTags(radio) 
  mimegy = meta.stream_title 
  if not mostmegy then 
    mostmegy = "radio" 
  end 
  if mostmegy ~= mimegy then 
    mostmegy = mimegy 
  end 
end 
addEventHandler("onClientRender", getRootElement(),RendStart) 
  
function asd() 
  textWidth = dxGetTextWidth(meta.stream_title, 1, "default-bold") 
  textWidth2 = textWidth / 2 
   
    local tick = getTickCount() - start 
    local progress = tick/3000 
    if animState == "open" then  
  posY = interpolateBetween(screenX + 10, 0, 0, screenY - screenY/1.01123595505618, 0, 0, progress, "OutBounce") 
  else 
  posY = interpolateBetween(0, 0, 0,screenX + 10, 0, 0, progress, "OutBounce") 
  end 
  dxDrawRectangle(screenX / 2 - textWidth2 - 6, posY - 2, textWidth2 * 2 + 12, 63, tocolor(255, 255, 255, 100))--background 
  dxDrawRectangle(screenX / 2 - textWidth2 - 5, posY - 1, textWidth2 * 2 + 10, 30, tocolor(25, 25, 25, 200))--SONG text background 
  dxDrawRectangle(screenX / 2 - textWidth2 - 5, posY + 30, textWidth2 * 2 + 10, 30, tocolor(10, 10, 10, 200))--SONG Name background 
  dxDrawText("SONG", screenX / 2 - textWidth2, posY - 1, screenX / 2 + textWidth2, posY + 30, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false) 
  dxDrawText(meta.stream_title, screenX / 2 - textWidth2, posY + 30, screenX / 2 + textWidth2, posY + 60, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false) 
  
end 
  
function rend() 
    addEventHandler("onClientRender", root, asd) 
    addEventHandler("onClientRender", getRootElement(),RendStart) 
end 
  
function Removerend() 
setTimer(function() 
    removeEventHandler("onClientRender", root, asd) 
    removeEventHandler("onClientRender", getRootElement(),RendStart) 
    stopSound(radio) 
   end, 800, 1) 
end 
  
function show(x) 
    if x == true then 
    rend() 
    zeneindit() 
    animState = "open" 
    start = getTickCount() 
    else 
    Removerend() 
    outputChatBox("#005588[Radio]:#ffffff Off", 255, 255, 255, true) 
    start = getTickCount() 
    animState = "close" 
    end 
    active = x 
end 
  
function isShowCustomHud() 
return active 
end 
function StopStart() 
if isShowCustomHud() == false then 
    show(true) 
else 
    show(false) 
end 
end 
addCommandHandler("song",StopStart) 
bindKey("i", "down", StopStart) 
  
fileDelete("radio_client.lua") 

Link to comment
  • Moderators

2 reasons why this error shows up.

  1. The meta data isn't downloaded yet from the streamed song. You have to keep in mind that the meta isn't directly available. The first part of the song have to be downloaded before it will shows up, while the lua code doesn't wait for it.
  2. The song doesn't have a title.

Can be simply solved with:

if meta then -- check if there is a meta available. 
textWidth = dxGetTextWidth(meta.stream_title or "", 1, "default-bold") 

Link to comment
2 reasons why this error shows up.

  1. The meta data isn't downloaded yet from the streamed song. You have to keep in mind that the meta isn't directly available. The first part of the song have to be downloaded before it will shows up, while the lua code doesn't wait for it.
  2. The song doesn't have a title.

Can be simply solved with:

if meta then -- check if there is a meta available. 
textWidth = dxGetTextWidth(meta.stream_title or "", 1, "default-bold") 

Not exactly true, since the meta is always true if the sound is played, but the table will return empty, however it will become available after a while.

Also i'd recommend you to use a timer on 500ms or 1sec or even a larger time, since checking onClientRender for the song meta-data might be a bit performance-unwise since the song name wont make a difference if it changes over that time.

Link to comment
hmm, didn't know that because I never used it.
if meta and next(meta) then  
textWidth = dxGetTextWidth(meta.stream_title or "", 1, "default-bold") 

Yeah, i ran into this issue when making my radio-streaming script. Usually the meta-table fills after 2-3 seconds, but he needs to check if the meta is there since if the playSound doesnt work propertly it will return nil.

Same happens if the streamer is connected to the stream URL but it doesnt play a song, the meta-table will be true but empty.

Link to comment
Hah .. I just found out that this is that resource: https://community.multitheftauto.com/in ... ls&id=9262

But decompiled. Lel nice try to be "original".

screenX, screenY = guiGetScreenSize() 
  
function kiiromanevem() 
  outputChatBox("#005588[info]:#ffffff Mute Radio #232323Press #0066EEi", 255, 255, 255, true) 
end 
addEventHandler("onClientResourceStart", resourceRoot, kiiromanevem) 
function loadxml() 
  xmlFile = xmlLoadFile("options.xml") 
  if xmlFile then 
    animations = xmlFindChild(xmlFile, "animate", 0) 
    anim_mutat = xmlNodeGetValue(xmlFindChild(animations, "in", 0)) 
    anim_rejt = xmlNodeGetValue(xmlFindChild(animations, "out", 0)) 
    anim_speed = xmlNodeGetValue(xmlFindChild(animations, "speed", 0)) 
    url = xmlNodeGetValue(xmlFindChild(xmlFile, "url", 0)) 
    autoplay = xmlNodeGetValue(xmlFindChild(xmlFile, "autoplay", 0)) 
  else 
    create_xmlFile = xmlCreateFile("options.xml", "root") 
    create_animations = xmlCreateChild(create_xmlFile, "animate") 
    xmlNodeSetValue(xmlCreateChild(create_animations, "in"), "OutBounce") 
    xmlNodeSetValue(xmlCreateChild(create_animations, "out"), "OutBounce") 
    xmlNodeSetValue(xmlCreateChild(create_animations, "speed"), "700") 
    xmlNodeSetValue(xmlCreateChild(create_xmlFile, "url"), "http://stream.dubbase.fm:7004") 
    xmlNodeSetValue(xmlCreateChild(create_xmlFile, "autoplay"), "true") 
    xmlSaveFile(create_xmlFile) 
    xmlFile = xmlLoadFile("options.xml") 
    animations = xmlFindChild(xmlFile, "animate", 0) 
    anim_mutat = xmlNodeGetValue(xmlFindChild(animations, "in", 0)) 
    anim_rejt = xmlNodeGetValue(xmlFindChild(animations, "out", 0)) 
    anim_speed = xmlNodeGetValue(xmlFindChild(animations, "speed", 0)) 
    url = xmlNodeGetValue(xmlFindChild(xmlFile, "url", 0)) 
    autoplay = xmlNodeGetValue(xmlFindChild(xmlFile, "autoplay", 0)) 
  end 
end 
addEventHandler("onClientResourceStart", resourceRoot, loadxml) 
addCommandHandler("reloadmxml", loadxml) 
  
  
function zeneindit() 
  zene = playSound(url, true) 
  outputChatBox("#005588[Radio]:#ffffff On", 255, 255, 255, true) 
  setRadioChannel(0) 
end 
  
function bekapcs_kikapcs() 
  if allapot then 
    outputChatBox("#005588[Radio]:#ffffff Off", 255, 255, 255, true) 
    stopSound(zene) 
    allapot = false 
    xmlNodeSetValue(xmlFindChild(xmlFile, "autoplay", 0), "false") 
    xmlSaveFile(xmlFile) 
  else 
    zeneindit() 
    allapot = true 
    xmlNodeSetValue(xmlFindChild(xmlFile, "autoplay", 0), "true") 
    xmlSaveFile(xmlFile) 
  end 
end 
  
function ellenorizd_hogy_inulhat() 
  if autoplay == "true" then 
    bekapcs_kikapcs() 
  end 
end 
addEventHandler("onClientResourceStart", resourceRoot, ellenorizd_hogy_inulhat) 
bindKey("i", "down", bekapcs_kikapcs) 
  
function mimegy() 
  most = getSoundMetaTags(zene) 
  outputChatBox("#005588[Radio]:#ffffff Now Play: " .. most.stream_title, 255, 255, 255, true) 
end 
addCommandHandler("nowc", mimegy) 
  
  
function makeRadioStayOff() 
  setRadioChannel(0) 
  cancelEvent() 
end 
addEventHandler("onClientPlayerRadioSwitch", getRootElement(), makeRadioStayOff) 
  
  
function tobbimute() 
  if allapot then 
    for id = 1, #getElementsByType("sound") do 
      if getElementsByType("sound")[id] ~= zene then 
        soundLength = getSoundLength(getElementsByType("sound")[id]) 
        if soundLength > 30 then 
          setSoundVolume(getElementsByType("sound")[id], 0) 
        end 
      end 
    end 
  elseif not nemitas then 
    for id = 1, #getElementsByType("sound") do 
      if getElementsByType("sound")[id] ~= zene then 
        soundLength = getSoundLength(getElementsByType("sound")[id]) 
        if soundLength > 30 then 
          setSoundVolume(getElementsByType("sound")[id], 1) 
        end 
      end 
    end 
  end 
end 
addEventHandler("onClientRender", getRootElement(), tobbimute) 
  
  
addEventHandler("onClientRender", getRootElement(), function() 
  most = getSoundMetaTags(zene) 
  mimegy = most.stream_title 
  if not mostmegy then 
    mostmegy = "zene" 
  end 
  if mostmegy ~= mimegy then 
    mostmegy = mimegy 
    mutat() 
  end 
end) 
  
-- screenX, screenY [890] 
function asd() 
  textWidth = dxGetTextWidth(most.stream_title, 1, "default-bold") 
  textWidth = textWidth / 2 
  tick = getTickCount() 
  posY = interpolateBetween(screenX + 10, 0, 0, screenY - screenY/1.01123595505618, 0, 0, 1, "OutBounce") 
  dxDrawRectangle(screenX / 2 - textWidth - 6, posY - 2, textWidth * 2 + 12, 63, tocolor(0, 0, 0, 255)) 
  dxDrawRectangle(screenX / 2 - textWidth - 5, posY - 1, textWidth * 2 + 10, 30, tocolor(23, 23, 23, 255)) 
  dxDrawRectangle(screenX / 2 - textWidth - 5, posY + 30, textWidth * 2 + 10, 30, tocolor(10, 10, 10, 255)) 
  dxDrawText("Now", screenX / 2 - textWidth, posY - 1, screenX / 2 + textWidth, posY + 30, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false) 
  dxDrawText(most.stream_title, screenX / 2 - textWidth, posY + 30, screenX / 2 + textWidth, posY + 60, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false) 
  if 5 <= (tick - tick2) / 1200 then 
    rejt() 
  end 
end 
  
  
function asd2() 
  textWidth = dxGetTextWidth(most.stream_title, 1, "default-bold") 
  textWidth = textWidth / 2 
  tick = getTickCount() 
  posY = interpolateBetween(screenX - screenY - screenY/1.01123595505618, 0, 0, screenY + 10, 0, 0, 1, "OutBounce") 
  dxDrawRectangle(screenX / 2 - textWidth - 6, posY - 2, textWidth * 2 + 12, 63, tocolor(0, 0, 0, 255)) 
  dxDrawRectangle(screenX / 2 - textWidth - 5, posY - 1, textWidth * 2 + 10, 30, tocolor(23, 23, 23, 255)) 
  dxDrawRectangle(screenX / 2 - textWidth - 5, posY + 30, textWidth * 2 + 10, 30, tocolor(10, 10, 10, 255)) 
  dxDrawText("Now", screenX / 2 - textWidth, posY - 1, screenX / 2 + textWidth, posY + 30, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false) 
  dxDrawText(most.stream_title, screenX / 2 - textWidth, posY + 30, screenX / 2 + textWidth, posY + 60, tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false) 
end 
  
  
function mutat() 
  tick2 = getTickCount() 
  removeEventHandler("onClientRender", getRootElement(), asd2) 
  addEventHandler("onClientRender", getRootElement(), asd) 
end 
addCommandHandler("now", mutat) 
  
  
function rejt() 
  tick3 = getTickCount() 
  addEventHandler("onClientRender", getRootElement(), asd2) 
  removeEventHandler("onClientRender", getRootElement(), asd) 
end 
  
  
function zene_nemitas() 
  if allapot then 
    if nemitas then 
      for id = 1, #getElementsByType("sound") do 
        if getElementsByType("sound")[id] then 
          soundLength = getSoundLength(getElementsByType("sound")[id]) 
          if getElementsByType("sound")[id] == zene then 
            setSoundVolume(getElementsByType("sound")[id], 0) 
          end 
        end 
      end 
    else 
      for id = 1, #getElementsByType("sound") do 
        if getElementsByType("sound")[id] == zene then 
          setSoundVolume(getElementsByType("sound")[id], 1) 
        end 
      end 
    end 
  elseif nemitas then 
    for id = 1, #getElementsByType("sound") do 
      if getElementsByType("sound")[id] then 
        soundLength = getSoundLength(getElementsByType("sound")[id]) 
        if soundLength > 30 then 
          setSoundVolume(getElementsByType("sound")[id], 0) 
        end 
      end 
    end 
  else 
    for id = 1, #getElementsByType("sound") do 
      if getElementsByType("sound")[id] then 
        soundLength = getSoundLength(getElementsByType("sound")[id]) 
        if soundLength > 30 then 
          setSoundVolume(getElementsByType("sound")[id], 1) 
        end 
      end 
    end 
  end 
end 
addEventHandler("onClientRender", getRootElement(), zene_nemitas) 
  
  
function nemit_ellenoriz() 
  if nemitas then 
    contador = getTickCount() 
    addEventHandler("onClientRender", getRootElement(), encendidoFunc) 
    nemitas = false 
  else 
    contador = getTickCount() 
    addEventHandler("onClientRender", getRootElement(), apagadoFunc) 
    nemitas = true 
  end 
end 
bindKey("m", "down", nemit_ellenoriz) 
  
  
function apagadoFunc() 
  if nemitas then 
    if 1 >= (getTickCount() - contador) / 1200 then 
      kep1, kep2 = interpolateBetween(0, 0, 0, 64, 64, 0, 1, "OutElastic") 
    elseif 1 <= (getTickCount() - contador) / 1200 then 
      kep1, kep2 = interpolateBetween(64, 64, 0, 0, 0, 0, 1, "InElastic") 
      if 1 == 1 then 
        removeEventHandler("onClientRender", getRootElement(), apagadoFunc) 
      end 
    end 
    dxDrawImage(screenX / 2 - kep1 / 2, screenY - 100 - kep2 / 2, kep1, kep2, "images/soundoff.png") 
  end 
end 
  
  
function encendidoFunc() 
  if not nemitas then 
    if 1 >= (getTickCount() - contador) / 1200 then 
      kep1, kep2 = interpolateBetween(0, 0, 0, 64, 64, 0, 1, "OutElastic") 
    elseif 1 <= (getTickCount() - contador) / 1200 then 
      kep1, kep2 = interpolateBetween(64, 64, 0, 0, 0, 0, 1, "InElastic") 
      if 1 == 1 then 
        removeEventHandler("onClientRender", getRootElement(), encendidoFunc) 
      end 
    end 
    dxDrawImage(screenX / 2 - kep1 / 2, screenY - 100 - kep2 / 2, kep1, kep2, "images/soundon.png") 
  end 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...