BIG audio problem

I’m having a lot of trouble with my audio. I want it to play after the game countdown, which I can do. The only problem is I don’t know how to make it to where I can push the restart button, have it take me to the loading scene, come back to the game, and after the countdown, play the audio from the beginning. I have tried rewinding, but that doesn’t work. Here is a little bit of my code:
[lua]–This is the countdown–
elseif p3 == true then
timer.cancel(startTmr)
local Go = display.newText(“GO!!!”, (_W/2-100)/3, _H/2-100, “MarkerFelt-Wide”, 200)
Go.xScale = .5; Go.yScale = .5; Go:setTextColor(0,255,0)
transition.to(Go, {time=4000, xScale = 5, yScale = 5, alpha = 0})
Scene:insert(Go)
ready = true

inGameMusicChannel = audio.play(gameMusic, {channel=2})
end

–Then I push restart and this function is called–

local function reset (e)
if ready == true then
audio.stop(inGameMusicChannel)
audio.rewind(inGameMusicChannel)

timer.cancel(countTmr)
timer.cancel(startTmr)
timer.cancel(ptsTmr)
timer.cancel(gameTmr)
timer.cancel(horTmr)
timer.cancel(vertTmr)
Runtime:removeEventListener(“enterFrame”, findVertPos)
Runtime:removeEventListener(“enterFrame”, findHorPos)
Runtime:removeEventListener(“enterFrame”, blueList)
Runtime:removeEventListener(“enterFrame”, redList)
Runtime:removeEventListener(“enterFrame”, greenList)
Runtime:removeEventListener(“enterFrame”, yellowList)
Runtime:removeEventListener(“enterFrame”, silverList)
Runtime:removeEventListener(“enterFrame”, orangeList)

director:changeScene (“easyTransition”)

end
end

local function fade (e)
if ready == true then
audio.rewind(inGameMusicChannel)
audio.fade({channel=0, time=1000, volume=0})
tmr = timer.performWithDelay(1000, reset)
transition.to(Scene, {time=1000, alpha = 0})

end
end

restart:addEventListener(“touch”, fade)[/lua]
Whenever the game loads, the music just starts from where it stopped. It doesn’t rewind. Anybody have a solution?

Much appreciated,
J.K. [import]uid: 66117 topic_id: 18583 reply_id: 318583[/import]

Some plug and play code displaying your problem would help. (Obviously people could just use their own audio to test.)

Is rewind working if you use it on its own, outside of this project? (Just to test.)

Peach :slight_smile: [import]uid: 52491 topic_id: 18583 reply_id: 71375[/import]

EDIT:
Here is a different junk main.lua file. It doesn’t work either…
[lua]local box = display.newRect(100,100,50,50)

local var = 1

gameMusic = audio.loadStream(“yourMusic.wav”)
inGameMusicChannel = audio.play(gameMusic, {channel=2})

local function push (e)
if e.phase == “began” then
if var == 1 then
print(var)
var = var+1
audio.stop(inGameMusicChannel)
elseif var ==2 then
print(var)
var = var+1
audio.rewind(inGameMusicChannel)
elseif var==3 then
print(var)
var = var+1
audio.resume(inGameMusicChannel)
end
end
end
box:addEventListener(“touch”, push)[/lua]
For some reason it doesn’t resume the music… [import]uid: 66117 topic_id: 18583 reply_id: 71379[/import]

After a bit of playing about I’ve got a potential work around for you.

It seems to me that paused music cannot be rewound, so it needs to be done on a short timer - around 1 second.

So what I did was muted the channel, rewound it and paused it a second later - this way it appears to pause right away but still rewinds.

Then in the resume I set the volume back up to the default for that channel.

Obviously test with your own sound file but it should work. (I left step 2 in although it does nothing now.)

[lua]local box = display.newRect(100,100,50,50)

local var = 1

gameMusic = audio.loadStream(“happy.mp3”)
inGameMusicChannel = audio.play(gameMusic, {channel=2})

function test ()
audio.pause(inGameMusicChannel)
end

local function push (e)
if e.phase == “began” then
if var == 1 then
print(var)
var = var+1
audio.rewind(inGameMusicChannel)
audio.setVolume( 0, { channel=2 } )
timer.performWithDelay(1000, test, 1)
elseif var ==2 then
print(var)
var = var+1
elseif var==3 then
print(var)
var = var+1
audio.setVolume( 1.0, { channel=2 } )
audio.resume(inGameMusicChannel)
end
end
end
box:addEventListener(“touch”, push)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 18583 reply_id: 71522[/import]

Thanks Peach. I tried it and it would play, stop, then play from the paused point for about a half second, then play from the beginning. After a bit of tweaking I think I found what was wrong. I had been rewinding the channel name, not the audio handle:
[lua]local box = display.newRect(100,100,50,50)

local var = 1

gameMusic = audio.loadStream(“inGameMusic.wav”)

function test ()
print “test activated”
audio.pause(inGameMusicChannel)
end

local function push (e)
if e.phase == “began” then
if var == 1 then
print(var)
var = var+1
inGameMusicChannel = audio.play(gameMusic, {channel=2})

elseif var ==2 then
print(var)
var = var+1
audio.stop(inGameMusicChannel)
audio.rewind(inGameMusicChannel) elseif var==3 then
print(var)
var = var+1
inGameMusicChannel = audio.play(gameMusic, {channel=2})
end
end
end
box:addEventListener(“touch”, push)[/lua]
But instead, I should have been using this:
[lua]local box = display.newRect(100,100,50,50)

local var = 1

gameMusic = audio.loadStream(“inGameMusic.wav”)

function test ()
print “test activated”
audio.pause(inGameMusicChannel)
end

local function push (e)
if e.phase == “began” then
if var == 1 then
print(var)
var = var+1
inGameMusicChannel = audio.play(gameMusic, {channel=2})

elseif var ==2 then
print(var)
var = var+1
audio.stop(inGameMusicChannel)
audio.rewind(gameMusic) elseif var==3 then
print(var)
var = var+1
inGameMusicChannel = audio.play(gameMusic, {channel=2})
end
end
end
box:addEventListener(“touch”, push)[/lua]
Although it works on the simulator, it doesn’t work on my iPod… It will play from the beginning, only to stop after about 1/2 second. [import]uid: 66117 topic_id: 18583 reply_id: 71719[/import]

  1. If you are not using build 701 on iOS devices running 5.0.x, you might have problems with audio. (Now infamous Apple/OpenAL regression bug.) Please update to the daily builds.

  2. You might check out this thread:
    http://developer.anscamobile.com/forum/2011/12/01/build-2011698-and-almixer-ios-5-testing

  3. I think rewinding when paused is supposed to work. But yes, rewinding the channel vs. handle may make a difference depending on loadStream vs. loadSound. This should be in the docs.

[import]uid: 7563 topic_id: 18583 reply_id: 71725[/import]

@ewig,

1.) I’m currently using version 2011.591 (2011.8.2). I don’t have access to the daily builds because I’m not yet a subscriber.

2.) I tried a few things suggested in that post, but to no avail.

[import]uid: 66117 topic_id: 18583 reply_id: 71731[/import]