HttpWebRequest for ShoutCast on Windows Phone7

谁说我不能喝 提交于 2020-01-01 14:36:15

问题


I tring to stream shoutcast stream in my window phone 7 app

I start an async HttpWebRequest like this

//Init Request

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://ACommonoShoutCastUrl:8000");

myHttpWebRequest.Headers["Icy-MetaData"] = "1";

myHttpWebRequest.UserAgent = "WinampMPEG/5.09";

myHttpWebRequest.AllowReadStreamBuffering = true;

//Call

 // Create an instance of the RequestState and assign the previous myHttpWebRequest object to its request field.  

RequestState myRequestState = new RequestState();

 myRequestState.request = myHttpWebRequest;

 // Start the asynchronous request.

 IAsyncResult result = (IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallBack), myRequestState);

The problem is that the CallBack->RespCallBack is never called...

This code worked for me normally in other environments but not on the phone...

I tired also to use WebClient that seems to stream data,

the problem in this case is that it never call the end OpenReadCompleted because of endelss shoutcast stream

Thanks for support

any help would be appreciated


回答1:


SHOUTcast implements its own protocol so you can't directly access and play it. You can use DownloadStringAsync (you will need a WebClient instance for this) to download the PLS file and read the URL with the help of RegEx.

When you get the URL, you can read the raw audio data by implementing MediaStreamSource and then use a MediaElement to play the contents.

You can find a sample implementation of MediaStreamSource here.




回答2:


I just put the following on a page and the callback was called in repsonse to the button click. (I set a break point on the throw statement and it was hit.)

    private HttpWebRequest myHttpWebRequest;

    public MainPage()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //Init Request 
        //The following URI was chosen at random
        myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://yp.shoutcast.com/sbin/tunein-station.pls?id=1377200");
        myHttpWebRequest.Headers["Icy-MetaData"] = "1";
        myHttpWebRequest.UserAgent = "WinampMPEG/5.09";
        myHttpWebRequest.AllowReadStreamBuffering = true;

        // Start the asynchronous request.
        myHttpWebRequest.BeginGetResponse(RespCallBack, myHttpWebRequest);
    }

    private void RespCallBack(IAsyncResult ar)
    {
        throw new NotImplementedException();
    }



回答3:


I am trying to implement the Shoutcast streaming to my MediaElement via my own MediaStreamSource class. I have a loop in which I am downloading data from Shoutcast server and then set the data to the MediaStreamSource class - this works not perfectly for yet, but I discovered another more important issue. I made a test. I have downloaded a stream to a mp3 (stream is in mp3) file, then put this file to my application and set it to my MediaStreamSource. Here's the code for this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var assembly = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
    var res = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Demo1.sample.mp3");

    byte[] data = new byte[res.Length];
    res.Read(data, 0, data.Length);

    MemoryStream ms = new MemoryStream();
    ms.Write(data, 0, data.Length);
    ms.Position = 0;

    ShoutcastMediaStreamSource ss = new ShoutcastMediaStreamSource(ms);
    player.SetSource(ss);             
}

my ShoutcastMediaStreamSource based on MenagedMediaHelpers. So when I put this stream to my ShoutcastMediaStreamSource in debbuger I can see that the method OpenMediaAsync() is called properly, then the GetSampleAsync() is called in a loop by MediaElement, here also everything is fine, but when I run this app there is no sound ! Neither on the emulator nor on the device, and there are no errors. I think, the GetSampleAsync() method is running too fast, bacause for the file (and also stream) duration is about 30 sec., and this app ends after ca. 10 sec. But nevertheless, there should be a sound (scratch).

BUT whats suprising - this app works in Silverlight as Web Page ! The music is playing. I am confused.

Here is allso an app

http://timheuer.com/blog/archive/2010/08/16/download-and-store-media-for-playback-in-windows-phone-7-using-mediastreamsource.aspx

and there is a comment:

If Mp3MediaStreamSource is set as a source for MediaElement then MediaElement doesn't play that file and donesn't show any error in Windows phone 7 sdk RTM version. In previsios version it was working but it's not working with Windows phone 7 sdk final release.




回答4:


The callback gets called if you disable read stream buffering:

webRequest.AllowReadStreamBuffering = false;


来源:https://stackoverflow.com/questions/3734843/httpwebrequest-for-shoutcast-on-windows-phone7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!