如何使用Xamarin iOS / Android播放简单的音频文件

问题描述 投票:0回答:1

我正在尝试与Xamarin一起学习和玩耍:)我想在某个时间结束时播放简单的声音,部分成功了,这在android和ios模拟器上都可以使用,但是当我尝试在iPhone上构建应用程序时,声音再现时就迷恋了!

我写的代码是我从here复制过来的!

所以我的代码是这样:

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文件的当前位置使我崩溃:enter image description here

我认为问题出在info.plist中(尽管我很可能错了,但我不知道如何解决它:(有人能帮我吗?谢谢

c# xamarin audio xamarin.forms xamarin.ios
1个回答
0
投票

我已经检查了此代码,它可以在我的网站上使用。调用播放方法如下:

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

其他代码与您在iOS和Android中的相同,它们都可以成功播放声音。

[您需要注意的是,应将本地声音文件添加到每个平台。每个平台都有其专用文件夹来放置auido文件。

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

enter image description here

并且在iOS中,您需要将其放入“资源”文件夹中,如下所示:

enter image description here

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

您首先应按照以下步骤将文件添加到此文件夹:

enter image description here

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

enter image description here

然后安装应用程序时,该文件将被添加到mobile。无论是模拟器还是物理设备。

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

© www.soinside.com 2019 - 2024. All rights reserved.