How to stop AVAudioPlayer once we navigate from one view to into other view in iOS8? - objective-c

I am fresher to iOS. I am using AVFoundation framework for AVAudioPlayer. This is used for running background sound when user enters into application home screen. This background sound should be stopped when it goes to other views and should be enabled when user come back to the home screen again. Below code is working fine in IOS7, when I upgrade to IOS8 background sound continuously playing and doesn’t stop when I go to other views. Kindly help me in this issue.
I am using below code for start and stop the player
NSString *pewPewPath = [[NSBundle mainBundle]pathForResource:#"background" ofType:#"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
NSData *data1 = [NSData dataWithContentsOfURL:pewPewURL];
audio = [[AVAudioPlayer alloc] initWithData:data1 error:nil];
audio.volume=8;
[audio play];

Call the [audio stop]; and set nil to the audio instance when you are navigating to another view. And write this code before you call the next view controller.

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.

iOS7 sending SMS from inside the app has a misaligned modal view

I'm writing an app where I need to allow the user sending SMS from within the app.
It works fine on iOS6 but not on iOS7.
When I'm trying to send a text message, the modal view appears but there is a strange gap between the "to" field and the list of possible contacts.
After selecting the first contact, the "to" field slides up and disappears and then I see past messages and my new message but with the same gap again.
I'm attaching two images showing the issue:
Here is the code I'm using:
if([MFMessageComposeViewController canSendText]) {
NSArray *recipents = nil;
NSString *message = #"Let's go";
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipents];
[messageController setBody:message];
[self presentModalViewController:messageController animated:YES];
}
Please tell me if know you know how to fix this issue.
Thanks!
Additional info:
This bug only happens in iOS 7.x not in in iOS 6.x
Also, if I use the MFMailComposeViewControllerDelegate to send emails, it works just fine (although they are both implemented by MessageUI.h)...
Ok, I figured it out.
The issue happens because I have a custom UINavigationController.
In my AppDelegate, I had the following code:
UIImage *navBarImage = [UIImage imageNamed:#"BarIos7.png"];
[[UINavigationBar appearance] setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];
Once I removed it, everything started working fine.

MPMoviePlayerController iOS play audio file issue

Hi all I am using MPMoviePlayerController to play both video (mp4) and audio (mp3) files. I am facing an issue when the view is removed form screen, the audio keeps playing in the background, video is ok. On iOS 4.3 everything works great, but when I switch to iOS5 and attempt to play audio and press back (remove the view) the audio is still pressent in the background. Is there something different in how MPMoviePlayerController handles audio files in 4.3 and 5 ? I am using the same code for playing both video/audio.
moviePlayBackDidFinish method, where I stop the player
[player setInitialPlaybackTime:-1];
[player stop];
player = nil;
[[player view] removeFromSuperview];
[player release];
Any help is appreciated :)
use this code to pause the audio/video in the MPMoviePlayerController
-(void)viewWillDisappear:(BOOL)animated
{
[mediaPlayer pause];
}

Calling UIWebView from IBAction ButtonPressed

This is probably a simple question...
I have a UIView with a button that corresponds to a URL address http:// etc. I want to be able to click this web address button and have it load a UIWebView on a separate UIViewController and XIB that shows the website, allowing me to then hit a back button and go back to my first view that has the original button. Basically I want to be able to load the webpage but have it operate within the App rather than start up Safari. Is this possible?
So far I have build a dedicated XIB and ViewController for my webpage for which the viewDidLoad method looks like this:
- (void)viewDidLoad{
[super viewDidLoad];
NSString *urlAddress = pushURL; //pushURL is passed as parameter
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[addressBar setText:urlAddress];
}
Question is... how do I call the UIWebView from my button (which is in the first class/parent viewController)? All my other viewControllers in my app are using UITableView so it's a simple DidSelectRowAtIndex.
Thanks.
It sounds like you want to be using a UINavigationController. Then when the button is tapped just push the UIViewController with the web view to the UINavigationController.

Modal View Controller displays blank first time, works properly subsequently

I have a modal view controller that I am trying to present a web view in it. Tthe first time it appears, it shows up as blank. When I close it out and click it again, however, it displays fine. I'm thinking it may be an issue with the loading of the webView, but I've tried displaying it only when the webView finishes loading, but it never gets called then.
NSURL* newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webViewController.webView loadRequest: newURLRequest];
[newURL release];
[newURLRequest release];
webViewController.modalPresentationStyle=UIModalPresentationPageSheet;
[self presentModalViewController:webViewController animated:YES];
The webView control won't be accessible the first time until the first present call causes controls to be loaded and initialized. Before the first present, webViewController.webView will be nil and so calling loadRequest on it will do nothing.
You could move the loadRequest call after the presentModalViewController call.
But instead of accessing controls in view controller's views directly, it'd be better to declare the url string as a NSString property (called say urlString) in WebViewController and set it to fileString before the presentModalViewController call (and don't create the NSURL, etc there):
webViewController.urlString = fileString;
[self presentModalViewController:webViewController animated:YES];
Finally, in WebViewController, in viewWillAppear: or viewDidAppear:, create the NSURL (using urlString), create the NSURLRequest, and call loadRequest.