Git Product home page Git Product logo

microphonepowersample's Introduction

Microphone Power Sample

Add permissions in build.settings file

Android

settings =
{
    android =
    {
        usesPermissions =
        {
            "android.permission.RECORD_AUDIO",
        },
    },
}
In android 6+ need to get permission on runtime.

iOS

settings =
{
    iphone =
    {
        plist =
        {
            NSMicrophoneUsageDescription = "This app would like to access the microphone.",
        },
    },
}

Main.lua

local microphone = require "plugin.microphonePower"
local json = require("json")

local function isValueInTable( haystack, needle )
	assert( type(haystack) == "table", "isValueInTable() : First parameter must be a table." )
	assert( needle ~= nil, "isValueInTable() : Second parameter must be not be nil." )

	for key, value in pairs( haystack ) do
		if ( value == needle ) then
			return true
		end
	end
	return false
end

local function canUseMicrophone()
	local grantedPermissions = system.getInfo("grantedAppPermissions")
	if ( grantedPermissions ) then
		if ( not isValueInTable( grantedPermissions, "Microphone" ) ) then
			return false
		end
	end
	return true 
end

local function onMicrophoneAccessGiven()
	timer.performWithDelay( 100, function()
		microphone.getPower()
	end, -1 )
end

-- Called when the user has granted or denied the requested permissions.
local function permissionsListener( event )
	-- Print out granted/denied permissions.
	print( "permissionsListener( " .. json.prettify( event or {} ) .. " )" )

	if canUseMicrophone() then
		print("permissionsListener : microphone access given")
		-- microphone access is given 
		-- now get power of microphone
		onMicrophoneAccessGiven()
	else
		-- ask for permissions again to access microphone
		print("permissionsListener : microphone access not given")
	end
end

if canUseMicrophone() then
 	-- microphone access is given 
	-- now get power of microphone
	onMicrophoneAccessGiven()
else
	-- Make the actual request from the user.
	native.showPopup( "requestAppPermission", {
		appPermission = "Microphone",
		urgency = "Critical",
		rationaleTitle = "Microphone Permissions Required!",
		rationaleDescription = "This app wants to access your microphone.",
		listener = permissionsListener,
	} )
end

local function callBackListener( event )
	print( "event in microphonePower callback : ", json.encode(event) )
end
-- init microphonePower plugin
microphone.init(callBackListener)

microphonepowersample's People

Contributors

usmandotmughal avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.