[SOLVED] Can't remove the page and director returns "Director ERROR: Module page_12 must return a display.newGroup()."

Been trying to get this page close and return to another page (external lua), yet anything I did I just couldn’t get it to work…

Everything else works great… except for the close button (closeBtn)

Below is my code, any help would be appreciated, thank you all… :slight_smile:

[code]module(…, package.seeall)
function new()
local numPages = 12
–local SoundtrackGroup = display.newGroup()
–local disposeAudios
–local disposeTweens

local curPage = 12

–local drawScreen = function()
local bgAudio
local infoBar
local progressBar
local progress
local imageMarker
local cdCover
local songText
local byText
local artistText
local buttonBar
local playBtn
local stopBtn
local prevBtn
local nextBtn
local current
local total
local itunesBtn
local closeBtn

–######## AUDIO PLAYER ########

– [Songs]

– URL, Name, Artist, Art

local song1Info = {“audio/razorsout.mp3”, “Razors Out”, “Mike Shinoda & Joe Trapanese”, “images/audioplayer/s1.png”}
–local song2Info = {‘song2.m4a’, ‘Kriptonite’, ‘3 Doors Down’, ‘s2.png’}
–local songsInfo = {song1Info, song2Info}
local songsInfo = {song1Info}

local song1 = audio.loadStream(song1Info[1])
–local song2 = audio.loadStream(song2Info[1])
–local songs = {song1, song2}
local songs = {song1}

– Variables

local currentSong = 1
local playing
local timerSource
local min = 0
local sec = 0

– Functions

local Soundtrack = {}
local buildGUI = {}
local playCurrentSong = {}
local stopSong = {}
local nextSong = {}
local prevSong = {}
local updateInfo = {}
local updateProgress = {}
local buySong ={}
local closePlayer = {}

– Main Function

function Soundtrack()
buildGUI()
end

– Build GUI
function buildGUI()
bgAudio = display.newImage( playDir… “audio_bg.jpg”)
infoBar = display.newImage( playDir… “infoBar.png”, 74, 96)
imageMarker = display.newImage( playDir… “imgMarker.png”, 91, 114)
cdCover = display.newImage( playDir… “s1.png”, 91, 114)
itunesBtn = display.newImage( playDir… “itunesbutton.png”, 220, 500)
topHeader = display.newImage( imgDir… “topwall.png”, 0, 0)
closeBtn = display.newImage( imgDir… “butcloseoff.png”, 480, 8)

songText = display.newText(“Razors Out”, 295, 176, native.systemFontBold, 22)
songText:setTextColor(246, 237, 240)
byText = display.newText(“By”, 295, 200, native.systemFont, 16)
byText:setTextColor(191, 182, 183)
artistText = display.newText(“Mike Shinoda & Joe Trapanese”, 325, 200, native.systemFontBold, 16)
artistText:setTextColor(244, 204, 106)

buttonBar = display.newImage( playDir… “buttonBar.png”, 74, 359)
playBtn = display.newImage( playDir… “playBtn.png”, 91, 375)
stopBtn = display.newImage( playDir… “stopBtn.png”, 91, 375)
stopBtn.isVisible = false
prevBtn = display.newImage( playDir… “prevBtn.png”, 151, 375)
nextBtn = display.newImage( playDir… “nextBtn.png”, 209, 375)

progressBar = display.newImage( playDir… “progressBar.png”, 274, 386)
progress = display.newRoundedRect(0, 0, 100, 21, 3)
progress:setFillColor(244, 204, 106)
progress:setReferencePoint(display.TopLeftReferencePoint)
progress.x = 274
progress.y = 387
progress.isVisible = false

current = display.newText(“00:00”, 280, 389, native.systemFont, 12)
current:setTextColor(246, 237, 240)
total = display.newText("/00:00", 316, 389, native.systemFont, 12)
total:setTextColor(191, 182, 183)

playBtn:addEventListener(“tap”, playCurrentSong)
stopBtn:addEventListener(“tap”, stopSong)
nextBtn:addEventListener(“tap”, nextSong)
prevBtn:addEventListener(“tap”, prevSong)
itunesBtn:addEventListener(“tap”, buySong)
closeBtn:addEventListener(“tap”, closePlayer)
end

function buySong:tap(e)
audio.stop()
local webView = native.newWebView( 0, 0, 640, 960, webListener )
–webView:request( “http://itunes.apple.com/us/album/raid-redemption-original-motion/id511644889” )
webView:request( “http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewCollaboration?ids=41334108-322290360&s=143441” )
end

function closePlayer:tap(e)
audio.stop()
director:changeScene( “page_6”, “moveFromRight” )
end

function playCurrentSong:tap(e)
audio.rewind(songs[currentSong])
sec = 0
min = 0
audio.play(songs[currentSong])
playing = true
updateInfo()
timerSource = timer.performWithDelay(1000, updateProgress, 0)

playBtn.isVisible = false
stopBtn.isVisible = true
end

function stopSong:tap(e)
audio.stop()
buildGui.isVisible = false
–stopBtn.isVisible = false
–playing = false
end

function nextSong:tap(e)
audio.stop()
currentSong = currentSong + 1

if(currentSong > #songs) then
currentSong = 1
end

if(playing) then
audio.rewind(songs[currentSong])
sec = 0
min = 0
audio.play(songs[currentSong])
end

updateInfo()
end

function prevSong:tap(e)
audio.stop()
currentSong = currentSong - 1

if(currentSong < 1) then
currentSong = #songs
end

if(playing) then
audio.rewind(songs[currentSong])
sec = 0
min = 0
audio.play(songs[currentSong])
end

updateInfo()
end

function updateInfo()
songText.text = tostring(currentSong) … ". " … songsInfo[currentSong][2]
songText:setReferencePoint(display.TopLeftReferencePoint)
songText.x = 295
artistText.text = songsInfo[currentSong][3]
artistText:setReferencePoint(display.TopLeftReferencePoint)
artistText.x = 325

display.remove(cdCover)
cdCover = nil
local cdCover = display.newImage(songsInfo[currentSong][4], 91, 114)

local totalMin = math.floor(audio.getDuration(songs[currentSong]) / 1000 / 60)–
local totalSec = math.floor(audio.getDuration(songs[currentSong]) / 1000 % 60)–

total.text = “/” … totalMin … “:” … totalSec
total:setReferencePoint(display.topLeftReferencePoint)
total.x = 332
end

function updateProgress:timer(e)
sec = sec + 1

if(sec == 60) then
sec = 0
min = min + 1
end

local secS = tostring(sec)

if(#secS < 2) then
sec = ‘0’ … sec
end

current.text = min … ‘:’ … sec
current:setReferencePoint(display.topLeftReferencePoint)
current.x = 295

– Progress Bar

local position = min … sec
position = position * 1000

local duration = audio.getDuration(songs[currentSong])
local percent = (position / duration)
print(position, duration, percent)

progress.isVisible = true
progress.xScale = percent
end
Soundtrack()
end
–##### END AUDIO PLAYER ####
-----------------------------[/code] [import]uid: 68581 topic_id: 24327 reply_id: 324327[/import]

gfunk, when working with director every scene you make must return a display.newGroup()
Looks like your code above is just missing a couple lines…

Right after line 4 above put this in…

local localGroup = display.newGroup()  

Then right before the “end” on line 247 above put this in…

return localGroup  

And finally, remember when you running your buildGUI() function to add ALL the display objects you create into this localGroup. Example:

localGroup:insert(bgAudio)  

Etc, do this for any display objects you create in the scene.
Hope that helps,

Croisened
[import]uid: 48203 topic_id: 24327 reply_id: 98261[/import]

Croisened…

Thank you so much, implemented your recommendations and it works like a charm… Thank you buddy.

This is my first director based app, learning by day… :slight_smile: [import]uid: 68581 topic_id: 24327 reply_id: 98337[/import]

Awesome glad to see it making progress!

One other tip I would recommend is that you are working with lots of audio, timers, etc.

You need to be a good steward of such items and nil them out, dispose, etc. so that you drop all the unused memory you can when moving scenes.

Best way in director to do this (assuming you are using 1.4) is to include the following function (in your example above I would put this on line 245) before the call to Soundtrack()

 --Clean up local variables, etc...  
 clean = function()  
 print("Cleaning")  
 end  

Director will automagically call this as you switch scenes so this is the spot for all your good housekeeping, so you should do things like cancel any running timers, remove runtime listeners, dispose of your loaded audio, etc.

Just run this in the terminal and see the print statement fire when you switch scenes and it will make sense.
Looks to be a cool player - keep it up!

–Croisened

[import]uid: 48203 topic_id: 24327 reply_id: 98341[/import]