Giter Club home page Giter Club logo

react-native-audio-session's Introduction

react-native-audio-session

npm version npm downloads

A React Native module for handling category, category options and mode on the AVAudioSession sharedInstance on iOS.

Getting started

$ yarn add react-native-audio-session

and then

$ react-native link react-native-audio-session

Usage

import AudioSession from 'react-native-audio-session'

// Set AVAudioSession active
AudioSession.setActive(true)
.then(() => { console.log('Success!') })
.catch(error => { console.log('Error!') })

// Set AVAudioSession category
AudioSession.setCategory('Playback')
.then(() => { console.log('Success!') })
.catch(error => { console.log('Error!') })

// Set AVAudioSession category and options
AudioSession.setCategory('Playback', 'MixWithOthers')
.then(() => { console.log('Success!') })
.catch(error => { console.log('Error!') })

// Set AVAudioSession mode
AudioSession.setMode('VoiceChat')
.then(() => { console.log('Success!') })
.catch(error => { console.log('Error!') })

// Set AVAudioSession category, mode and options
AudioSession.setCategoryAndMode('Playback', 'VoiceChat', 'MixWithOthers')
.then(() => { console.log('Success!') })
.catch(error => { console.log('Error!') })

// Get current AVAudioSession category
AudioSession.currentCategory().then(category => {
  console.log(category)
})

// Get current AVAudioSession options
AudioSession.currentCategoryOptions().then(options => {
  console.log(options)
})

// Get current AVAudioSession mode
AudioSession.currentMode().then(mode => {
  console.log(mode)
})

API

Modules

AudioSession

Method Name Params Returns Notes
setActive Bool Promise<void> Set the current AVAudioSession as active (Reference).
setCategory AudioCategory, AudioCategoryOptions (nullable) Promise<void> Set the current AVAudioSession category (Reference).
setMode AudioMode Promise<void> Set the current AVAudioSession mode (Reference).
setCategoryAndMode AudioCategory, AudioMode, AudioCategoryOptions (nullable) Promise<void> Set the current AVAudioSession category and mode (Reference).
currentCategory - Promise<AudioCategory> Get the current AVAudioSession category (Reference).
currentOptions - Promise<AudioCategoryOptions> Get the current AVAudioSession options (Reference).
currentMode - Promise<AudioMode> Get the current AVAudioSession mode (Reference).

Params

AudioCategory

The AudioCategory param to all methods. Corresponds to AVAudioSessionCategory in iOS. Read more: Audio Session Category (developer.apple.com)

Param AVAudioSessionCategory
Ambient AVAudioSessionCategoryAmbient
SoloAmbient AVAudioSessionCategorySoloAmbient
Playback AVAudioSessionCategoryPlayback
Record AVAudioSessionCategoryRecord
PlayAndRecord AVAudioSessionCategoryPlayAndRecord
MultiRoute AVAudioSessionCategoryMultiRoute

AudioCategoryOptions

The AudioCategoryOptions param to all methods. Corresponds to AVAudioSessionCategoryOptions in iOS. Read more: AVAudioSessionCategoryOptions (developer.apple.com)

Param AVAudioSessionCategoryOptions
MixWithOthers AVAudioSessionCategoryOptionMixWithOthers
DuckOthers AVAudioSessionCategoryOptionDuckOthers
InterruptSpokenAudioAndMixWithOthers AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers
AllowBluetooth AVAudioSessionCategoryOptionAllowBluetooth
AllowBluetoothA2DP AVAudioSessionCategoryOptionAllowBluetoothA2DP
AllowAirPlay AVAudioSessionCategoryOptionAllowAirPlay
DefaultToSpeaker AVAudioSessionCategoryOptionDefaultToSpeaker

AudioMode

The AudioMode param to all methods. Corresponds to AVAudioSessionMode in iOS. Read more: Audio Session Mode (developer.apple.com)

Param AVAudioSessionMode
Default AVAudioSessionModeDefault
VoiceChat AVAudioSessionModeVoiceChat
VideoChat AVAudioSessionModeVideoChat
GameChat AVAudioSessionModeGameChat
VideoRecording AVAudioSessionModeVideoRecording
Measurement AVAudioSessionModeMeasurement
MoviePlayback AVAudioSessionModeMoviePlayback
SpokenAudio AVAudioSessionModeSpokenAudio

Contributing

If you find a bug or would like to request a new feature, just open an issue. You are also welcome to submit pull requests and contribute to the project.

License

The MIT License (MIT)

Copyright (c) 2018 Johan Kasperi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-native-audio-session's People

Contributors

dlindstrm avatar johankasperi avatar rpavlovs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-audio-session's Issues

Argument list too long: recursive header expansion failed

Screenshot 2020-08-28 at 6 10 14 PM

Argument list too long: recursive header expansion failed at /Users/iPhone/../../../../node_modules/react-native-audio-session/ios/../../../ios/Pods/React/node_modules/acorn-jsx/node_modules/acorn/src/loose.

Getting this error after linking.

Please help me to fix this issue.

"react-native-audio-session": "0.0.4",
"react-native": "^0.59.10",
"xcode": "11.3",

Change constants values to same as in ios reference

I'm trying to change the ducking behaviour without changing category, so I need to look it up and use right away.

const audioCategory = await ReactNativeAudioSession.currentCategory()
// => 'AVAudioSessionCategoryPlayback'

await ReactNativeAudioSession.setCategory(audioCategory, audioOptions)
// => throws 'Audio session could not be set' 

currentCategory() returns values in the format not supported by setCategory(). e.g 'AVAudioSessionCategoryPlayback' vs 'Playback'

I will just make a map of iOS categories to react-native-audio-session categories for now. But changing the constants maps to use same values as in iOS reference would have been more convenient.

Is that something worth making?

"Playback" category

Hello. I'm doing an iOS VoIP app with push-to-talk functionality, and it works fine in foreground.
But when it goes to background, I set audiosession category to Playback, from PlayAndRecord because I need to still play audio on device, and user has no possibility to record while app is in background. But the red recording indicator is still shown in the status bar while app is in background as if my app is recording. Is there another way to turn of microphone while app is in background?

Setting multiple options?

AVAudioSession allows you to set multiple options at the same time.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker |  AVAudioSessionCategoryOptionAllowBluetoothA2DP | AVAudioSessionCategoryOptionAllowBluetooth error:nil];

Maybe I'm being thick, but I cannot figure out how to do that with this implementation.

Setting Playback and DuckOthers does not work

import * as ReactNativeAudioSession from 'react-native-audio-session'

await ReactNativeAudioSession.default.setCategory(
  ReactNativeAudioSession.AudioCategories.Playback,    
  ReactNativeAudioSession.AudioOptions.DuckOthers
)

Errors out with Error Domain=NSOSStatusErrorDomain Code=-50 "(null)" which means parameters error according to this issue:
https://stackoverflow.com/questions/1861989/avaudioplayer-initialization-error-code-50

Any idea why this happens?

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.