如何在通知期间致电Xamarin IOS中的Avplayer,而不是最大持续时间通知声音



我需要播放3分钟长的音频文件。但是默认通知声音的播放不超过30秒。所以我的想法是打电话给avplayer会播放我所需的音频。但是我不知道该怎么称呼。谁能帮帮我吗。我会非常感谢。

我在这里附上我的通知方法。

public void AVPlayer()
{
    NSUrl songURL;
    if (!MusicOn) return;
    //Song url from your local Resource  
    songURL = new NSUrl("azan.wav");
    NSError err;
    player = new AVAudioPlayer(songURL, "Song", out err);
    player.Volume = MusicVolume;
    player.FinishedPlaying += delegate {
        // backgroundMusic.Dispose();  
        player = null;
    };
    //Background Music play  
    player.Play();
}
public void CreateRequest(JamatTime jamat)
{
    // Create action
    var actionID = "pause";
    var title = "PAUSE";
    var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.None);
    // Create category
    var categoryID = "message";
    var actions = new UNNotificationAction[] { action };
    var intentIDs = new string[] { };
    var categoryOptions = new UNNotificationCategoryOptions[] { };
    var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    // Register category
    var categories = new UNNotificationCategory[] { category };
    UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    // Rebuild notification
    var content = new UNMutableNotificationContent();
    content.Title = " Jamat Time alert";
    content.Badge = 1;
    content.CategoryIdentifier = "message";`enter code here`
    content.Sound = UNNotificationSound.GetSound("sample.wav");
    var times = new string[] { jamat.Asr, jamat.Dhuhr, jamat.Faijr, jamat.Ishaa, jamat.Jumah, jamat.Maghib };
    int id = 0;
    foreach (var time in times)
    {
        var ndate = DateTime.ParseExact(time, "h:mm tt", null);
        var date = new NSDateComponents()
        {
            Calendar = NSCalendar.CurrentCalendar,
            Hour = ndate.Hour,
            Minute = ndate.Minute,
            Second = 0
        };
        content.UserInfo = new NSDictionary<NSString, NSString>(
            new NSString[] {
                (NSString)"time1",
                (NSString)"time2"
            },
            new NSString[] {
                (NSString)DateTime.Now.ToString("h:mm tt"),
                (NSString)time
            });
        var trigger = UNCalendarNotificationTrigger.CreateTrigger(date, true);
        // ID of Notification to be updated
        var request = UNNotificationRequest.FromIdentifier(id++.ToString(), content, trigger);
        // Add to system to modify existing Notification
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err1) =>
        {
            if (err1 != null)
            {
                Console.WriteLine("Error: {0}", err1);
            }
            Console.WriteLine($"Success: {request}");
        });
    }
}

您不能播放音频文件而不是UNNotificationSound。当本地通知到来时,无法触发玩家的游戏方法。您只能使用上面发布的代码配置声音属性。文件应嵌入到捆绑资源中。

看来您知道不注明:https://developer.apple.com/documentation/usernotification/usernotifications/unnotificationaund?language = objc。但是我仍然想提醒您文件的格式和长度限制。

最后我解决了问题。当通知启动时,WillPresentNotification((方法命中率,我只会在那里称为Avplayer并完美工作。如果您想通过不注明播放声音,那就不可能了,因为这受到30秒的限制。但是问题仅在前景中起作用。

最新更新