Skip to content

Using the player in your own project

Matias Muhonen edited this page Feb 12, 2016 · 13 revisions

iOS

To use FreeStreamer simply means linking with FreeStreamer.framework. Drop FreeStreamer.framework to your project (FreeStreamer/FreeStreamer.xcodeproj). In project settings / Build Phases, add FreeStreamer.framework in Link Binary With Libraries as you would do with any system framework.

If you want to support background audio, add App plays audio and App downloads content from network to the target's Required background modes. You can find the property by clicking on the target on Xcode and opening the Info tab.

You can now stream an audio file like this. Declare the stream in your header file:

@class FSAudioStream;

@interface MyClass : NSObject {
    FSAudioStream *_audioStream;
}

Initialize and use the stream in your implementation:

#import <FreeStreamer/FreeStreamer.h>

_audioStream = [[FSAudioStream alloc] init];
[_audioStream playFromURL:[NSURL URLWithString:@"http://www.example.com/audio_file.mp3"]];

Note that FSAudioStream must exist during the playback of the stream. Do not declare the class as a local variable of a method or the stream will be deallocated and will not play.

Some servers may send an incorrect MIME type. In this case, FreeStreamer may not be able to play the stream. If you want to avoid the content-type checks (that the stream actually is an audio file), you can set the following property:

audioStream.strictContentTypeChecking = NO;
// Optionally, set the content-type where to fallback to
audioStream.defaultContentType = @"audio/mpeg";

For streaming playlists, you need to use the FSAudioController class. The class has some additional logic to resolve the playback URLs. Again, declare the class:

@class FSAudioController;

@interface MyClass : NSObject {
    FSAudioController *_audioController;
}

And use it:

#import <FreeStreamer/FreeStreamer.h>

_audioController = [[FSAudioController alloc] init];
_audioController.url = @"http://www.example.com/my_playlist.pls";
[_audioController play];

It is also possible to check the exact content of the stream by using the FSCheckContentTypeRequest and FSParsePlaylistRequest classes:

FSCheckContentTypeRequest *request = [[FSCheckContentTypeRequest alloc] init];
request.url = @"http://www.example.com/not-sure-about-the-type-of-this-file";
request.onCompletion = ^() {
    if (self.request.playlist) {
        // The URL is a playlist; now do something with it...
	}
};
request.onFailure = ^() {	
};

[request start];

That's it! For more examples, please take a look at the example project. For instance, you may want to use the onStateChange, onMetaDataAvailable and onFailure blocks to handle state changes.

OS X

For OS X, you can either use CocoaPods or manually link the classes from FreeStreamer.framework. For manual linking, see FreeStreamerDesktop/FreeStreamerDesktop.xcodeproj

Clone this wiki locally