当 cordova-plugin-local-notification 尝试播放 mp3 文件时,Monaca Android 应用程序崩溃

Posted

技术标签:

【中文标题】当 cordova-plugin-local-notification 尝试播放 mp3 文件时,Monaca Android 应用程序崩溃【英文标题】:Monaca Android App crashes when cordova-plugin-local-notification tries to play a mp3 file 【发布时间】:2021-01-29 11:22:29 【问题描述】:

我是 monaca/cordova 的新手,我正在尝试使用 React 框架和这个插件获得带有声音的背景通知:https://github.com/katzer/cordova-plugin-local-notifications。我正在 android 10 设备中构建应用程序。当我尝试没有声音的通知时,它可以正常工作,但是在添加声音时,它会崩溃。这是错误的日志:

    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
    at de.appplant.cordova.plugin.notification.util.AssetUtil.getUriFromFile(AssetUtil.java:357)
    at de.appplant.cordova.plugin.notification.util.AssetUtil.getUriFromAsset(AssetUtil.java:147)
    at de.appplant.cordova.plugin.notification.util.AssetUtil.parse(AssetUtil.java:90)
    at de.appplant.cordova.plugin.notification.Options.getSound(Options.java:351)
    at de.appplant.cordova.plugin.notification.Builder.build(Builder.java:126)
    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.buildNotification(TriggerReceiver.java:130)
    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:60)
    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:265)
    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:217)
    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:102)
    at de.appplant.cordova.plugin.localnotification.LocalNotification.schedule(LocalNotification.java:271)
    at de.appplant.cordova.plugin.localnotification.LocalNotification.access$400(LocalNotification.java:62)
    at de.appplant.cordova.plugin.localnotification.LocalNotification$1.run(LocalNotification.java:146)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)

这是我用来获取通知的功能(如果不添加声音,可以正常工作)。

 const showNotification = () => 
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() 
      console.log(cordova.plugins.notification.local.schedule);
    
   cordova.plugins.notification.local.schedule(
      id: 1,
      title: "Alarm Time",
      text: String(value),
      trigger:  at: value ,
      sound: "file://assets/songs/samplesong.mp3" // <-- this is where the error occurs. 
    );
  ;

*(value 是用户选择的 Date())。

这是我的config.xml 文件:

<widget xmlns="http://www.w3.org/ns/widgets" id="com.example.helloworld" version="1.0.0">
  <name>Onsen UI v2 React Minimum</name>
  <description>Made with Monaca (http://monaca.mobi)</description>
  <author/>
  <access origin="*"/>
  <content src="index.html"/>
  <preference name="loglevel" value="DEBUG"/>
  <preference name="AndroidLaunchMode" value="singleTop"/>
  <preference name="BackgroundColor" value="0xffffffff"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="ErrorUrl" value=""/>
  <preference name="Fullscreen" value="false"/>
  <preference name="KeepRunning" value="true"/>
  <preference name="monaca:WebViewEngine" value="default"/>
  <preference name="SplashScreen" value="screen"/>
  <preference name="SplashScreenDelay" value="1000"/>
  <preference name="Orientation" value="default"/>
  <preference name="monaca:DisableCookie" value="false"/>
  <preference name="monaca:AndroidIsPackageNameSeparate" value="false"/>
  <preference name="AllowInlineMediaPlayback" value="false"/>
  <preference name="AutoHideSplashScreen" value="true"/>
  <preference name="BackupWebStorage" value="cloud"/>
  <preference name="EnableViewportScale" value="false"/>
  <preference name="FadeSplashScreen" value="true"/>
  <preference name="FadeSplashScreenDuration" value="250"/>
  <preference name="KeyboardDisplayRequiresUserAction" value="true"/>
  <preference name="MediaPlaybackRequiresUserAction" value="false"/>
  <preference name="ShowSplashScreenSpinner" value="false"/>
  <preference name="SuppressesIncrementalRendering" value="false"/>
  <preference name="TopActivityIndicator" value="gray"/>
  <preference name="GapBetweenPages" value="0"/>
  <preference name="PageLength" value="0"/>
  <preference name="PaginationBreakingMode" value="page"/>
  <preference name="PaginationMode" value="unpaginated"/>
  <preference name="ios-XCBuildConfiguration-TARGETED_DEVICE_FAMILY" value="1,2"/>
  <feature name="App">
    <param name="android-package" value="org.apache.cordova.App"/>
  </feature>
  <feature name="LocalStorage">
    <param name="ios-package" value="CDVLocalStorage"/>
  </feature>
  <platform name="ios">
    <preference name="orientation" value="all"/>
    <splash src="/res/ios/screen/Default@2x~universal~anyany.png"/>
    <icon src="/res/ios/icon/icon.png"  />
    <icon src="/res/ios/icon/icon@2x.png"  />
    <icon src="/res/ios/icon/icon-72.png"  />
    <icon src="/res/ios/icon/icon-72@2x.png"  />
    <icon src="/res/ios/icon/icon-60.png"  />
    <icon src="/res/ios/icon/icon-60@2x.png"  />
    <icon src="/res/ios/icon/icon-60@3x.png"  />
    <icon src="/res/ios/icon/icon-76.png"  />
    <icon src="/res/ios/icon/icon-76@2x.png"  />
    <icon src="/res/ios/icon/icon-40.png"  />
    <icon src="/res/ios/icon/icon-40@2x.png"  />
    <icon src="/res/ios/icon/icon-50.png"  />
    <icon src="/res/ios/icon/icon-50@2x.png"  />
    <icon src="/res/ios/icon/icon-small.png"  />
    <icon src="/res/ios/icon/icon-small@2x.png"  />
    <icon src="/res/ios/icon/icon-small@3x.png"  />
    <icon src="/res/ios/icon/icon-83.5@2x~ipad.png"  />
    <icon src="/res/ios/icon/icon-1024.png"  />
    <splash src="/res/ios/screen/Default@2x~universal~anyany.png"/>
    <config-file platform="ios" parent="CFBundleLocalizations" target="*-Info.plist">
      <array>
        <string>en</string>
      </array>
    </config-file>
  </platform>
  <platform name="android">
    <icon src="/res/android/icon/ldpi.png" density="ldpi"/>
    <icon src="/res/android/icon/mdpi.png" density="mdpi"/>
    <icon src="/res/android/icon/hdpi.png" density="hdpi"/>
    <icon src="/res/android/icon/xhdpi.png" density="xhdpi"/>
    <icon src="/res/android/icon/xxhdpi.png" density="xxhdpi"/>
    <icon src="/res/android/icon/xxxhdpi.png" density="xxxhdpi"/>
    <splash src="/res/android/screen/splash-port-ldpi.9.png" density="port-ldpi"/>
    <splash src="/res/android/screen/splash-port-mdpi.9.png" density="port-mdpi"/>
    <splash src="/res/android/screen/splash-port-hdpi.9.png" density="port-hdpi"/>
    <splash src="/res/android/screen/splash-port-xhdpi.9.png" density="port-xhdpi"/>
    <splash src="/res/android/screen/splash-port-xxhdpi.9.png" density="port-xxhdpi"/>
    <splash src="/res/android/screen/splash-port-xxxhdpi.9.png" density="port-xxxhdpi"/>
    <splash src="/res/android/screen/splash-mdpi.png" density="mdpi"/>
  </platform>
  <platform name="electron">
    <icon src="/res/electron/icon/icon_electron_512.png"  />
    <splash src="/res/electron/screen/electron_splash_image.png"  />
    <preference name="SplashScreenWidth" value="620"/>
    <preference name="SplashScreenHeight" value="300"/>
    <preference name="ShowSplashScreen" value="false"/>
  </platform>
  <preference name="WindowsStorePublisherName" value="My Name"/>
  <preference name="WindowsStoreIdentityName" value="b88890db-d285-d39c-671d-89a187a00094"/>
</widget>

【问题讨论】:

【参考方案1】:

尝试用一个“/”写路径:

sound: "file:/assets/songs/samplesong.mp3"

【讨论】:

以上是关于当 cordova-plugin-local-notification 尝试播放 mp3 文件时,Monaca Android 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

更新:为啥当用户是管理员时关系有效,但当普通用户时不会?

当位于顶级栏目显示下级栏目,当位于二级栏目显示同级栏目,当位于三级目录,显示上级栏目

当应用程序在后台时扫描外围设备(例如,当屏幕锁定时)

psp一骑当千交叉冲击金手指

当组长有感

专访黄勇:Java在未来的很长一段时间仍是主流(把老板当情人,把同事当小孩,把客户当病人)