C# 如何使用Xamarin iOS/Android播放简单的音频文件
我正试着学习和和Xamarin一起玩:) 我想在某个时间结束时播放一个简单的声音,部分我成功了,这在android和ios模拟器上都能正常工作,但当我尝试在iPhone上构建应用程序时,声音再现的那一刻就让我大吃一惊 我写的代码是从中复制的 所以我的代码是: iAudio.cs:C# 如何使用Xamarin iOS/Android播放简单的音频文件,c#,xamarin,audio,xamarin.forms,xamarin.ios,C#,Xamarin,Audio,Xamarin.forms,Xamarin.ios,我正试着学习和和Xamarin一起玩:) 我想在某个时间结束时播放一个简单的声音,部分我成功了,这在android和ios模拟器上都能正常工作,但当我尝试在iPhone上构建应用程序时,声音再现的那一刻就让我大吃一惊 我写的代码是从中复制的 所以我的代码是: iAudio.cs: using System; namespace StopWatch { public interface IAudio { void PlayAudioFile(string fileN
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");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统诊断;
使用System.IO;
使用System.Linq;
运用系统反思;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
命名空间秒表
{
//了解有关使自定义代码在Xamarin.Forms预览器中可见的更多信息
//参观https://aka.ms/xamarinforms-previewer
[设计时间可见(错误)]
公共部分类主页:ContentPage
{
秒表;
公共主页()
{
初始化组件();
秒表=新秒表();
lblStopWatch.Text=“00:00:00”;
lblStopWatchAllert.Text=“”;
}
私有void btnStartClicked(对象发送方,事件参数e)
{
秒表。开始();
Device.StartTimer(时间跨度从毫秒(10),()=>
{
lblStopWatch.Text=stopwatch.appeased.ToString().Substring(0,8);
控件(lblStopWatch.Text);
返回true;
});
}
私有void btnStopClicked(对象发送方,事件参数e)
{
秒表;
}
私有void btnResetClicked(对象发送方,事件参数e)
{
秒表复位();
}
专用无效控件(字符串时间)
{
如果(字符串比较(时间,“00:00:03:0000”)>0)
{
秒表复位();
lblStopWatchAllert.Text=“时间到了!”;
DependencyService.Get().PlayAudioFile(“Alert.mp3”);
}
}
}
}
在iOS AudioService.cs文件的这一点上,代码使我崩溃:
我认为问题在于info.plist(尽管我很可能错了),但我不知道如何解决它:(
有人能帮我吗?谢谢你我已经检查了这段代码,它在我的网站上运行。调用play方法如下:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
DependencyService.Get().PlayAudioFile(“Alan_Walker.mp3”);
其他代码与您在iOS和Android中的代码相同,它们都能成功播放声音
这里您需要注意的是,本地声音文件应该添加到每个平台。每个平台都有其特定的文件夹来放置auido文件
在Android中,您需要将其放入Assets
文件夹,如下所示:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
在iOS中,您需要将其放入Resources文件夹,如下所示:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
=====================================更新===================================
您应首先将文件添加到此文件夹,如下所示:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
然后将现有文件添加到此文件夹:
然后在安装应用程序时,此文件将添加到移动设备中。无论是模拟器还是物理设备
注意:
未将文件从其他路径复制到Xamarin.iOS项目,这无法确保将文件添加到项目中。我已检查此代码,它在我的站点中工作。调用播放方法如下:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
DependencyService.Get().PlayAudioFile(“Alan_Walker.mp3”);
其他代码与您在iOS和Android中的代码相同,它们都能成功播放声音
这里您需要注意的是,本地声音文件应该添加到每个平台。每个平台都有其特定的文件夹来放置auido文件
在Android中,您需要将其放入Assets
文件夹,如下所示:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
在iOS中,您需要将其放入Resources文件夹,如下所示:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
=====================================更新===================================
您应首先将文件添加到此文件夹,如下所示:
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
然后将现有文件添加到此文件夹:
然后在安装应用程序时,此文件将添加到移动设备中。无论是模拟器还是物理设备
注意:
未将文件从其他路径复制到Xamarin.iOS项目,这无法确保将该文件添加到项目中。Hi,能否显示Xamarin iOS文件夹中的音频文件位置。它应添加到iOS中的
Resources
文件夹中。Hi,能否显示Xamarin iOS文件夹中的音频文件位置。它应添加到Resources
iOS中的文件夹。我已经这样做了,事实上,如果我在模拟器上尝试代码,它会正常工作。只有当我尝试在iPhone上构建时,才会出现此验证问题。因此,“我认为”是否存在与权限相关的问题,并且我在生成时未在设备上复制音频文件。可能是与info.plist或itunes相关的问题?Thanks@CharlieLomello您好,我也在物理设备上进行了测试,它也可以工作。而且我没有在info.plist
中添加任何内容。顺便说一下,也许您没有将音频文件放在Xamari中n、 iOS。如果不将文件名复制到资源
,您需要先在文件夹中添加文件,然后将现有文件添加到此文件夹。我将对此进行解释。如果只是将文件名复制到文件夹,文件路径也在pc中。在物理设备中安装应用程序时,无法在移动设备中添加文件。谢谢,我错过了最后一步,n如何完美地工作:DI已经做到了这一点,事实上,如果我在模拟器上尝试代码,它就会正常工作。只有在我尝试在iPhone上构建时,才会出现此问题。因此,“我很瘦。”