I have an application that shows a lot of videos. To load and play the file, I use the following code:
- (IBAction)playVideoooo {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL URLWithString:#"/UnioneDiCentro2011_Live.isml/manifest(format=m3u8-aapl)"]];
switch ( [self interfaceOrientation] ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
[[moviePlayerController view] setFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT)];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
[[moviePlayerController view] setFrame:CGRectMake(0, 0, L_WIDTH, L_HEIGHT)];
break;
}
[moviePlayerController prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil]; // Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[self view] addSubview:[moviePlayerController view]];
}
- (void)moviePlayerLoadStateChanged:(NSNotification*)notification {
// Unless state is unknown, start playback
if ([moviePlayerController loadState] != MPMovieLoadStateUnknown) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarOrientation:[self interfaceOrientation]
animated:YES];
[moviePlayerController play];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
switch ( [self interfaceOrientation] ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
[[moviePlayerController view] setFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT)];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
[[moviePlayerController view] setFrame:CGRectMake(0, 0, L_WIDTH, L_HEIGHT)];
break;
}
if ( [moviePlayerController isFullscreen] ) {
[moviePlayerController setFullscreen:NO];
}
}
Actually the system seems to works, but I have to push the button linked to "playVideooooo" two times, to let the notification work. If I move the [moviePlayerController play]; into the IBActions the video starts correctly. How should I get the Notifications work?
Question partially solved: the matter is not in the NSNotification but in PrepareToPlay.
Related
I want to update my nstextview with the data generated during nstask execution(Ipa Generation). But when i run my code to execute nstask, in the middle my nstask blocks my ui but the task continues to execute. At last when nstask terminates my ui starts working properly.
This is my code where i am running my nstask:
dispatch_queue_t taskQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
NSTask *task=[[NSTask alloc]init];
dispatch_async(taskQueue, ^{
#try {
[task setArguments:arguments];
[task setLaunchPath: launchPath];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(taskCompletion:) name: NSTaskDidTerminateNotification object:task];
// Output Handling
NSPipe *outputPipe = [[NSPipe alloc] init];
outputFileHandle = [[NSFileHandle alloc]init];
[task setStandardOutput:outputPipe];
outputFileHandle=[outputPipe fileHandleForReading];
[outputFileHandle waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:outputFileHandle queue:nil usingBlock:^(NSNotification *notification){
NSData *output = [outputFileHandle availableData];
NSString *outStr = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(taskCompletion:) name: NSTaskDidTerminateNotification object:task];
NSLog(#"%#",outStr);
NSString *textViewData =[self.outputView string];
self.outputView.string = [textViewData stringByAppendingString:[NSString stringWithFormat:#"\n%#", outStr]];
// Scroll to end of outputText field
NSRange range;
range = NSMakeRange([self.outputView.string length], 0);
[self.outputView scrollRangeToVisible:range];
});
[outputFileHandle waitForDataInBackgroundAndNotify];
}];
[task launch];
[task waitUntilExit];
}
#catch (NSException *exception) {
NSLog(#"Problem Running Task: %#", [exception description]);
}
#finally {
NSLog(#"i m in finally xbuild");
}
});
I really stuck in that .Your suggestions will be helpful for me.
I'm trying to build an app to play local music, but unfortunately, i'm unable to do audio playback in background in iOS5 with AVQueuePlayer.
In my ViewDidLoad, i got this code :
// Player setup
mAudioPlayer = [[AVQueuePlayer alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[mAudioPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0, 1) queue:NULL usingBlock:^(CMTime time) {
[self updatePositionOnDisplay];
}];
// Audio session setup
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
Here is my "playerItemDidReachEnd" method:
- (void)playerItemDidReachEnd:(NSNotification*)notification
{
NSLog(#"playerItemDidReachEnd");
UIBackgroundTaskIdentifier newTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:newTaskID];
}];
NSLog(#"playerItemDidReachEnd 2");
NSLog(#"searching next song");
mCurrentSong = [self getNextSongWithIsSwitched:NO];
if(mCurrentSong != nil){
NSLog(#"Start : %# - %#", mCurrentSong.artist, mCurrentSong.title);
mTapeTitle.text = [NSString stringWithFormat:#"%# - %#", mCurrentSong.artist, mCurrentSong.title];
AVPlayerItem* i = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:mCurrentSong.path]];
if(i != nil){
[mAudioPlayer insertItem:i afterItem:nil];
}else
NSLog(#"BING!! no AVPlayerItem created for song's path: %#", mCurrentSong.path);
[i release];
}else{
NSLog(#"no song found");
[mAudioPlayer pause];
isPlaying = NO;
[mPlayButton setSelected:NO];
}
[[UIApplication sharedApplication] endBackgroundTask:newTaskID];
newTaskID = UIBackgroundTaskInvalid;
}
When I start the playback, it works, and keep playing when i switch off the screen. BUT when the song is over, here are the logs
2012-03-01 10:00:27.342 DEMO[3096:707] playerItemDidReachEnd
2012-03-01 10:00:27.360 DEMO[3096:707] playerItemDidReachEnd 2
2012-03-01 10:00:27.363 DEMO[3096:707] searching next song
2012-03-01 10:00:27.381 DEMO[3096:707] Start : Moby - Ah-Ah
But no song start effectively...
Can anyone tell me what's wrong with my code ??
Thanks a lot.
try to comment next lines
[[UIApplication sharedApplication] endBackgroundTask:newTaskID];
newTaskID = UIBackgroundTaskInvalid;
if it works then you need to add an observer to a mAudioPlayer for "currentItem.status" when status AVPlayerStatusReadyToPlay then end background task
This might seem straightforward, but the following code does not work, because the "observeValueForKeyPath" function is never called, although I keep changing the text in the NSTextfield:
- (void)awakeFromNib
{
[myNSTextField addObserver:self forKeyPath:#"value" options:NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"observeValueForKeyPath");
}
The log message #"observeValueForKeyPath" is never printed. I tried observing the key #"stringValue", but this does not work neither...
Am I missing something ??
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(textFieldDIdChange:)
name:NSControlTextDidChangeNotification
object:self.textField];
[[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification
object:self.textView
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note){
NSLog(#"Text: %#", self.textView.textStorage.string);
}];
I have got this background thread that does a few things with core data objects. I get the context as follows:
- (id)_managedObjectContextForThread;
{
NSManagedObjectContext * newContext = [[[NSThread currentThread] threadDictionary] valueForKey:#"managedObjectContext"];
if(newContext) return newContext;
newContext = [[NSManagedObjectContext alloc] init];
[newContext setPersistentStoreCoordinator:[[[NSApplication sharedApplication] delegate] persistentStoreCoordinator]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_mergeChangesFromManagedObjectContext:)
name:NSManagedObjectContextDidSaveNotification
object:newContext];
[[[NSThread currentThread] threadDictionary] setValue:newContext forKey:#"managedObjectContext"];
return newContext;
}
then I fetch some objects, modify them and save the context:
- (void) saveContext:(NSManagedObjectContext*)context {
NSError *error = nil;
if (![context save:&error]) {
[[NSApplication sharedApplication] presentError:error];
}
}
- (void)_mergeChangesFromManagedObjectContext:(NSNotification*)notification;
{
[[[[NSApplication sharedApplication] delegate] managedObjectContext] performSelectorOnMainThread:#selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}
.. later I remove the observer. This works for the main part. But some properties don't get updated when they get merged back. The properties that were nil before get updated. The ones that had a value stay the same.
I tried:
[newContext setMergePolicy:NSOverwriteMergePolicy];
... (and the other merge policies) on the main context but it did not work :P
Thank you for your help.
Note: I have bound the values to a NSTableView. I log them after the merge. The values properties that were nil seem to work fine.
How are you registering both contexts for notifications? You need to do something like this:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(backgroundContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:backgroundMOC];
[nc addObserver:self
selector:#selector(mainContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:mainMOC];
And implement the callbacks:
// merge changes in background thread if main context changes
- (void)mainContextDidSave:(NSNotification *)notification
{
SEL selector = #selector(mergeChangesFromContextDidSaveNotification:);
[backgroundMOC performSelector:selector onThread:background_thread withObject:notification waitUntilDone:NO];
}
// merge changes in main thread if background context changes
- (void)backgroundContextDidSave:(NSNotification *)notification
{
if ([NSThread isMainThread]) {
[mainMOC mergeChangesFromContextDidSaveNotification:notification];
}
else {
[self performSelectorOnMainThread:#selector(backgroundContextDidSave:) withObject:notification waitUntilDone:NO];
}
}
I am implementing an Audio based application. In that I am playing two different sounds using two AVPlayers. I need to do different actions once the sounds played. For this I used NSNotifications. But my problem is I am not able to find the Notifications related to which player. My notifications code and selector code is as follows please any one suggest me what the mistake I did.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playingItemDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:iPodPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playingItemDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:applicationPlayer ];
- (void)playingItemDidEnd:(NSNotification *)notification
{
id object= [notification object];
if(object==ipodPlayer)
{
printf("\n Notification from iPod Player ");
}
else if(object==applicationPlayer)
{
printf("\n Notification from application Player ");
}
}
Thanks in advance,
Chandra.
I need to change the code base as follows,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playingItemDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[applicationPlayer currentItem] ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playingItemDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[iPodPlayer currentItem]];
And selector code should be as follows,
- (void)playingItemDidEnd:(NSNotification *)notification
{
AVPlayerItem* object= [notification object];
if(object==[applicationPlayer currentItem])
{
}
else if(object==[avPlayer currentItem])
{
}
}