MPMoviePlayerController Background playing - objective-c

Okay guys i have a problem. I'm stream MPMoviePlayerController and i want it to play audio in background and i've somewhat achieved this.
This is what i do in my -didFinishLaunchingWithOptions:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
Now whenever the application calls -applicationWillResignActive:
i do a post notification to continue the playback. And this works but it's an ugly fix. As there is a second delay between the sound stopping and the notification being called. So the playback stops for a second and then continues again by calling the notification which just says [viewPlayer play];
And many other have achieved smooth background playback. Like spotify or other apps whenever you enter background mode there is no sound lag/clipping in sound. This is really annoying to listen to whenever i press the home button or lock the phone.
Yes i did set the background mode for playback.
I have also tried -applicationDidEnterBackground: but this notification is even slower. It comes after -applicationWillResignActive:
I have no idea how to fix this, and or how others achieved it. I have looked through almost all other similar questions. None have my problem.
Thanks in advance.

I've recently used a framework to stream YouTube video inline in a UIView. This framework has a category on MPMoviePlayerController which works pretty well. You notice a change in the music when going to background but it is still acceptable.
The category can be found here:
MPMoviePlayerController+BackgroundPlayback.h
MPMoviePlayerController+BackgroundPlayback.m

Related

objective c iphone musicplayer crashing

I am creating a non appstore jailbreak tweak and I came across a problem that causes my app to crash.
I am using musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
the music player has complete functionality, play/pause, next, previous. it displays the title of the song, the artist, the artwork and everything.
the problem seems to be the loading and unloading of the musicplayer.
for example, if i am running the app, and i close the iphone's Music app, it will cause my app to crash. also if i load the app without the Music app running in the background it will crash.
as long as the music app is running in the background my app will not crash.
can anyone help me with this?
is something to be set to nil? or how do i go about releasing them?
should they be synthesize/property?
I was having this problem also for my tweak, and I couldn't figure out a way to fix it. I ended up using the AVPlayer Method, It seems to work well for me. Granted I only need to play one song, so if you need to play a playlist of some sorts you are out of luck.
I used it like the following in the mediapickerclass:
NSURL *url = [[mediaItemCollection.items objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL]
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[player play];
If you need you need to reference it from somewhere else you can just write the url to a .plist

AVQueuePlayer playing items simultaneously rather than in sequence

I am trying to put together a simple app to play a pre-roll video followed by some content video.
Currently, I'm trying to use the AVQueuePlayer class to get this done. Unfortunately, it seems to want to play the videos properly in sequence.
For example, the pre-roll plays by itself for a few seconds, then (prior to the pre-roll being complete) it starts to try and play the content video. For a few seconds, the player seems to be at war with itself and switches back and forth between the two videos (neither playing very well at all). Finally, when the pre-roll is finished, the content video then plays the rest of the way normally.
I've looked through the documentation for the AVQueuePlayer and I don't see anything obvious that I'm missing.
My code is pretty basic:
AVPlayerItem *preRollItem = [AVPlayerItem playerItemWithURL: preRollUrl];
AVPlayerItem *contentItem = [AVPlayerItem playerItemWithURL: contentUrl];
self.player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:preRollItem, contentItem, nil]];
[self.player play];
What is the trick to getting the videos to play in sequence.
Make sure you are actually testing on the device. From my experience the iOS5 simulator has big problems with AVQueuePlayer and does bizarre things.
I found the iOS4.3 simulator is doing a much better job when it comes to testing AVFoundation.

How do you know when a user chooses to play a video through AirPlay?

I have a custom video player set up with custom controls, and I utilize MPVolumeView to provide an airplay button. When a user chooses to use AirPlay, they interact with that Apple UI and there is no event (that I can find) that says "hey, the video is now playing over AirPlay".
The problem is that, if I close the player and reopen it, it loads the movie (load state changes to MPMovieLoadStatePlayable), I play it, and I immediately get a playback did finish notification with reason being MPMovieFinishReasonPlaybackEnded, and the video continues to try to play through AirPlay. I'm certain the movie stops and is deallocated whenever I close the player.
If anyone has any advice on how to handle this, knows some events to listen for, or has any ideas about this whatsoever, please let me know. Thanks!
The answer here turns out to be that, at least up to 4.3, there is no way to get an answer to this through code.
The problem in this case is how you dispose of the MPMoviePlayerController when you're finished with it. Even if the video plays through, before you finally release it, you have to call pause and then stop. Like this:
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] init];
// use the player. then when done with it:
[mp pause];
[mp stop];
[mp release];
If you don't do this then the next time you create a MPMoviePlayerController, certain properties are somehow ghosted in the framework. Playing a video progressively caused audio from the previous mp to play while the new mp did its initial buffering. Also, if the previous video was playing over airplay, the next video would get a notification that the video finished right after it starts and some other weirdness.
Long story short, dispose of your video players with the above sequence to avoid issues with later movie players.

Stopping the background music at another UIViewController

This may be a basic question but I'm a newby at IOS development..
I need a background music for my iPad application and I need to stop it at another UIViewController. I Start my background music just like that at my MainMenuViewController.m file
NSString* pathToBackGroundMusic = [[NSBundle mainBundle] pathForResource:#"MenuBackGround" ofType:#"mp3"];
backGroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToBackGroundMusic] error:nil];
backGroundMusic.numberOfLoops = -1;
[backGroundMusic play];
Now i need to stop it when I press a button at an another UIVIewController CustomGameMenuViewController.m... I'm sorry if it is already answered but i couldn't find it... Any help will be appreciated. Thanks.
There are a couple of ways to do this. The simplest way would be to use NSNotifications. Look up the documentation on NSNotification and NSNotificationCenter. With this method the original view controller would register to listen for a certain notification and your other controller would send that notification when it needed to stop music.
Another route would be to set up the first view controller as a delegate or property of the second one. This is a little more involved and convoluted. If this is the only communication back and forth between these controllers I would go the notification route.

Can I disable UIPickerView scroll sound?

I want to disable the annoying clicks that the UIPickerView generates upon scrolling up and down. Is there a way to do this? I want to play short sounds for each item that the picker view lands upon. It gets ruined by the built in sound.
I understand that the picker sounds can be turned off globally by switching off the keyboard sounds in iPhone/iPod settings. But is there a way to programatically do this?
Any help will be much appreciated!
Thanks
I've been struggling with a UIPickerView sound issue, and even though it's only partially relevant to the original question, I'm posting the problem/solution here because this topic keeps coming up in my search results so I think anyone else in the same boat may end up here too…
I needed to initialize a UIPickerView to restore the currently selected row from saved data. Simple, right? In viewDidLoad, just call the selectRow:inComponent:animated method of UIPickerView:
[myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
This works as expected, but has a side effect that it generates a single "click" sound as if the user had scrolled the control. The click sound only occurs when running on a device (not the simulator), and only if the device has iOS 3.x installed (I tested with 3.1.3 and 3.2). This was apparently a bug in iOS that was fixed starting with iOS 4.0. But if you need to target Gen1 iPhone, you're stuck with iOS 3.1.3 where this problem is present.
I discussed the issue with Apple DTS, but they were unable to suggest any workaround other than upgrading to 4.0. I asked if they would make an exception and permit the use of the undocumented setSoundsEnabled mentioned above (which does actually solve the problem). The answer was, "There are no exceptions."
After some additional detective work, I discovered that you can prevent the sound from occurring by temporarily removing the UIPickerView from the superview, call selectRow, then re-add it to the superview. For example, in viewDidLoad:
UIView *superview = [myPicker superview];
[myPicker removeFromSuperview];
[myPicker reloadAllComponents];
[myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
[superview addSubview:myPicker];
This gets rid of the extraneous click sound without using undocumented/private APIs so should pass Apple's approval process.
After using this specific undocumented api for over a year on the App Store Apple finally asked me to remove it from my App. It is very frustrating for audio apps to have that damn click sound. The best advice is to share with users that the picker sound can be disabled globally in the settings application under "Sounds" and setting "Keyboard Clicks" to "Off". I also strongly recommend visiting https://bugreport.apple.com/ and filing a bug for UIPickerView, as it can cause distortion in audio applications when the picker click is played.
they have just rejected an app of mine because the use of undocumented api's...thats one of them.
Someone I know says he got this past the App Store review just last week:
// Hide private API call from Apple static analyzer
SEL sse = NSSelectorFromString([NSString stringWithFormat:#"%#%#%#", #"set",#"Sounds",#"Enabled:"]);
if ([UIPickerView instancesRespondToSelector:sse]) {
IMP sseimp = [UIPickerView instanceMethodForSelector:sse];
sseimp(self.thePicker, sse, NO);
}
There is an undocumented way (I'm actually not sure if it is still available in iphone 3.0) but here it is any way
#import <UIKit/UIKit.h>
#interface SilintUIPickerView: UIPickerView
{ }
- (void) setSoundsEnabled: (BOOL) enabled;
#end
use this subclass instead and call [view setSoundsEnabled: NO]
I'm interested in knowing how it goes in the latest SDK, give it a shot and let us know.
Could this trick work? Someone was able to suppress the camera shutter sound effect by playing an inverted copy of the sound at the same moment: https://stackoverflow.com/a/23758876/214070
Maybe this not the answer for this particular question, but I had a similar problem - set minimumDate for datePicker, and I wanted set it without annoying "click" sound. After some time found very simple solution:
datePickerCustomTime.minimumDate = [[NSDate date] dateByAddingTimeInterval:300]// min time to set = now + 5 min
[datePickerCustomTime setDate:[[NSDate date] dateByAddingTimeInterval:300] animated:NO];
I found small quickie solution for this try below
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, yPickerView, VIEW_WIDTH, PICKERVIEW_HEIGHT)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
pickerView.alpha = 0.8f;
pickerView.tag = fieldTag;
[pickerView selectRow:pickerViewSelectedIndex inComponent:0 animated:NO];
set the animated:NO for selectRow: method