Heeeelp with playback in Android.

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
--------------------------------------------------------------------------------------------------------------||
local widget = require “widget”
--------------------------------------------------------------------------------------------------------------||
– For Recording
local dataFileName = “RecordLetterA”;
if “simulator” == system.getInfo(“environment”) then
    dataFileName = dataFileName … “.aif”
else
    local platformName = system.getInfo(“platformName”)
    if “iPhone OS” == platformName then
        dataFileName = dataFileName … “.aif”
    elseif “Android” == platformName then
        dataFileName = dataFileName … “.3gp”
        isAndroid = true;
    else
        print(“Unknown OS” … platformName)
    end
end
print(dataFileName)
--------------------------------------------------------------------------------------------------------------||
local r
local recButton
local fSoundPlaying = false;
local fSoundPaused = false;
local isAndroid = false;
--------------------------------------------------------------------------------------------------------------||
local function updateStatus()
    local statusText = " ";
    local statusText2 = " ";
        if r then
            local recString = " ";
            local fRecording = r:isRecording()
                if fRecording then
                    recString = “Recording”;
                    recButton:setLabel(“Stop Recording”)
                elseif fSoundPlaying then
                    recString = “Playing”;
                    recButton:setLabel(“Pause Playback”)
                elseif fSoundPaused then
                    recString = “Paused”;
                    recButton:setLabel(“Resume Playback”)
                else
                    recString = “Idle”;
                    recButton:setLabel(“Record”)
                end
            statusText = recString;
            statusText2 = "Sampling Rate: " … tostring (r:getSampleRate() … “Hz”)
        end
end
--------------------------------------------------------------------------------------------------------------||
local function onCompleteSound(event)
    fSoundPlaying = false;
    fSoundPaused = false;
    
    if not isAndroid then
        audio.dispose(event.handle)
    end
    updateStatus()
end
--------------------------------------------------------------------------------------------------------------||
local function recButtonPress(event)
    if fSoundPlaying then
        fSoundPlaying = false;
        fSoundPaused = true;
        if isAndroid then
            media.pauseSound()
        else
            audio.pause()
        end
    elseif fSoundPaused then
        fSoundPlaying = true;
        fSoundPaused = false;
        if isAndroid then
            media.playSound()
        else
            audio.resume()
        end
    else
        if r then
            if r:isRecording() then
                r:stopRecording()
                local filePath = system.pathForFile(dataFileName, system.DocumentsDirectory)
                local file = io.open(filePath, “r”)
                if file then
                    io.close(file)
                    fSoundPlaying = true;
                    fSoundPaused = false;
                    if isAndroid then
                        media.playSound(dataFileName, system.DocumentsDirectory, onCompleteSound)
                    else
                        playBackSoundHandle = audio.loadStream(dataFileName, system.DocumentsDirectory)
                        audio.play(playBackSoundHandle, { onComplete = onCompleteSound } )
                    end
                end
            else
                fSoundPlaying = false;
                fSoundPaused = false;
                r:startRecording()
            end
        end
    end
    updateStatus()
end            
--------------------------------------------------------------------------------------------------------------||
local rateUpButtonPress = function(event)
    local theRates = {8000, 11025, 16000, 22050, 44100}
        if not r:isRecording() and not fSoundPlaying and not fSoundPaused then
            local f = r:getSampleRate()
            local i, v = next (theRates, nil)
            while i do
                if v <= f then
                    i, v = next (theRates, i)
                else
                    i = nil;
                end
            end
            if v then
                r:setSampleRate(v)
            else
                r:setSampleRate(theRates[1])
            end
        end
        updateStatus()
    end
--------------------------------------------------------------------------------------------------------------||
local rateDownButtonPress = function(event)
    local theRates = {44100, 22050, 16000, 11025, 8000}
        if not r:isRecording() and fSoundPlaying and not fSoundPaused then
            local f = r:getSampleRate()
            local i, v = next(theRates, nil)
            while i do
                if v >= f then
                    i, v = next(theRates, nil)
                else
                    i = nil;
                end
            end
            if v then
                r:getSampleRate()
            else
                r:setSampleRate(theRates[1])
            end
        end
        updateStatus()
end
--------------------------------------------------------------------------------------------------------------||
function scene:createScene(event)
    local screenGroup = self.view
    --Background
    local Background = display.newImage(“TriviaBoard.jpg”)    
    screenGroup:insert(Background)                                                            
    --Airplane Image
    local Airplane = display.newImageRect(“TriviaAAA.png”, 590, 1000)
    Airplane.x = 300;
    Airplane.y = 500;
    screenGroup:insert(Airplane)
    --Back
    Back = widget.newButton{
        defaultFile = “BackButton.png”,
        overFile = “ClickedBack.png”,
        width = 300, height = 120,
        onRelease = ToGoBack
    }
    Back.x = 170;
    Back.y = 765;
    screenGroup:insert(Back)
    
    --Next
    Next = widget.newButton{
        defaultFile = “NextButton.png”,
        overFile = “ClickedNext.png”,
        width = 300, height = 120,
        onRelease = ToTraceB
    }
    Next.x = 430;
    Next.y = 765;
    screenGroup:insert(Next)    
    --SpeakerIcon
    Speaker = widget.newButton{
        defaultFile = “SpeakerButton.png”,
        overFile = “ClickedSpeaker.png”,
        width = 130, height = 130,
        onRelease = PlayA
    }
    Speaker.x = 180;
    Speaker.y = 890;
    screenGroup:insert(Speaker)
    --Recording Icon
    recButton = widget.newButton{
        defaultFile = “RecordBut.png”,
        overFile = “ClickedSpeaker.png”,
        width = 160, height = 160,
        onRelease = recButtonPress,
        label = “Record”,
        fontSize = “20”,
        emboss = true;
    }
    recButton.x = 420;
    recButton.y = 890;
    screenGroup:insert(recButton)    
    
    local filePath = system.pathForFile(dataFileName, system.DocumentsDirectory)
    r = media.newRecording(filePath)
    updateStatus()    
end    
--------------------------------------------------------------------------------------------------------------||
    --To go to B
    function ToTraceB(event)        
        storyboard.gotoScene(“TracingLetterB”, “fade”, 500)            
    end
    --To go back
    function ToGoBack(event)        
        storyboard.gotoScene(“TracingLetterA”, “fade”, 500)            
    end
    --Play Sound
    function PlayA(event)
        media.playSound(“A.wav”)
        recButton:setEnabled(true)
    end
    --To Record
    function RecordA()        
    
    end
--------------------------------------------------------------------------------------------------------------||            
function scene:enterScene(event)
    local screenGroup = self.view    
    recButton:setEnabled(false)
end

function scene:exitScene(event)
    local screenGroup = self.view

end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene
 

Hi Jonathan.  Do you have a question?  Is there a particular issue you need help with?  It’s great that you posted some code, but unless we know more specifically what problem you’re having, you’re likely not going to get any helpful responses.

Rob

I already can record audio Sir Rob, but i don’t know what’s wrong why it is not playing back. this code is from the sample apps of Corona SDK. i just put it inside my application. thank you!

Hi Jonathan.  Do you have a question?  Is there a particular issue you need help with?  It’s great that you posted some code, but unless we know more specifically what problem you’re having, you’re likely not going to get any helpful responses.

Rob

I already can record audio Sir Rob, but i don’t know what’s wrong why it is not playing back. this code is from the sample apps of Corona SDK. i just put it inside my application. thank you!