Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ group 'com.example.flutter_ringtone_manager'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.7.10'
ext.kotlin_version = "1.8.22"
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath("com.android.tools.build:gradle:8.1.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
}
}


allprojects {
repositories {
google()
Expand All @@ -29,15 +30,15 @@ android {
namespace 'com.example.flutter_ringtone_manager'
}

compileSdk 34
compileSdk = 35

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = '1.8'
jvmTarget = JavaVersion.VERSION_11
}

sourceSets {
Expand All @@ -46,22 +47,22 @@ android {
}

defaultConfig {
minSdkVersion 19
minSdk = 21
}

dependencies {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.mockito:mockito-core:5.0.0")
}

testOptions {
unitTests.all {
useJUnitPlatform()

testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class FlutterRingtoneManagerPlugin : FlutterPlugin, MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"playRingtone" -> {
playDefaultSoundByID(result, TYPE_RINGTONE)
val isLoop = call.argument<Boolean>("isLoop") ?: false
playDefaultSoundByID(result, TYPE_RINGTONE, isLoop)
}

"playAlarm" -> {
playDefaultSoundByID(result, TYPE_ALARM)
val isLoop = call.argument<Boolean>("isLoop") ?: false
playDefaultSoundByID(result, TYPE_ALARM, isLoop)
}

"playNotification" -> {
Expand All @@ -48,8 +50,9 @@ class FlutterRingtoneManagerPlugin : FlutterPlugin, MethodCallHandler {

"playAudioAsset" -> {
val uri = call.argument<String>("uri")
val isLoop = call.argument<Boolean>("isLoop") ?: false
if (uri != null) {
playUri(Uri.parse(uri!!), result)
playUri(Uri.parse(uri!!), result, isLoop)
} else {
result.error("PLAY_FAILED", "assetPath can not be null", null)
}
Expand All @@ -61,6 +64,26 @@ class FlutterRingtoneManagerPlugin : FlutterPlugin, MethodCallHandler {
ringtone?.stop()
}
}
"getUriOfSystemSoundByID" -> {
val soundId = call.argument<Int>("soundID") ?: 1007
when (soundId) {
1007 -> {
val smsSentToneUri: Uri = RingtoneManager.getActualDefaultRingtoneUri(context, TYPE_NOTIFICATION)
result.success(smsSentToneUri.toString())
}
1005 -> {
val alarmToneUri: Uri = RingtoneManager.getActualDefaultRingtoneUri(context, TYPE_ALARM)
result.success(alarmToneUri.toString())
}
1000 -> {
val alarmToneUri: Uri = RingtoneManager.getActualDefaultRingtoneUri(context, TYPE_RINGTONE)
result.success(alarmToneUri.toString())
}
else -> {
result.error("URI_GET_FAILED", "Invalid system sound ID for Android", null)
}
}
}

else -> {
result.notImplemented()
Expand All @@ -72,19 +95,21 @@ class FlutterRingtoneManagerPlugin : FlutterPlugin, MethodCallHandler {
channel.setMethodCallHandler(null)
}

private fun playDefaultSoundByID(result: MethodChannel.Result, id: Int) {
private fun playDefaultSoundByID(result: MethodChannel.Result, id: Int, isLoop: Boolean = false) {
playUri(
RingtoneManager.getActualDefaultRingtoneUri(context, id),
result
result,
isLoop
)
}

private fun playUri(uri: Uri, result: MethodChannel.Result) {
private fun playUri(uri: Uri, result: MethodChannel.Result, isLoop: Boolean) {
try {
if (ringtone != null && ringtone!!.isPlaying) {
ringtone!!.stop()
}
ringtone = RingtoneManager.getRingtone(context, uri)
ringtone?.isLooping = isLoop
ringtone?.play()
} catch (e: Exception) {
e.printStackTrace()
Expand Down
1 change: 1 addition & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx4G
android.useAndroidX=true
android.enableJetifier=true
org.gradle.java.home=C\:\\Users\\bharadpr\\.jdks\\jbr-17.0.12
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
4 changes: 2 additions & 2 deletions example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "org.jetbrains.kotlin.android" version "1.7.10" apply false
id "com.android.application" version "8.1.0" apply false
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

include ":app"
11 changes: 9 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
final _flutterRingtoneManager = FlutterRingtoneManager();
String? alarmUri = "get alarm Uri";

@override
void initState() {
Expand All @@ -44,7 +45,13 @@ class _MyAppState extends State<MyApp> {
},
child: const Text("Play Ringtone")),
OutlinedButton(
onPressed: () => {_flutterRingtoneManager.playAlarm()},
onPressed: () async {
alarmUri = await _flutterRingtoneManager.getUriOfSystemSoundByID(SystemSoundID.alarm);
setState(() {});
},
child: Text(alarmUri!)),
OutlinedButton(
onPressed: () => {_flutterRingtoneManager.playAlarm(playInLoop: true)},
child: const Text('Play Alarm')),
OutlinedButton(
onPressed: () {
Expand All @@ -54,7 +61,7 @@ class _MyAppState extends State<MyApp> {
ElevatedButton(
onPressed: () {
_flutterRingtoneManager
.playAudioAsset("audio/test.mp3");
.playAudioAsset("audio/test.mp3", playInLoop: false);
},
child: const Text("Play custom Asset")),
if (Platform.isIOS)
Expand Down
Loading