gpt4 book ai didi

c# - AudioPlayerAgent、计时器和网络服务

转载 作者:行者123 更新时间:2023-11-30 15:40:01 32 4
gpt4 key购买 nike

我的应用程序读取了一个 shoutcast。

播放的音乐的元数据是从返回我 Json 的网络服务收集的(因此我不必解码流)。我使用计时器每 20 秒调用一次 Web 服务,这在我的应用程序中有效,但在 AudioPlayer.cs 中无效

    //Timer
private DispatcherTimer timer;


/// <remarks>
/// AudioPlayer instances can share the same process.
/// Static fields can be used to share state between AudioPlayer instances
/// or to communicate with the Audio Streaming agent.
/// </remarks>
public AudioPlayer()
{
if (!_classInitialized)
{
_classInitialized = true;
// Subscribe to the managed exception handler
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
Application.Current.UnhandledException += AudioPlayer_UnhandledException;

});
}

//I activate the timer
timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(20) // <------- Error here UnauthorizedAccessException was unhandled. Invalid cross-thread access.
};
timer.Tick += timer_Tick;
timer.Start();
}


private void timer_Tick(object sender, EventArgs e)
{
HttpWebRequest request = WebRequest.Create("http://127.0.0.81:8003/getmeta") as HttpWebRequest;
request.BeginGetResponse(new AsyncCallback(AsyncBack), request);
}

private void AsyncBack(IAsyncResult ias)
{
HttpWebRequest req = (HttpWebRequest)ias.AsyncState;

try
{
using (HttpWebResponse res = req.EndGetResponse(ias) as HttpWebResponse)
{
StreamReader stream = new StreamReader(res.GetResponseStream());
String jsonToParse = stream.ReadToEnd();
JObject jObject = JObject.Parse(jsonToParse);

AudioTrack track = BackgroundAudioPlayer.Instance.Track;

track.BeginEdit();
track.Title = (string) jObject["title"];
track.Artist = (string) jObject["artist"];
track.Album = (string) jObject["album"];
track.EndEdit();


res.Close();
}
}
catch (WebException e)
{
timer.Stop();
}
}

谢谢你的帮助

最佳答案

AudioPlayer 类非常独特。它只存在一小段时间。在使用 BackgroundAudioPlayer 的应用程序中,您对 AudioPlayer 类的实现只会保持事件状态以完成更改播放状态的任务。因此,当用户开始播放某些内容时,将创建 AudioPlayer 类的一个实例来完成开始播放的任务。一旦您在 OnUserAction 或 OnPlayStateChanged 中调用 NotifyComplete(),您的 AudioPlayer 实例就会消失。

与 AudioPlayer 相关联的后台线程仍将处于事件状态,您可以在该线程中让其他对象处于事件状态,但 AudioPlayer 将被终止。创建的默认 AudioPlayer 使用 _classInitialized 字段对此进行提示。这是静态的,因为 AudioPlayer 将被创建多次,但我们只想订阅该事件一次。

我会建议两件事之一。第一种是仅在需要前进到下一首歌曲时才获取 json 响应。在响应返回之前,您不会调用 NotifyComplete()。这是一些伪代码:

override OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
{
GetJsonResponse();
}
private void GetJsonResponce()
{
// async call to your service
// When async completes:
track.BeginEdit();
track.Title = (string) jObject["title"];
track.Artist = (string) jObject["artist"];
track.Album = (string) jObject["album"];
track.EndEdit();
NotifyComplete();
}

第二个是有一个在后台执行此操作的类。该类将有一个静态属性来获取线程中存在的实例。然后您的 AudioPlayer 将从该对象中获取所需的信息

public class Songs
{
static Songs _instance;
public static Songs Instance
{
get { return _instance ?? new Songs(); }
}
// Do you timer stuff here

// Allow the ability to access the timer stuff also.
}
// in AudioPlayer
override OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
{
Songs.Instance.GetStuff
NotifyComplete();
}

关于c# - AudioPlayerAgent、计时器和网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9702935/

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