forked from kurenai7968/volume_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c9dabcf
Showing
93 changed files
with
2,815 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
|
||
.packages | ||
.pub/ | ||
.idea/ | ||
|
||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 5d36f2e7f5387b6c751449258ade8e4e6edf99be | ||
channel: beta | ||
|
||
project_type: plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## 1.0.0 | ||
|
||
* Android: Support get/set/volume | ||
* iOS: Support get/set/volume |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: Add your license here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# volume_controller | ||
|
||
A Flutter plugin for iOS and Android get/set/listen system volume. | ||
|
||
## Getting Started | ||
|
||
dependencies: volume_controller: ^1.0.0 | ||
|
||
## Functions | ||
|
||
- getVolume: get current volume from system | ||
> await VolumeController.getVolume() | ||
- setVolume: input a double number to set system volume. The range is [0, 1] | ||
> await VolumeController.setVolume(double volume) | ||
- volumeListener: listen system volume | ||
> VolumeController.volumeListener.listen((volume) { // TODO }); | ||
## Usage | ||
|
||
```dart | ||
class _MyAppState extends State<MyApp> { | ||
TextEditingController _textController = TextEditingController(); | ||
double _volume = 0; | ||
double _getVolume = 0; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
VolumeController.volumeListener.listen((volume) { | ||
setState(() => _volume = volume); | ||
}); | ||
} | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Volume Plugin example app'), | ||
), | ||
body: Column( | ||
children: [ | ||
Text('Current volume: $_volume'), | ||
Row( | ||
children: [ | ||
Flexible(child: TextField(controller: _textController)), | ||
RaisedButton( | ||
color: Colors.blue, | ||
onPressed: () { | ||
if (_textController.text != '') { | ||
VolumeController.setVolume( | ||
double.parse(_textController.text)); | ||
setState(() {}); | ||
} | ||
}, | ||
child: Text('Set Volume By Value'), | ||
), | ||
], | ||
), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Text('Volume is: $_getVolume'), | ||
RaisedButton( | ||
color: Colors.blue, | ||
onPressed: () async { | ||
_getVolume = await VolumeController.getVolume(); | ||
setState(() {}); | ||
}, | ||
child: Text('Get Volume'), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
group 'com.kurenai7968.volume_controller' | ||
version '1.0-SNAPSHOT' | ||
|
||
buildscript { | ||
ext.kotlin_version = '1.3.50' | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:4.1.0' | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
} | ||
} | ||
|
||
rootProject.allprojects { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion 30 | ||
|
||
sourceSets { | ||
main.java.srcDirs += 'src/main/kotlin' | ||
} | ||
defaultConfig { | ||
minSdkVersion 16 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
org.gradle.jvmargs=-Xmx1536M | ||
android.useAndroidX=true | ||
android.enableJetifier=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'volume_controller' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.kurenai7968.volume_controller"> | ||
</manifest> |
50 changes: 50 additions & 0 deletions
50
android/src/main/kotlin/com/kurenai7968/volume_controller/VolumeControllerPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.kurenai7968.volume_controller | ||
|
||
import android.content.Context | ||
import androidx.annotation.NonNull | ||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.plugin.common.EventChannel | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
import io.flutter.plugin.common.MethodChannel.Result | ||
|
||
class VolumeControllerPlugin: FlutterPlugin, MethodCallHandler { | ||
private val CHANNEL = "com.kurenai7968.volume_controller." | ||
private lateinit var context:Context | ||
private lateinit var volumeObserver: VolumeObserver | ||
private lateinit var methodChannel:MethodChannel | ||
private lateinit var volumeListenerEventChannel: EventChannel | ||
private lateinit var volumeListenerStreamHandler: VolumeListener | ||
|
||
|
||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
context = flutterPluginBinding.applicationContext | ||
volumeObserver = VolumeObserver(context) | ||
|
||
volumeListenerEventChannel = EventChannel(flutterPluginBinding.binaryMessenger, CHANNEL + "volume_listener_event") | ||
volumeListenerStreamHandler = VolumeListener(context) | ||
volumeListenerEventChannel.setStreamHandler(volumeListenerStreamHandler) | ||
|
||
methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, CHANNEL + "method") | ||
methodChannel.setMethodCallHandler(this) | ||
} | ||
|
||
|
||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
when (call.method) { | ||
"setVolume" -> { | ||
var volume:Double? = call.argument("volume") | ||
volumeObserver.setVolumeByPercentage(volume!!) | ||
} | ||
"getVolume" -> result.success(volumeObserver.getVolume()) | ||
} | ||
} | ||
|
||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
methodChannel.setMethodCallHandler(null) | ||
volumeListenerEventChannel.setStreamHandler(null) | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
android/src/main/kotlin/com/kurenai7968/volume_controller/VolumeObserve.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.kurenai7968.volume_controller | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Context.AUDIO_SERVICE | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.media.AudioManager | ||
import io.flutter.plugin.common.EventChannel | ||
import kotlin.math.round | ||
|
||
|
||
class VolumeObserver(private val context:Context){ | ||
private var audioManager: AudioManager = context.getSystemService(AUDIO_SERVICE) as AudioManager | ||
|
||
fun setVolumeByPercentage(volume:Double) { | ||
var volumePercentage:Double = volume | ||
var _volume:Int = 0 | ||
if (volume > 1) { | ||
volumePercentage = 1.0 | ||
} | ||
if (volume < 0) { | ||
volumePercentage = 0.0 | ||
} | ||
var maxVolume:Int = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) | ||
_volume = (round(volumePercentage * maxVolume)).toInt() | ||
|
||
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, _volume, 0) | ||
} | ||
|
||
fun getVolume():Double { | ||
var currentVolume:Int = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) | ||
var maxVolume:Int = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) | ||
return round((currentVolume / maxVolume.toDouble()) * 10000) / 10000 | ||
} | ||
} | ||
|
||
|
||
class VolumeListener(private val context: Context): EventChannel.StreamHandler { | ||
private val VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION" | ||
private lateinit var volumeBroadcastReceiver:VolumeBroadcastReceiver | ||
private lateinit var audioManager: AudioManager | ||
private var eventSink:EventChannel.EventSink? = null | ||
|
||
|
||
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { | ||
eventSink = events | ||
audioManager = context.getSystemService(AUDIO_SERVICE) as AudioManager | ||
volumeBroadcastReceiver = VolumeBroadcastReceiver(eventSink) | ||
registerReceiver() | ||
eventSink?.success(volume()) | ||
} | ||
|
||
override fun onCancel(arguments: Any?) { | ||
context.unregisterReceiver(volumeBroadcastReceiver) | ||
eventSink = null | ||
} | ||
|
||
private fun registerReceiver() { | ||
val filter = IntentFilter(VOLUME_CHANGED_ACTION) | ||
context.registerReceiver(volumeBroadcastReceiver, filter) | ||
} | ||
|
||
private fun volume():Double { | ||
var currentVolume:Int = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) | ||
var maxVolume:Int = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) | ||
return round((currentVolume / maxVolume.toDouble()) * 10000) / 10000 | ||
} | ||
} | ||
|
||
class VolumeBroadcastReceiver(private val events: EventChannel.EventSink?): BroadcastReceiver() { | ||
private lateinit var audioManager: AudioManager | ||
private var volumePercentage: Double = 0.0 | ||
private var currentVolume:Int = 0 | ||
private var maxVolume:Int = 0 | ||
override fun onReceive(context: Context, intent: Intent?) { | ||
audioManager= context!!.getSystemService(AUDIO_SERVICE) as AudioManager; | ||
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) | ||
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) | ||
volumePercentage = round((currentVolume / maxVolume.toDouble()) * 10000) / 10000 | ||
events?.success(volumePercentage) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
**/doc/api/ | ||
**/ios/Flutter/.last_build_id | ||
.dart_tool/ | ||
.flutter-plugins | ||
.flutter-plugins-dependencies | ||
.packages | ||
.pub-cache/ | ||
.pub/ | ||
/build/ | ||
|
||
# Web related | ||
lib/generated_plugin_registrant.dart | ||
|
||
# Symbolication related | ||
app.*.symbols | ||
|
||
# Obfuscation related | ||
app.*.map.json | ||
|
||
# Android Studio will place build artifacts here | ||
/android/app/debug | ||
/android/app/profile | ||
/android/app/release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 5d36f2e7f5387b6c751449258ade8e4e6edf99be | ||
channel: beta | ||
|
||
project_type: app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# volume_controller_example | ||
|
||
Demonstrates how to use the volume_controller plugin. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Flutter application. | ||
|
||
A few resources to get you started if this is your first Flutter project: | ||
|
||
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) | ||
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
gradle-wrapper.jar | ||
/.gradle | ||
/captures/ | ||
/gradlew | ||
/gradlew.bat | ||
/local.properties | ||
GeneratedPluginRegistrant.java | ||
|
||
# Remember to never publicly share your keystore. | ||
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app | ||
key.properties |
Oops, something went wrong.