Thread Tools Display Modes
01-31-08, 10:43 PM   #1
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Play a sound file when you're mounted?

Hey,

I want to make an addon that plays "On the road again" when you mount. Then stops when you're unmounted, I want it to work it all kinds of mounts.

Now, how would I go about doing this?
Could someone help me?

I've never made an addon before, I just think that sounds like an awesome idea.

Thanks!
  Reply With Quote
02-01-08, 12:23 AM   #2
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Really simple. Just write a simple addon to check "IsMounted()" and "not IsMounted()".

www.wowwiki.com
  Reply With Quote
02-01-08, 08:58 AM   #3
ProfOak
A Cyclonian
Join Date: Oct 2005
Posts: 47
Hi!

You can make it like this:

-- the file that will call your files:
MyAddon.toc
-- add the next code line
MyAddon.lua

-- the file that will contain the code:
MyAddon.lua

-- add the following code:

-- create your addon's name space
MyAddon = {}
-- create a simple frame
local frame = CreateFrame("Frame");

-- register event, might be another better to make this but this one was the nicest I've found
frame:RegisterEvent("PLAYER_AURAS_CHANGED");

-- set the trigger to catch the event and call your function
frame:SetScript("OnEvent", function(this, event, ...)
MyAddon[event](MyAddon, ...) end; );

-- your function
function MyAddon:PLAYER_AURAS_CHANGED()
if(IsMounted()) then
-- since this event triggers so many times, we better declare anything inside the case we want
local soundFile = "path";
-- path is where your music will be
-- Ex:Interface\\AddOns\\YourAddon\\YourMusic.mp3
PlayMusic(soundFile);
else
StopMusic();
end;
end;

I didnt test this, so no clue if is working. One drawback is that else clause, it will trigger the stopmusic() everytime.
There might be a way to get from chat msgs the right place to put the ismount and the else clause.

Anyway, I hope it helps.

Cheers

Last edited by ProfOak : 02-01-08 at 09:02 AM.
  Reply With Quote
02-01-08, 10:13 AM   #4
Shirik
Blasphemer!
Premium Member
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2007
Posts: 818
Originally Posted by ProfOak View Post
-- set the trigger to catch the event and call your function
frame:SetScript("OnEvent", function(this, event, ...)
MyAddon[event](MyAddon, ...) end; );
While there's nothing wrong with what you did, I strongly advise that you use "self" instead of "this" when making these types of functions. This fits the standard Lua syntax, and you'll get less confusion when others are reading your code (and may save yourself some confusion as well).
__________________
たしかにひとつのじだいがおわるのお
ぼくはこのめでみたよ
だけどつぎがじぶんおばんだってことわ
しりたくなかったんだ
It's my turn next.

Shakespeare liked regexes too!
/(bb|[^b]{2})/
  Reply With Quote
02-01-08, 10:48 AM   #5
ProfOak
A Cyclonian
Join Date: Oct 2005
Posts: 47
Thanks for the tip Shirik :P

Stonedpossum, I forgot to add some important info that you'll need in your toc file:

MyAddon.toc
## Interface: 20300
## Title: My Addon
## Notes: This is my first AddOn.
MyAddon.lua


The explanation:
## Interface: 20300 -- is were u type current game version
## Title: My Addon -- the name that ppl using ur addon will see in the addon options
## Notes: This is my first AddOn. -- info
-- the next session is where u put the files to be loaded, .lua and/or .xml

Now the explanation is complete ^^

If u into the addon development, check foruns here and http://www.wowwiki.com/

Cheers :P
  Reply With Quote
02-01-08, 01:30 PM   #6
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Thanks guys.

Now, one more question, can I just put this all in notepad and save it as the type of files needed, or do I need to download a program?
  Reply With Quote
02-01-08, 03:04 PM   #7
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Stonedpossum View Post
Thanks guys.

Now, one more question, can I just put this all in notepad and save it as the type of files needed, or do I need to download a program?
Notepad/wordpad will do what you want.
  Reply With Quote
02-01-08, 05:40 PM   #8
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Hey. Sorry for all the questions, I feel so dumb. Haha, but this is my very first time doing any sort of programing and making WoW addons, so anyway, I did this:

Code:
MyAddon = {OnTheRoad}
local frame = CreateFrame("Frame");
frame:RegisterEvent("PLAYER_AURAS_CHANGED");
frame:SetScript("OnEvent", function(this, event, ...)
OnTheRoad[event](OnTheRoad, ...) end; );
function MyAddon:PLAYER_AURAS_CHANGED()
if(IsMounted())
local soundFile = "Interface\\AddOns\\OnTheRoad\\music.mp3";
PlayMusic(music.mp3);
else
StopMusic();
end;
end;
It didn't work... haha... can someone tell me what I'm doing wrong. I bet it's something really easy :P

That's my Addon.lua file. The addon comes up on the list, but doesn't do anything.

Also, is it best to make an addon in Lua or XML?

Last edited by Stonedpossum : 02-01-08 at 05:49 PM.
  Reply With Quote
02-01-08, 05:55 PM   #9
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Stonedpossum View Post
Hey. Sorry for all the questions, I feel so dumb. Haha, but this is my very first time doing any sort of programing and making WoW addons, so anyway, I did this:

Code:
MyAddon = {OnTheRoad}
local frame = CreateFrame("Frame");
frame:RegisterEvent("PLAYER_AURAS_CHANGED");
frame:SetScript("OnEvent", function(this, event, ...)
OnTheRoad[event](OnTheRoad, ...) end; );
function MyAddon:PLAYER_AURAS_CHANGED()
if(IsMounted())
local soundFile = "Interface\\AddOns\\OnTheRoad\\music.mp3";
PlayMusic(music.mp3);
else
StopMusic();
end;
end;
It didn't work... haha... can someone tell me what I'm doing wrong. I bet it's something really easy :P
I would personally do

Addon.lua
Code:
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_AURAS_CHANGED");
f:SetScript("OnEvent", function()
   if IsMounted() then
      PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
   end
end)
Addon.toc
Code:
## Interface: 20300
## Title: My Addon
## Notes: This is my first AddOn.
Addon.lua
Place both of these in a folder named Addon.


replace "Interface\\AddOns\\OnTheRoad\\music.mp3" with the location of the mp3 you want to use.

Last edited by Slakah : 02-01-08 at 06:00 PM.
  Reply With Quote
02-01-08, 05:59 PM   #10
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Don't I want it to stop the music when you unmount?

And also, what's wrong with the where the music file is?

Because that's where it is...
  Reply With Quote
02-01-08, 06:24 PM   #11
erica647
A Cobalt Mageweaver
 
erica647's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 209
Stonedpossum... I love that nic! lol
__________________
Karadra
Level 80 Human Deathknight
Silvermoon/Nerfed Guild
  Reply With Quote
02-01-08, 06:28 PM   #12
Icecoldcoke
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 132
Originally Posted by Stonedpossum View Post
Don't I want it to stop the music when you unmount?

And also, what's wrong with the where the music file is?

Because that's where it is...

Code:
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_AURAS_CHANGED");
f:SetScript("OnEvent", function()
   if IsMounted() then
      PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
   else
      StopMusic()
   end
end)
  Reply With Quote
02-01-08, 11:20 PM   #13
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Hey, ok I think it worked. I logged on when i was mounted and it was playing, but when I unmounted and and remounted it didn't play. How can I make it play every time?

Addon.lua:
Code:
MyAddon = {OnTheRoad}
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED");
frame:SetScript("OnEvent", function()
   if IsMounted() then
      PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
   else
      StopMusic()
   end
end)
OnTheRoad.toc
Code:
# Interface: 20300
## Title: OnTheRoad
## Notes: Plays on the road when you're mounted. Pure awesome!
Addon.lua
UPDATE:
Ok, here's something weird. It doesn't work if you mount, but it plays the music when you run into a building and get unmounted... WTF?!? Help please...

Another UPDATE:
Now it's just playing when I log on mounted... I'm so confused!

Last edited by Stonedpossum : 02-02-08 at 01:17 AM.
  Reply With Quote
02-02-08, 05:54 AM   #14
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
MyAddon = {OnTheRoad}
Remove that it's not doing anything.

I'm not certain but maybe "PLAYER_AURAS_CHANGED" is called before IsMounted() will return true. If so try

Code:
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil) 
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
            else  
                StopMusic()
            end
        end
end)

Last edited by Slakah : 02-02-08 at 06:04 AM.
  Reply With Quote
02-02-08, 06:30 AM   #15
mulesh
A Chromatic Dragonspawn
 
mulesh's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 193
The value for IsMounted() is indeed delayed during PLAYER_AURAS_CHANGED. The event fires too fast. I came across this problem myself when creating the PlaySound addon. Using an OnUpdate handler is the best way to go.
__________________
"Don"t tase me bro!" ~ Andrew Meyer
  Reply With Quote
02-02-08, 12:24 PM   #16
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Gah! Ok, I have,

Code:
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil) 
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
            else  
                StopMusic()
            end
        end
end)
But it still doesn't even work.
  Reply With Quote
02-02-08, 01:04 PM   #17
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Stonedpossum View Post
Gah! Ok, I have,

Code:
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil) 
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
            else  
                StopMusic()
            end
        end
end)
But it still doesn't even work.

put
Code:
DEFAULT_CHAT_FRAME:AddMessage("Stopping music")
after StopMusic() to find out if its IsMounted() and
Code:
DEFAULT_CHAT_FRAME:AddMessage("Playing music")
after PlayMusic() then try mounting up. If you see Playing music printed then it means PlayMusic() is failing else its IsMounted try playing around with the value in
Code:
t >= 1
.

Is Music enabled, in the sound options? And does <Your addon> appear in the addon list?
  Reply With Quote
02-02-08, 01:19 PM   #18
Stonedpossum
A Murloc Raider
Join Date: Oct 2007
Posts: 8
Code:
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil) 
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3") DEFAULT_CHAT_FRAME:AddMessage("Playing music")
            else  
                StopMusic() DEFAULT_CHAT_FRAME:AddMessage("Stopping music")
            end
        end
end)
No message pops up, still no sound.

Yes, my sound is on.

Yes, it does show up on the list.

and you said something about changing the "t >= 1" what would I even change it to? I mean, what does that even mean...

Sorry for being so stupid when it comes to this programing stuff, never done it before.
  Reply With Quote
02-02-08, 03:59 PM   #19
mulesh
A Chromatic Dragonspawn
 
mulesh's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 193
Change it to this and see if it pops up any messages:
Code:
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil) 
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
                DEFAULT_CHAT_FRAME:AddMessage("Playing music")
            else  
                StopMusic()
                DEFAULT_CHAT_FRAME:AddMessage("Stopping music")
            end
        end
end)
To answer your question, the OnUpdate script is called by the game every time the screen refreshes (i.e. 30fps means 30 calls per second). The "elapsed" variable is a value for how much time has passed since the last OnUpdate call. Setting the timer to zero (local t = 0) and then we keep adding the value of "elapsed" to it (t = t + elapsed) until the timer equals 1 second or more (t >= 1). Once it equals 1 or more seconds we check if the player is mounted or not. By changing the t >= 1 line to something like t >= 2 would cause it to check for player mount every 2 seconds.

Not to put a stopper to your learning experience, but you could just use my PlaySound addon (shameless plug, whoohoo!!).

Good luck dood
__________________
"Don"t tase me bro!" ~ Andrew Meyer
  Reply With Quote
02-04-08, 08:51 AM   #20
ProfOak
A Cyclonian
Join Date: Oct 2005
Posts: 47
Hi.

First on my original post, I put a line with:

MyAddon = {}

and your using MyAddon = {OnTheRoad}, which is wrong.

MyAddon = {} -- is used to create an empty table. You shouldn't put anything inside curly bracets.

So, pls copy and paste the code as is and then test it.

I also found this events that you can try to test and see if they fit best:

CHAT_MSG_SPELL_PERIODIC_FRIENDLYPLAYER_BUFFS
Check arg1 for "mount" and/or "mounted" strings.

COMBAT_TEXT_UPDATE
Check arg1 for "AURA_START", "AURA_END", "AURA_START_HARMFUL",
"AURA_END_HARMFUL" strings.

UNIT_AURA("player")
Use this instead of "PLAYER_AURAS_CHANGED"


MyAddon:UNIT_AURA(arg1, ...)
if( IsMounted() and arg1 = "player" ) then
....


U need to do some work here, I'm @ work and cant test it
sssshhhhh dont tell my boss

--------------------------------------------------------------------------
I like MyAddon = {} for 2 reasons, readability and u can add more functions if u like by using MyAddon:NEW_FUNCTION(args), avoinding the polution of the global enviroment and possible confiltcs between addons fucntions.
But this is just my point of view, u should follow whatever u feel it fits best ur porposes.
--------------------------------------------------------------------------

Hope I could give u a help

Cheers
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Play a sound file when you're mounted?

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off