首页 > 解决方案 > 如何使用 Xamarin iOS/Android 播放简单的音频文件

问题描述

我正在尝试学习和玩一点 Xamarin :) 我想在某个时间结束时播放一个简单的声音,部分我成功了,这适用于 android 和 ios 模拟器但是当我尝试构建应用程序时在我的 iPhone 上这个美眉在声音再现的那一刻!

我写的代码是从这里复制的!

所以我的代码是这样的:

iAudio.cs:

using System;
namespace StopWatch
{
    public interface IAudio
    {
        void PlayAudioFile(string fileName);
    }
}

Android 中的 AudioService.cs:

using System;
using Xamarin.Forms;
using StopWatch.Droid;
using Android.Media;
using Android.Content.Res;

[assembly: Dependency(typeof(AudioService))]
namespace StopWatch.Droid
{
    public class AudioService : IAudio
    {
        public AudioService()
        { }
        public void PlayAudioFile(string fileName)
        {
            var 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();
        }
    }
}

iOS 中的 AudioService.cs:

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

这是 mi MainPage.xaml.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace StopWatch
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        Stopwatch stopwatch;
        public MainPage()
        {
            InitializeComponent();
            stopwatch = new Stopwatch();
            lblStopWatch.Text = "00:00:00";
            lblStopWatchAllert.Text = "";
        }

        private void btnStartClicked(object sender, EventArgs e)
        {
            stopwatch.Start();
            Device.StartTimer(TimeSpan.FromMilliseconds(10), () =>
            {
                lblStopWatch.Text = stopwatch.Elapsed.ToString().Substring(0, 8);
                controll(lblStopWatch.Text);
                return true;

            });
        }

        private void btnStopClicked(object sender, EventArgs e)
        {
            stopwatch.Stop();
        }

        private void btnResetClicked(object sender, EventArgs e)
        {
            stopwatch.Reset();
        }

        private void controll(String time)
        {
            if (string.Compare(time, "00:00:03:0000") > 0)
            {

                stopwatch.Reset();
                lblStopWatchAllert.Text = "time is over!";

                DependencyService.Get<IAudio>().PlayAudioFile("Alert.mp3");

            }
        }

    }
}

代码在 iOS AudioService.cs 文件的这一点使我崩溃: 在此处输入图像描述

我认为问题在于 info.plist (虽然我很可能是错的)但我不知道如何解决它:( 有人可以帮助我吗?谢谢

标签: c#xamarinaudioxamarin.formsxamarin.ios

解决方案


我已经检查了这段代码,它在我的网站上工作。调用 play 方法如下:

DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");

其他代码和你在iOS和Android中的代码一样,都可以成功播放声音。

这里需要注意的是,本地声音文件应该添加到每个平台。每个平台都有自己的专用文件夹来放置 auido 文件。

Android中,您需要将其放在Assets文件夹中,如下所示:

在此处输入图像描述

iOS中,您需要将其放在 Resources 文件夹中,如下所示:

在此处输入图像描述

===========================更新======================= =============

您应该首先将文件添加到此文件夹,如下所示:

在此处输入图像描述

然后将现有文件添加到此文件夹:

在此处输入图像描述

然后在安装应用程序时,这个文件将被添加到移动端。不管是模拟器还是物理设备。

注意

不将文件从其他路径复制到 Xamarin.iOS 项目,这不能确保将文件添加到项目中。


推荐阅读