【发布时间】:2021-01-10 04:30:25
【问题描述】:
我试图覆盖 UIApplication 的 RemoteControlRecieved 方法并收到以下错误:
NSInternalInconsistencyException 原因:只能有一个 UIApplication 实例。
我了解这个问题,但不知道如何解决它。我有一个实现 UIApplication 和 IStreaming 接口的 StreamingService 类。我所有的 AVPlayer 功能都在这个类中。 StreamingViewModel 类调用
DependencyService.Get<IStreaming>().Play().
当调用此行时,我收到上述错误。我不确定如何从 StreamingService 或 StreamingViewModel 类覆盖 RemoteControlRecieved。
非常感谢任何有关代码示例的帮助。
下面的类
public class StreamingViewModel : INotifyPropertyChanged
{
public bool DisplayPlay { get => !isPlaying; }
public bool DisplayPauseStop { get => isPlaying; }
// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
bool isPlaying;
bool IsPlaying
{
get => isPlaying;
set
{
isPlaying = value;
// Notify the property has changed
OnPropertyChanged("DisplayPlay");
OnPropertyChanged("DisplayPauseStop");
}
}
public void Play()
{
DependencyService.Get<IStreaming>().Play();
IsPlaying = true;
}
public void Pause()
{
DependencyService.Get<IStreaming>().Pause();
IsPlaying = false;
}
public void Stop()
{
DependencyService.Get<IStreaming>().Stop();
IsPlaying = false;
}
}
类 StreamingService
[assembly: Xamarin.Forms.ExportRenderer(typeof(MainPage), typeof(StreamingService))]
[assembly: Dependency(typeof(StreamingService))]
namespace test.iOS
{
public class StreamingService : PageRenderer, IStreaming
{
AVPlayer player;
bool isPrepared;
string dataSource = "https://stream.voxx.pro/radio/8260/radio.mp3";
public override void ViewDidLoad()
{
Console.WriteLine("StreamService ViewDidLoad");
base.ViewDidLoad();
}
public StreamingService()
{
Console.WriteLine("StreamService Default Constructor");
}
public void Play()
{
Console.WriteLine("Play");
if (!isPrepared || player == null)
player = AVPlayer.FromUrl(NSUrl.FromString(dataSource));
//Audio player Notification in lock screen
MPNowPlayingInfo nowPlayingInfo;
nowPlayingInfo = new MPNowPlayingInfo();
nowPlayingInfo.Artist = "Radio Caravan";
nowPlayingInfo.Title = "Join The Caravan";
// Register for receiving controls from lock screen and controlscreen
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = nowPlayingInfo;
//var command = MPRemoteCommandCenter.Shared;
//command.PlayCommand.Enabled = true;
//command.PauseCommand.Enabled = true;
//command.NextTrackCommand.Enabled = false;
//command.PreviousTrackCommand.Enabled = false;
isPrepared = true;
player.Play();
base.BecomeFirstResponder();
//To listen changes in lock screen
UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
}
public void Pause()
{
player.Pause();
}
public void Stop()
{
player.Dispose();
isPrepared = false;
}
public override void RemoteControlReceived(UIEvent theEvent)
{
Console.WriteLine("Remote Control Received");
base.RemoteControlReceived(theEvent);
if (theEvent.Subtype == UIEventSubtype.RemoteControlPause)
{
Console.WriteLine("Remote Pause");
player.Pause();
}
else if (theEvent.Subtype == UIEventSubtype.RemoteControlPlay)
{
Console.WriteLine("Remote Play");
player.Play();
}
}
}
}
MainPage.cs
namespace test
{
public partial class MainPage : ContentPage
{
private StreamingViewModel ViewModel { get { return (StreamingViewModel)this.BindingContext; } }
public MainPage()
{
InitializeComponent();
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
BindingContext = new StreamingViewModel();
}
// Callbacks to images tapped
private void Play_tapped(object sender, EventArgs e)
{
ViewModel.Play();
}
private void Pause_tapped(object sender, EventArgs e)
{
ViewModel.Pause();
}
private void Stop_tapped(object sender, EventArgs e)
{
ViewModel.Stop();
}
}
}
接口:IStreaming
public interface IStreaming
{
void Play();
void Pause();
void Stop();
}
【问题讨论】:
标签: c# ios xamarin dependency-injection