Windows Phone background music playing local agent implementation is discussed

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Some of the problems encountered in WP platform music before an article discussing play, after cudgel thinking and multi reference Android implementation; that we can consider a local agent thought to complete our listening and deposit, and fluid drag the progress bar. Hope everyone together to discuss. You can download my code to study

Source code

Android local agent in the reference:

Android MediaPlayer and Http Proxy combined with the basics

Android MediaPlayer and Http Proxy binding increases if we could achieve it, we can realize the buffer progress bar

Get along well with the Android MdeiaPlayer Meida Proxy

Through the above reference my ideas as follows:

1, First I will BackGroundAudiaoPlayer AudiaoTrack address is set to the local proxy address, request the local proxy,

2, Local agents by the request, the same request is sent to the remote server,

3, The remote server response to the local proxy,

4, The local agent will flow back to BackGroundAudiaoPlayer,

Next we first have a look I capture analysis directly request the remote server BackGroundAudiaoPlayer, he includes two processes, the first request:

In my songs after a period of time, it will continue to send a Range request:

The two process directly request the remote server BackGroundAudiaoPlayer, a help should be sent the above two request, if the file is large enough in May second steps will be repeated.

The following is my implementation process:

1, First I will replace the local address remote address, and start the local monitoring:

 //Start the local proxy is listening
            MediaProxy mp = new MediaProxy();
            //In the server address prefixed with the local address and port, let him ask the local agent
            string mp3Url = @" style_ DJ latest version of.Mp3";
            BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri(mp3Url, UriKind.Absolute), "South of the Yangtze River Style", "Stick", null, null, null, EnabledPlayerControls.All);

The second step I process the request MdeiaProxy.cs related in the local agent.:

/// <summary> /// The proxy class /// </summary> public class MediaProxy { StreamSocketListener socketListener; public void StartSocketListener() { socketListener = new StreamSocketListener();//Create a local StreamSocketListener monitor socketListener.ConnectionReceived + = socketListener_ConnectionReceived; socketListener.BindServiceNameAsync("33123"); } void socketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) { StreamSocket s = args.Socket; RequestHttp(s); } /// <summary> /// Remote requests to the server /// </summary> /// <param name="obj"></param> private async void RequestHttp(object obj) { StreamSocket s = obj as StreamSocket; // bool hasRange = false;//To mark the second request try { DataReader reader = new DataReader(s.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; uint numStrBytes = await reader.LoadAsync(5120); string requestStr = reader.ReadString(numStrBytes); // using (IOutputStream output = args.Socket.OutputStream) Stream outputStream = s.OutputStream.AsStreamForWrite(); { string[] requestHeaders = requestStr.Split(new char[] { '\r', '\n' }); string requestMethod = requestHeaders[0]; string[] requestParts = requestMethod.Split(' '); string httpServer = "http:/"; string url = httpServer + requestParts[1];//The assembly distancemp3Address HttpWebRequest webRequest = WebRequest.CreateHttp(url); webRequest.AllowReadStreamBuffering = false;//Set here tofalse, To avoid downloading the entire flow to obtain the corresponding #region HTTPThe head information processing Dictionary<string, string> pragmaDic = new Dictionary<string, string>(); for (int i = 1; i <requestHeaders.Length; i++) { if (!string.IsNullOrWhiteSpace(requestHeaders[i])) { string[] head = requestHeaders[i].Split(':'); if (head.Length == 2 && head[0] != "Host") { if (head[0].ToLower() == "accept") { webRequest.Accept = head[1]; continue; } //The first request does not containRange //if (head[0].ToLower() == "range") //{ // webRequest.Headers[head[0]] = head[1]; // hasRange = true; // continue; //} if (head[0].ToLower() == "contentlength") { webRequest.ContentLength = long.Parse(head[1]); continue; } if (head[0].ToLower() == "contenttype") { webRequest.ContentType = head[1]; continue; } if (head[0].ToLower() == "user-agent") { webRequest.UserAgent = head[1]; continue; } if (head[0].ToLower() == "pragma") { pragmaDic.Add(head[1], head[1]); continue; } webRequest.Headers[head[0]] = head[1]; } } } if (pragmaDic.Count > 0) { string pragma = string.Empty; foreach (string p in pragmaDic.Values) { pragma += p; } webRequest.Headers["Pragma"] = pragma; } #endregion webRequest.BeginGetResponse((res) => { System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { HttpWebRequest request = res.AsyncState as HttpWebRequest; HttpWebResponse response = request.EndGetResponse(res) as HttpWebResponse; WebHeaderCollection headerCollection = response.Headers; string codeEn = response.StatusCode.ToString(); int codeNum = (int)response.StatusCode; //The first response header: 将服务器返回的头信息全部返回给播放器 //string header =string.Format("HTTP/1.1 {0} {1}\r\n{2}" ,codeNum,codeEn, headerCollection.ToString()); //The second response header: 将自定义头部 string header1 = String.Format(@"HTTP/1.1 200 OK\r\nContent-Type: audio/mpeg\r\nConnection: keep-alive\r\nContent-Length: {0}\r\n\r\n", response.ContentLength); Stream stream = response.GetResponseStream(); // using (Stream stream = response.GetResponseStream()) { byte[] headerArray = Encoding.UTF8.GetBytes(header1); outputStream.WriteAsync(headerArray, 0, headerArray.Length); stream.CopyToAsync(outputStream); outputStream.FlushAsync(); } }); }, webRequest); } } catch (Exception ex) { } } }

I don't know I the realization of the above problem is how, I met many strange questions:

1, I sometimes play the song, but the player to the part error, sometimes also can play songs, but most of the time is not to play the song, I hope you make a point in time to test my code in the AudioPlayer.cs OnError.

2, Because every time an error in OnError, the exception message and every time is a string of numbers, do I don't know what went wrong.

Analysis of the causes of the problems:

In OnError's remarks in such a word "every player error (such as AudioTrack did not download correctly) called", so I guess I don't have the right to return the data to the wrong.

There is a monitoring processing function which I will get the same request, I think because the HTTP in the WP request is asynchronous, because the asynchronous non blocking, so when I data also did not return, BackgroudAudiaPlayer did not receive will continue to send the same request,

In general, received two after the program will produce abnormal, and into the OnError. So I guess if a synchronous request will not correct (seemingly Android is synchronous request implementation), debugging a few days can not solve, hope everybody have a look my idea is feasible, or I have to achieve the problem,

Please comment. Welcome to my blog, many special discuss

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lorin at December 25, 2013 - 8:31 PM