Xamarin.forms C#中的音频暂停 [英] audio pause in xamarin.forms C#
问题描述
因此,我正在使用xamarin.forms构建需要播放和控制音频的应用程序.我目前可以在android和iOS上播放音频文件,但是无法使用其他控件(例如.Pause()和.Stop()
So i am building an app using xamarin.forms which needs to play and control audio. I have currently got my audio file playing on both android and iOS BUT I can't get other controls to work such as .Pause() and .Stop()
在两个示例中都可以播放音频.
Playing audio works in both examples.
我先谢谢你!
这是我在每个平台上的代码
Here is my code for each platform
机器人:
using System;
using Xamarin.Forms;
using AudioPlayEx.Droid;
using AudioToolbox;
using Android.Media;
using Android.Content.Res;
using Android.OS;
using Android;
[assembly: Dependency(typeof(AudioService))]
namespace AudioPlayEx.Droid
{
public class AudioService : IAudio
{
public AudioService()
{
}
MediaPlayer player;
public void PlayAudioFile(string fileName)
{
var player = new MediaPlayer();
var fd = Android.App.Application.Context.Assets.OpenFd(fileName);
player.Prepared += (s, e) =>
{
player.Start();
};
player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
player.Prepare();
}
protected MediaPlayer playme;
public void PauseAudioFile(String fileName)
{
var player = new MediaPlayer();
var fd = Android.App.Application.Context.Assets.OpenFd(fileName);
player.Prepared += (s, e) =>
{
player.Pause();
};
player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
player.Prepare();
}
}
}
iOS:
using System;
using Xamarin.Forms;
using AudioPlayEx;
using AudioPlayEx.iOS;
using System.IO;
using Foundation;
using AVFoundation;
[assembly: Dependency(typeof(AudioService))]
namespace AudioPlayEx.iOS
{
public class AudioService : IAudio
{
public AudioService()
{
}
public void PlayAudioFile(string fileName)
{
string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
var url = NSUrl.FromString(sFilePath);
var _player = AVAudioPlayer.FromUrl(url);
_player.FinishedPlaying += (object sender, AVStatusEventArgs e) => {
_player = null;
};
_player.Play();
}
public void PauseAudioFile(string fileName)
{
string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
var url = NSUrl.FromString(sFilePath);
var _player = AVAudioPlayer.FromUrl(url);
_player.Pause();
}
}
}
推荐答案
我认为您需要将AVAudioPlayer的实例保存到变量中并在该对象上调用pause.现在,您正在创建一个新的AVAudioPlayer并对其调用暂停,这无济于事.我添加了iOS代码以显示我的意思. Android应该类似.
I think you need to save your instance of AVAudioPlayer to a variable and call pause on that object. Right now you're creating a new AVAudioPlayer and calling pause on it, which is doing nothing. I've added iOS code to show what I mean. Android should be similar.
iOS:
public class AudioService : IAudio
{
AVAudioPlayer _player;
public void PlayAudioFile(string fileName)
{
string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
var url = NSUrl.FromString(sFilePath);
_player = AVAudioPlayer.FromUrl(url);
_player.FinishedPlaying += (object sender, AVStatusEventArgs e) => {
_player = null;
};
_player.Play();
}
public void PauseAudioFile(string fileName)
{
_player.Pause();
}
}
这篇关于Xamarin.forms C#中的音频暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!