【问题标题】:Firemonkey custom Notification sound not workingFiremonkey自定义通知声音不起作用
【发布时间】:2015-09-29 10:33:38
【问题描述】:

我希望我只是在某处遗漏了一些东西,我希望在触发本地通知时播放自定义通知声音。

在一个新表单上,我放下了 2 个按钮,btnnotification 和 btnmediaplayer

procedure TForm1.btnnotificationClick(Sender: TObject);
var
    MyNotification : TNotification;
begin
    MyNotification := NotificationCenter1.CreateNotification;
    try
        MyNotification.EnableSound := True;
        MyNotification.SoundName := TPath.Combine(TPath.GetDocumentsPath, 'Siren.mp3');
        MyNotification.Name := 'mynote';
        MyNotification.AlertBody := 'Test';
        MyNotification.FireDate := Now;
        ShowMessage(MyNotification.SoundName); //<-- Points towards a valid filepath
        NotificationCenter1.ScheduleNotification(MyNotification);
    finally
        //MyNotification.DisposeOf;
    end;
end;

我将 Siren 文件添加到部署中的内部资产中,通知触发但没有声音,然后我添加了另一个按钮,这次我只是使用 TMediaplayer

procedure TForm1.btnmediaplayerClick(Sender: TObject);
begin
    MediaPlayer1.FileName := TPath.Combine(TPath.GetDocumentsPath, 'Siren.mp3');
    MediaPlayer1.Play;
    ShowMessage(MediaPlayer1.FileName);//<--Points towards the same path as the notification
end;

使用 TMediaplayer 没有问题,这意味着文件确实存在,我现在已经在 2 种不同的设备上进行了尝试:Xperia Z2 和三星 Note 2 都运行 Andriod V4.4.2, 并且两台设备的通知音量都设置为最大,但仍然没有声音。

更新 1

  • 我在运行 4.1.2 的三星 Tab3 上尝试过,这意味着它不是 4.4.2 的问题
  • 我尝试手动复制 com.andriod.project 文件夹中的文件

但通知仍然没有声音。

已解决:您需要将声音存储在外部并使用 GetSharedDocumentsPath,将其存储在内部并使用 GetDocumentsPath 不起作用

【问题讨论】:

  • 看来,你自己解决了这个问题。请发布您的解决方案以及一些详细的解释作为答案,以便您的问题被识别为已回答的问题。

标签: delphi firemonkey


【解决方案1】:

使用property ReceiveLocalNotification。 你必须在这个属性中使用你的声音

procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
  ANotification: TNotification);

begin

   MediaPlayer1.FileName:=TPath.Combine(TPath.GetDocumentsPath, 'Siren.mp3');

end;

【讨论】:

  • 我已尝试修复您答案中的格式。我不熟悉 firemonkey,所以不知道语法 - 请查看我所做的并在我创建错误的任何地方更正。
  • 这不是正确的答案,因为它只会在您点击通知后开始播放 Siren.mp3,如果您的应用程序已经在前台,这将打开您的应用程序并播放文件什么都不做,或者如果你没有点击通知,它也什么也不做,看看我的问题底部的已解决声明
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多