gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:25:49 24 4
gpt4 key购买 nike

我正在尝试与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(尽管我很可能错了),但我不知道如何解决它:(
有人能帮我吗?谢谢

最佳答案

我已经检查了此代码,它在我的网站上有效。调用play方法如下:

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

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

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

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

enter image description here

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

enter image description here

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

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

enter image description here

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

enter image description here

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

注意:

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

关于c# - 如何使用Xamarin iOS/Android播放简单的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59635973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com