xamarin中的音频暂停.c#



所以我正在使用xamarin构建一个应用程序。需要播放和控制音频的形式。我目前有我的音频文件在安卓和iOS上播放,但我不能得到其他控制工作,如。pause()和。stop ()

播放音频在两个例子中都有效。

我提前感谢你!

下面是每个平台的代码

ANDROID:

using System;
using Xamarin.Forms;
using AudioPlayEx.Droid;
using AudioToolbox;
using Android.Media;
using Android.Content.Res;
using Android.OS;
using Android;
[assembly: Dependency(typeof(AudioService))]
namespace AudioPlayEx.Droid
{
    public class AudioService : IAudio
    {
        public AudioService()
        {
        }
        MediaPlayer player;
        public void PlayAudioFile(string fileName)
        {
            var player = new MediaPlayer();
            var fd = Android.App.Application.Context.Assets.OpenFd(fileName);
            player.Prepared += (s, e) =>
            {
                player.Start();
            };
            player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            player.Prepare();
        }
        protected MediaPlayer playme;
        public void PauseAudioFile(String fileName)
        {
            var player = new MediaPlayer();
            var fd = Android.App.Application.Context.Assets.OpenFd(fileName);
            player.Prepared += (s, e) =>
            {
                player.Pause();
            };
            player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
            player.Prepare();
        }
    }
}
iOS:

using System;
using Xamarin.Forms;
using AudioPlayEx;
using AudioPlayEx.iOS;
using System.IO;
using Foundation;
using AVFoundation;
[assembly: Dependency(typeof(AudioService))]
namespace AudioPlayEx.iOS
{
    public class AudioService : IAudio
    {
        public AudioService()
        {
        }
        public void PlayAudioFile(string fileName)
        {
            string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
            var url = NSUrl.FromString(sFilePath);
            var _player = AVAudioPlayer.FromUrl(url);
            _player.FinishedPlaying += (object sender, AVStatusEventArgs e) => {
                _player = null;
            };
            _player.Play();
        }

        public void PauseAudioFile(string fileName)
        {
            string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
            var url = NSUrl.FromString(sFilePath);
            var _player = AVAudioPlayer.FromUrl(url);
            _player.Pause();
        }
    }
}

我认为你需要将AVAudioPlayer的实例保存到一个变量并在该对象上调用pause。现在你正在创建一个新的AVAudioPlayer并在它上调用pause,它什么都不做。我添加了iOS代码来说明我的意思。Android也应该类似。

iOS:

public class AudioService : IAudio
{
    AVAudioPlayer _player;
    public void PlayAudioFile(string fileName)
    {
        string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
        var url = NSUrl.FromString(sFilePath);
        _player = AVAudioPlayer.FromUrl(url);
        _player.FinishedPlaying += (object sender, AVStatusEventArgs e) => {
            _player = null;
        };
        _player.Play();
    }

    public void PauseAudioFile(string fileName)
    {
        _player.Pause();
    }
}

这个话题很老了,但我还是这么做了:

MediaPlayer player;
        public void PlayAudioFile(string fileName, string ONOF)
        {
           
            if (ONOF == "OFF")
            {
                player.Stop();
            }
            if (ONOF == "ON")
                    {
                 player = new MediaPlayer();
                var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);
                player.Prepared += (s, e) =>
                {
                    
                        player.Start(); 
                   
                };
                player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
                player.Prepare();
           }
        }

相关内容

  • 没有找到相关文章

最新更新