npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-audio-focus

v0.1.5

Published

A React Native library to handle audio focus changes on both Android and iOS. This module allows React Native applications to respond to audio focus changes such as when another app starts playing audio or when a phone call is received. It helps in managi

Downloads

16

Readme

react-native-audio-focus

A React Native library to handle audio focus changes on both Android and iOS. This module allows React Native applications to respond to audio focus changes such as when another app starts playing audio or when a phone call is received. It helps in managing audio playback effectively by pausing or lowering the volume of the audio when necessary.

Features

  • Detects when audio focus is gained or lost.
  • Handles audio interruptions and allows resuming audio playback.
  • Simple API to request and abandon audio focus.
  • Supports both Android and iOS platforms.
  • Written in TypeScript for type safety.

Events

    - AUDIOFOCUS_GAIN
    - AUDIOFOCUS_LOSS
    - AUDIOFOCUS_LOSS_TRANSIENT //android
    - AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK //android

Installation

npm install react-native-audio-focus

or

yarn add react-native-audio-focus

Linking

For React Native 0.60 and above, the library is automatically linked using autolinking. For older versions, you need to manually link the library:

react-native link react-native-audio-focus

Android Setup

  1. Add the Native Module: In your application's android/app/src/main/java/com/yourappname/MainApplication.java, register the AudioFocusPackage:

    import com.yourprojectname.AudioFocusPackage; // Add this import
    
    public class MainApplication extends Application implements ReactApplication {
    
    @Override
    protected List<ReactPackage> getPackages() {
        @SuppressWarnings("UnnecessaryLocalVariable")
        List<ReactPackage> packages = new PackageList(this).getPackages();
        packages.add(new AudioFocusPackage()); // Add this line
        return packages;
    }
    
    // other methods...
    }
    
  2. Permissions: Add the following permissions to your main/AndroidManifest.xml:

    <manifest>
     ...
     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
     ...
    </manifest>

iOS Setup

  1. Swift Bridging Header: Ensure your React Native project can use Swift. If you haven't already, create a bridging header:

    • Open your Xcode project.
    • Create a new Swift file (File > New > File > Swift File) and name it Dummy.swift (you can name it anything, actually).
    • Xcode will prompt you to create a bridging header. Accept the prompt.
  2. Add the Audio Focus Module: Ensure that AudioFocusModule.swift is correctly added to your project.

  3. Modify Header File: Add below line in **Your_Project-Bridging-Header.h:

      #import <React/RCTBridgeModule.h>
      #import <React/RCTEventEmitter.h>
  4. Register the Module: No need to explicitly register the module in AppDelegate.m, as it's done automatically.

Usage

  • Import the library into your React Native project:
import AudioFocusModule from 'react-native-audio-focus';
  • Request Audio Focus
AudioFocusModule.requestAudioFocus();
  • Abandoning Audio Focus
AudioFocusModule.abandonAudioFocus();
  • Listening to audio focus Changes
const unsubscribe = AudioFocusModule.addAudioFocusListener((event) => {
  if (event === 'AUDIOFOCUS_GAIN') {
    console.log('Audio focus gained');
    // Resume playback or increase volume
  } else if (event === 'AUDIOFOCUS_LOSS') {
    console.log('Audio focus lost');
    // Pause playback or lower volume
  }
});

// To remove the listener when it's no longer needed
unsubscribe();

Example

import React, { useEffect } from 'react';
import { View, Text, Button } from 'react-native';
import AudioFocusModule from 'react-native-audio-focus';

const App = () => {
  useEffect(() => {
    const unsubscribe = AudioFocusModule.addAudioFocusListener((event) => {
      if (event === 'AUDIOFOCUS_GAIN') {
        console.log('Audio focus gained');
        // Resume playback or increase volume
      } else if (event === 'AUDIOFOCUS_LOSS') {
        console.log('Audio focus lost');
        // Pause playback or lower volume
      }
    });

    return () => {
      unsubscribe();
    };
  }, []);

  return (
    <View>
      <Text>Audio Focus Module Example</Text>
      <Button title="Request Audio Focus" onPress={() => AudioFocusModule.requestAudioFocus()} />
      <Button title="Abandon Audio Focus" onPress={() => AudioFocusModule.abandonAudioFocus()} />
    </View>
  );
};

export default App;

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or suggestions. See the contributing guide to learn how to contribute to the repository and the development workflow.

License

This library is licensed under the MIT License.


Made with create-react-native-library