c# - 如何使用 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 (虽然我很可能是错的)但我不知道如何解决它:( 有人可以帮助我吗?谢谢
解决方案
我已经检查了这段代码,它在我的网站上工作。调用 play 方法如下:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
其他代码和你在iOS和Android中的代码一样,都可以成功播放声音。
这里需要注意的是,本地声音文件应该添加到每个平台。每个平台都有自己的专用文件夹来放置 auido 文件。
在Android中,您需要将其放在Assets
文件夹中,如下所示:
在iOS中,您需要将其放在 Resources 文件夹中,如下所示:
===========================更新======================= =============
您应该首先将文件添加到此文件夹,如下所示:
然后将现有文件添加到此文件夹:
然后在安装应用程序时,这个文件将被添加到移动端。不管是模拟器还是物理设备。
注意:
不将文件从其他路径复制到 Xamarin.iOS 项目,这不能确保将文件添加到项目中。
推荐阅读
- c - 将 -130 转换为 16 位二进制二进制补码数
- java - 如何访问异常处理程序中的访问请求正文
- image - 在 Image.Network 中找不到图像
- angular - 带有分页功能的 NgRx 存储
- c++ - 解决 SPOJ 问题
- visual-studio-code - 如何在 Visual Studio Code 中自动清除终端?
- arduino - 如何更正错误 - 此范围内未定义“A2”?
- c# - 类作为参数
- python - 在 Django 视图中将对象的参数设置为其默认值
- angular - ag-grid @version v17.1.1 无法读取未定义的属性“创建”