Sunday, 22 November 2020

Trying to set an audio asset as a ringtone on Android

I'm using the following package to set a an audio asset as a ringtone ringtone_set

This is the error I keep encountering when calling the setRingtone method.

W/System.err(15204): java.lang.NullPointerException: uri
W/System.err(15204):    at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128)
W/System.err(15204):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1229)
W/System.err(15204):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:1009)
W/System.err(15204):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:985)
W/System.err(15204):    at acr.rt.ringtone_set.RingtoneSetPlugin.setThings(RingtoneSetPlugin.java:109)
W/System.err(15204):    at acr.rt.ringtone_set.RingtoneSetPlugin.onMethodCall(RingtoneSetPlugin.java:151)
W/System.err(15204):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
W/System.err(15204):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
W/System.err(15204):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
W/System.err(15204):    at android.os.MessageQueue.nativePollOnce(Native Method)
W/System.err(15204):    at android.os.MessageQueue.next(MessageQueue.java:325)
W/System.err(15204):    at android.os.Looper.loop(Looper.java:151)
W/System.err(15204):    at android.app.ActivityThread.main(ActivityThread.java:6724)
W/System.err(15204):    at java.lang.reflect.Method.invoke(Native Method)
W/System.err(15204):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err(15204):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

I am using another package to request permissions to access the users storage. If the library can't find the asset then it will log a message to the console. I can play the sound just fine as well, its just when I try to set the ringtone.

Here is the AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.anisoundboard2">
    <uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

Here is how I am loading the assets in pubspec.yaml

flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/images/
    - assets/sounds/

Here is the function that is called when the user clicks Set as Ringtone

  Future setRingtone(String soundFileName) async {
    try {
      await requestStoragePermission();
      await RingtoneSet.setRingtone('assets/sounds/$soundFileName');
    } catch(e) {
      print(e);
    }
  }

And this is the function I use to request permission

  Future requestStoragePermission() async {
    final status = await Permission.storage.request();
    print(status);
  }

This worked on my Android Emulator, but I am currently testing on an actual device. The LG Phoenix 4



from Trying to set an audio asset as a ringtone on Android

No comments:

Post a Comment