How to stream online radio from SHOUTCast in my iOS App? - objective-c

I am developing an iPhone Online Radio Streamer App. I used Matt Gallaher's AudioStreamer class to stream online tune in stations from SHOUTCast. But the problem is that the AudioStreamer API has been abandoned and fails to play various radio channels.
I have googled and found various alternatives including: AVPlayer, MPMoviePlayer etc.
Please recommend which player will fulfill the requirement best.

You can use MPMovieplayerviewcontroller. It is perfect for streaming audio/video. I am also
use this for streaming audio in one of my App & also it looks like default player of iPhone.
Ok here is coding for use of this player as I am doing in my project:
NSString *geturl = [[radiotablearray objectAtIndex:btntag]objectForKey:#"iurl"];
NSLog(#"geturl..%#",geturl);
NSURL *fileURL=[NSURL URLWithString:geturl];
NSLog(#"fileURL..%#",fileURL);
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[moviePlayerController.moviePlayer prepareToPlay];
moviePlayerController.moviePlayer.shouldAutoplay=YES;
moviePlayerController.view.frame = self.view.frame;
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
Also add mediaplayer & Avfoundation framework in App.And add or import these two in .h file:
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
& import #import <MediaPlayer/MediaPlayer.h>this in .m file. also make property of player like below in .h file:
MPMoviePlayerViewController *moviePlayerController;
#property(strong,retain) MPMoviePlayerViewController *moviePlayerController;
And add method where you want but also make changes in code according to your need I am just send you my implement code you just change it with your requirement. If you have any problem then ask me. Best of luck.

// viewcontroller.h
MPMoviePlayerViewController *moviePlayer;
//viewcontroller.m
This is implemented in viewDidLoad
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",#"http://sample url"]]];
[moviePlayer.moviePlayer prepareToPlay];
moviePlayer.view.hidden = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];

Related

iOS 9 is 'Picture In Picture' mode available with 'Requires Fullscreen' set to YES?

I have an app that requires fullscreen, but want to make Picture In Picture feature available when user opens video playback modal controller (My own player written with AVFoundation).
I have 'app requires fullscreen' flag set to YES in settings.
Can I use Picture in Picture mode for video playback while all my app requires to be fullscreen?
I've created a sample project and found that YES, Picture In Picture feature doesn't depend whether your app needs fullscreen or not.
Maybe this would be helpful for those who are looking for the same question as I did.
Set base SDK version to latest (9.0)
Set "App requires fullscreen" flag in the project settings
Set AVAudioSession category to AVAudioSessionCategoryPlayback in application:didFinishLaunchingWithOptions:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
Just created AVPlayerViewController and presented it from my root controller
AVPlayerViewController *moviePlayerController = [[AVPlayerViewController alloc] init];
AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:#"http://127.0.0.1:12345/movie.mp4"]];
moviePlayerController.player = player;
[self presentViewController:moviePlayerController animated:YES completion:nil];
PiP button appeared at the bottom right corner of playback controls and it works!
P.S. I may remove the question and answer if it's obvious or too simple and nobody find it useful.

How to play a video splash in ios?(Objective c)

I need a help in how to implement the video splash while my application launches in ios.
The sample which is similar to:
http://www.iosappx.com/code/9173/
It's really simple guy.
You can find the solution in sample project here :
https://github.com/chinsyo/uber-video-welcome
You can make a fake splash screen and load it after the original Splash appeared with smoothly animation then you play the video as you want
Bests,
You could have a "fake" launch video. So, if you have a local video file(mp4 or other supported formats), you could set the window rootViewController to AVPlayerViewController. Example as below:
Within your AppDelegate.m file,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVPlayer *player = [AVPlayer playerWithURL:"YOUR URL"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(continueWithLaunch) name: AVPlayerItemDidPlayToEndTimeNotification object:player];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
self.window.rootViewController = controller;
controller.player = player;
[player play];
}
-(void) continueWithLaunch {
//Change the window rootView controller to your base controller (e.g. tabbar controller)
}
I don't think it is possible: right now, the only "splash" (i.e., launch) screens you can implement for iOS are either:
Static images (the historic Default*.png)
Single-page storyboards (Launchscreen.storyboard)
The app you linked to seems to be just playing video on its first (login) screen, after the "splash" (likely, backed by a view controller).

Stopping Audio from different class

Note: I have looked at other questions and the answers to them are quite vague or unhelpful
I have this code in View_Controller.h
#property AVAudioPlayer *playerSaxophone;
Then I do this in the same file (in viewDidLoad):
NSURL *backgroundMusicSaxophone = [NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:#"saxophone" ofType:#"wav"]];
self.playerSaxophone = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicSaxophone error:nil];
self.playerSaxophone.numberOfLoops = -1;
[self.playerSaxophone setVolume:0.5];
[self.playerSaxophone play];
In a different view controller I want to be able to stop or start this audio from playing by clicking 2 buttons. Is there any way I can do this?
Edit: I tried this in the "different" view controller .m file
//I do import ViewController.h in this file
- (IBAction)stop:(id)sender {
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[viewController.playerSaxophone stop];
}
But it didn't work.
You need to call:
[playerSaxophone stop];
to make the music stop.
Problem: Another view controller won't have a reference to playerSaxophone.
The fact that you want to add code that references this from another view controller means you'll have to find a way to have the three objects - view controller A, view controller B, and your audio player - communicate.
Here are a few options:
Use the delegation pattern to have one view controller tell the other view controller to stop the player.
The same as #1, but using the notification pattern. (This is ideal if you'd have a 3rd place that controls the player).
You could move playerSaxophone to be a #property on either your App Delegate or a singleton. Then each view controller will be able to find it.
These are just a few solutions. There are others.
Additional reading:
Apple's Coordinating Efforts Between View Controllers
This Stack Overflow answer
How do I set up a simple delegate to communicate between two view controllers?

detect when UIWebView finished loading without a delegate

is it possible to execute:
[UIWebView loadRequest:[NSURLRequest requestWithURL:url]];
in a way that I know when UIWebView is finished loading the NSURL so I can proceed with further processing ? Other than using a delegate. If not what are the conditions under which the delegate will work.
Thank you for any tips/ideas
To explain further. I have this code that worked for me in a test application:
webview = [[UIWebView alloc] init];
webview.delegate = self;
[webview loadRequest:[NSURLRequest requestWithURL:url]];
In the test app's header I had:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webview;
}
I'm trying to replicate this in a very complex environment that I don't fully understand.
I don't know where to put since it's not clear to me. The interface is defined as:
#interface Stream (Extension)
#end
and it has four more interface defined later in the header file.
In my implementation I get "undeclared identifier self"
You could find out when the web view has finished fetching the content of the NSURL and any embedded content requests in the page if you used an NSURLProtocol to intercept the network traffic (but you would have to know in advance if the html page is going to make requests to fetch i.e. images).
But you cannot detect when the UIWebView has loaded the dom or finished displaying the page
without the delegate (out of curiosity may I know why you are asking such a question).

Setting the AVAudioSession category in AppDelegate.m

So I hate to have to ask this question but I've spent a fair bit of time searching through Apple's documentation and Google with no avail. I'm simply trying to set the AVAudioSession category for my app ONCE, when the applicationDidFinishLaunching. I have an app that plays an audio stream and I would like it to continue playing when the app enters the background, so I'm trying to use the Playback category. Here is my code for AppDelegate.m :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];
// create window and set up navigation controller
[window addSubview:myNavController.view];
[window makeKeyAndVisible];
}
# pragma mark -
# pragma mark AVAudioSession Delegate Methods
- (void)beginInterruption {
}
- (void)endInterruption {
}
- (void)endInterruptionWithFlags:(NSUInteger)flags {
}
- (void)inputIsAvailableChanged:(BOOL)isInputAvailable {
}
With this code, the audio fades out anytime I hit the home button, putting
the app in the background. Any help is much appreciated, I hope that it
is a quick fix type of answer for anybody who has done this before.
First add the UIBackgroundModes key to your Info.plist file if you haven't done already.
More info here.
If you have done that already, which framework do you use to play your media?
Thanks for the help Irene. You are pretty much right with your answer except I just wanted to provide the steps that were necessary for it to work for me. I read the apple documentation that you posted and for some reason it left these important details out:
When you add the UIBackgroundModes key in the .plist file, you have to make it an array.
The value for Item 0 of the array should be audio.
Of course your app should also take care of setting its audio session category in combination with setting this key.