Streaming MP3 audio from a WCF REST service

For a project i am working on in my spare time, i needed to stream MP3 audio from a REST service, implemented in WCF. I was supprised at how easy this was. It’s as simple as setting the response content type and returning a stream:

IAudioDemoService.cs method definition:

Method = "GET",
UriTemplate = "/AudioStream")]
Stream GetAudioStream();

AudioDemoService.cs method implementation:

public Stream GetAudioStream()
	WebOperationContext.Current.OutgoingResponse.ContentType = "audio/mpeg";
	return new FileStream(@"c:\AudioFile.mp3", FileMode.Open);

(actually the content type isn’t strictly nessesary, but it enables a client to identify the information in the stream – a browser for instance might use it to select an appropriate application for processing)


Tags: , , , , , ,

5 Responses to “Streaming MP3 audio from a WCF REST service”

  1. Paul Says:

    Does this read the whole file c:\AudioFile.mp3 into the server memory before returning it back to the client?

    I tried a similar thing and the server memory jumped right up when I used a very large file of 500mb.

  2. Max Says:

    Where can i find info about content type? a reference list etc? tnx

    Based on this how would u implement a file Seek like that of youtube so u can go foward an backwards on the mp3file and let this work on a standard player like windows media player or VLC.

  3. Vitas Says:

    Hi, maybe you could help me out. I’m thinking of streaming some audio files like this. What would be a best way to handle and recover data from this scenario:Let say we would like to have 2 servers and if one fails in a middle of the stream, how would we go recovering/starting from point we crashed on another server? I hope I expressed my-self clear enough.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: