0

我想运行Background file transferBackground audio agent但我得到错误的示例代码,在前台应用程序中运行正确。是否可以从后台音频代理运行后台传输?

这里是例子:

string transferFileName = @"http://www.reggaeavenue.com/MP3/leave%20short.mp3"; 
Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute); 

BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri); 

transferRequest.Method = "GET"; 

string downloadFile = "result.mp3"; 
Uri downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute); 
transferRequest.DownloadLocation = downloadUri; 

transferRequest.Tag = downloadFile; 

transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery; 

try 
{ 
    BackgroundTransferService.Add(transferRequest); 
} 
catch (InvalidOperationException ex) 
{ 
    MessageBox.Show("Unable to add background transfer request. " + ex.Message); 
} 
catch (Exception) 
{ 
    MessageBox.Show("Unable to add background transfer request."); 
} 

上的线与加transferRequest到BackgroundTransferService我得到错误:

System.InvalidOperationException: Operation is not valid due to the current state of the object. 
    at Microsoft.Phone.BackgroundTransfer.BackgroundTransferRequest.SubmitHelper() 
    at Microsoft.Phone.BackgroundTransfer.BackgroundTransferRequest.Submit() 
    at Microsoft.Phone.BackgroundTransfer.BackgroundTransferService.Add(BackgroundTransferRequest request) 
    at Project.AudioPlaybackAgent.AudioPlayer.CreateBackgroundTransfer() 

那么,是否可以运行从后台代理transferm?我怎样才能解决这个问题?谢谢

回答

2

根据MSDN后台代理不支持某些API(包括后台传输)。即使您设法做一些事情,您的应用程序也可能无法通过认证测试。

为什么不在主UI中下载文件或直接从网络源播放文件?

+0

谢谢,我无法找到它。因为我想缓存歌曲然后播放它们。我也想从后台缓存它们。 –

+0

当然,您可以连接背景音频和您的主UI,主UI下载(在后台)文件并更新播放列表。在bacgound代理下载可能很困难,请注意,您有限的时间(30秒)结束调用状态(在发送NotifyComplete()之前) - 如果您的时间用完,您的代理将被终止。 – Romasz

+0

是的,我可以在后台代理下载它,但是当用户尝试使用控件时,什么都不会发生。必须杀死第一个代理,然后创建处理响应的新代码,这对我来说是不可接受的。 –