如何在静音模式下播放声音?

huangapple go评论109阅读模式
英文:

How to play a sound in Silent Mode?

问题

我正在使用Flutter(目前仅支持Android)编写一个闹钟应用程序。

我成功使用通知播放了闹钟声音。但仅在手机未处于静音模式时才起作用。

是否有办法使用Flutter构建这样的应用程序?无论手机的状态如何,播放声音都是该应用程序的核心功能。如果不可能实现这一点,我应该停止开发该应用程序。

已尝试使用AwesomeNotifications插件播放的声音(使用最高优先级的Alarm类型)以及FlutterRingtonePlayer播放的声音(通过通知触发)。

Flutter 3.3.5 • 稳定通道

用于测试的物理设备正在运行Android 13

编辑:由于该应用程序是一个闹钟,将使用一个隔离线程播放声音。

英文:

I'm coding an alarm app using Flutter (Android only, for now).

I managed to play the alarm using a notification. But it only works when the phone isn't in Silent Mode.

Is there a way to build such an app using Flutter? Playing the sound no matter the state of the phone is the core functionality of the app. If this isn't possible, I should stop the development of this app.

Already tried using a sound played by AwesomeNotifications plugin (using Alarm Type with max priority) and a sound played by FlutterRingtonePlayer (triggered by a notification).

Flutter 3.3.5 • channel stable

Physical device used to test is running Android 13

Edit: as the app is an alarm, an isolate will play the sound.

答案1

得分: 1

1- 在你的 pubspec.yaml 文件中将 sound_mode 添加为依赖项:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  sound_mode: ^2.0.2

2- 在 AndroidManifest.xml 中添加以下权限,以使应用程序出现在“勿扰访问”列表中:

<manifest ...>
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>

    <application ...>
        ...
    </application>
</manifest>

要更改设备的声音模式:

import 'package:sound_mode/utils/ringer_mode_statuses.dart';

// 获取设备当前的声音模式:
String ringerStatus = await SoundMode.ringerModeStatus;
print(ringerStatus);

// 将设备的声音模式从静音更改为正常:
if (ringerStatus == 'silent') {
  try {
    await SoundMode.setSoundMode(RingerModeStatus.normal);
  } on PlatformException {
    print('请启用所需的权限');
  }
}

// 在这里编写你的代码:播放声音

注意:以上只是翻译的代码部分,不包括其他内容。

英文:

1-Add sound_mode as a dependency in your pubspec.yaml file

dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter

sound_mode: ^2.0.2

2-Add the following permission to AndroidManifest.xml for the app to appear in the 'Do Not Disturb Access' list :

&lt;manifest ... &gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NOTIFICATION_POLICY&quot;/&gt;
    
    &lt;application ... &gt;
    ...
&lt;/manifest&gt;

To change the device's sound mode:

import &#39;package:sound_mode/utils/ringer_mode_statuses.dart&#39;;

//To get the device&#39;s current sound mode:
String ringerStatus = await SoundMode.ringerModeStatus;
print(ringerStatus);

// To change the device&#39;s sound mode: silent to normal.
if(ringerStatus == &#39;silent&#39;){
   try {
      await SoundMode.setSoundMode(RingerModeStatus.normal);
     } on PlatformException {
     print(&#39;Please enable permissions required&#39;);
    }
}

// your code here : play a sound

答案2

得分: 0

MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound);
mp.start();

这段代码应在启动闹钟时调用(应在本地代码中调用)。

英文:
MediaPlayer mp= MediaPlayer.create(contexto, R.raw.your_sound);
mp.start();

You should call this when you start your alarm(you should call this in native code)

答案3

得分: 0

希望这个可以解决你的问题。

英文:

Once try with this because this package is providing functionality to work in silent mode also.

assets_audio_player: ^3.0.6

https://pub.dev/packages/assets_audio_player

https://i.stack.imgur.com/qbjFB.png

I hope this things are solve your issue.

huangapple
  • 本文由 发表于 2023年1月9日 07:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定