MPMoviePlayerController iOS play audio file issue - objective-c

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];
}

Related

ios8 avfoundation camera freeze upon start

My camera freezes when the app starts. The only way to unfreeze it is to go back to the home screen (without closing the app) and reopening it.
I have used [session startRunning]in my ViewWillAppear and ViewDidAppear, but nothing works. Before my phone ran iOS 8, everything worked fine.
you should add the [session startRunning] in the main thread just like this:
dispatch_async(dispatch_get_main_queue(), ^{
if (![session isRunning]) {
[session startRunning];
}
});

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

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.

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

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];

Control non iPod music players (such as Spotify) from within iOS app

In my app I need to pause and restart background music while I play my own sound.
The approach for the iPod is to use:
[[MPMusicPlayerController iPodMusicPlayer] pause];
and :
[[MPMusicPlayerController iPodMusicPlayer] play];
For non iPod apps this has no affect.
Second attempt is to enable ducking:
UInt32 property = sender.on;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(property),&property);
AudioSessionSetActive(property);
Also works for iPod music player, not Spotify.
Setting kAudioSessionCategory_SoloAmbientSound does stop all music but doesn't restart it.
The way to control this is using AVAudioSession's setActive method.
When you want to play your sound, set your session to be active:
[[AVAudioSession sharedInstance] setActive:YES error:nil];
When you stop playing, then set your session to inactive but with the flag to notify others as well. This flag should cause the other app to restart (should work for iPod and others). For iOS6+ with AVAudioSession you could do this:
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersonDeactivation error:nil];
Pre iOS 6:
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:nil];
You don't need too. iOS only allows one application to be playing audio at a time and will automatically pause a music player so that you can play your music, as your app has music playing priority. Just play yours, and let iOS deal with it.

UIImagePickerController and application background modes

I’m building an application which supports both video playback and recording (not simultaneously, it’s just two separate features it provides). In order to make the videos play after the app enters background and gets back, I had to add an App plays audio item to Required background modes in the plist (I’m using MPMoviePlayerController for playback).
This, however, causes a problem with my video recording (I’m using UIImagePickerController for it). Basically, even after the picker is dismissed (either by the Cancel button or when it’s finished picking media), the app still keeps the audio recording session running.
If I remove the App plays audio item from the plist, the ImagePickerController’s audio session stops misbehaving, but then I can’t resume the playback of MPMoviePlayerViewController on switching to app from background mode.
Is there a way I can customise the handling of the audio session so that both the MPMoviePlayerController and UIImagePickerController can work properly?
yes, there is a way you can customize the handling of the audio session for what you desire: don't try to set the App plays audio setting.
instead, in your AppDelegate code (which is usually in AppDelegate.m from the simple wizard projects provided), supply code in the applicationWillResignActive: method that will stop the playback in your MPMoviePlayerController, and then use applicationDidBecomeActive: to resume the playback at the point at which you paused it if so desired.
this will not only allow the video to be resumed after a temporary pause, but it will allow you to save the state so that the video can be resumed in case the app is removed from memory or in case the user causes it to be quit.
You can scratch the background modes and instead use notifications to pause/resume your player. See UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification in the UIApplication class reference.
You can snag some code and see this implemented in this class. Here is some of the relevant code from that class:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_didBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_willResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
- (void) _didBecomeActive:(NSNotification*)notification {
if (_wasPlaying) {
[_movieController play];
}
}
- (void) _willResignActive:(NSNotification*)notification {
_wasPlaying = _movieController.currentPlaybackRate != 0.0;
if (_wasPlaying) {
[_movieController pause];
}
}