Skip to content

TakuSemba/AudioThief

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a4ca579 Β· Mar 3, 2021

History

25 Commits
Feb 27, 2021
Feb 16, 2021
Oct 20, 2018
Feb 16, 2021
Feb 14, 2021
Oct 18, 2018
Oct 18, 2018
Nov 10, 2019
Feb 16, 2021
Feb 16, 2021
Nov 10, 2019
Oct 18, 2018
Oct 18, 2018
Mar 3, 2021
Oct 18, 2018

Repository files navigation

AudioThief

Gradle

//app/build.gradle
dependencies {
    implementation 'com.github.takusemba:audiothief:x.x.x'
}

Features

Platform Download License API

Since multiple apps can play audio to the sample ouptut stream simultaneously, it is requred to handle AudioFocus correctly.

However you do not want to call or set an alerm just to see if your impletemtation is working as you expected.

AudioThief gain AudioFocus from your app and release it back.


Usage

1. Add AudioThief dependency

dependencies {
    implementation 'com.github.takusemba:audiothief:x.x.x'
}

2. Edit your manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <!--this permission is required if your app targets Android 9.0 (API level 28)-->
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

  <application>
  <!--...-->
    <service
       android:name="com.takusemba.audiothief.AudioFocusGainService"
       android:exported="true"
       />
  </application>
       
</manifest>

3. Gain and Release AudioFocus

You can start a foreground service that gains AudioFocus via adb command, or simply start the srevice inside your app.

adb shell am startservice --ei AUDIO_REQUEST_KEY [audioRequestKey] your.package.name/com.takusemba.audiothief.AudioFocusGainService

It holds AudioFocus until the foreground service stops.

adb shell am stopservice --ei AUDIO_REQUEST_KEY [audioRequestKey] your.package.name/com.takusemba.audiothief.AudioFocusGainService

AudioRequestKey

AudioRequestKey defines how you gain AudioFocus. That would be Int of 1, 2, 3, or 4.

AudioRequestKey FocusGain Usage Content Type
1 AUDIOFOCUS_GAIN USAGE_MEDIA CONTENT_TYPE_MOVIE
2 AUDIOFOCUS_GAIN_TRANSIENT USAGE_ALARM CONTENT_TYPE_SONIFICATION
3 AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK USAGE_NOTIFICATION CONTENT_TYPE_SONIFICATION
4 AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE USAGE_ALARM CONTENT_TYPE_SONIFICATION

Sample

Clone this repo and check out the app module.

Author

Licence

Copyright 2017 Taku Semba.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.