video clip no longer plays (autorelease issue) - xcode4

I took care of a memory leak related to the alloc object, however I think I have fouled up my code as now my video clip does not play. I believe that I have caused the device to release before the clip starts. Could someone help me to rearrange my code? I would be greatly appreciative of a few hints to get playback working again. Here is a sample of what I am working with.
#implementation ELECTRIC_GROOVEYViewController
-(IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"1960" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController =
[[[MPMoviePlayerController alloc] initWithContentURL:fileURL]autorelease];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// no moviecontrolls
moviePlayerController.controlStyle = MPMovieControlStyleNone;
// looping forever
moviePlayerController.repeatMode= MPMovieRepeatModeOne;
[moviePlayerController play];
}

You are autoreleaseing the moviePlayerController object. It has no further retention. Hence the object must be getting deallocated. Maintain a reference to it by creating an instance variable and release it after you are done with it.

Related

useLayoutToLayoutNavigationTransitions in iOS7 crash

I am building an iOS7 app and I am trying to make use of the new useLayoutToLayoutNavigationTransitions effect. I have 2 UICollectionViewControllers and when I do the following I get the transition effect
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondVC.useLayoutToLayoutNavigationTransitions = YES;
[self.navigationController pushViewController:secondVC animated:YES];
this works fine but what I want to do is make an api call and then in the completion block I want to push onto the nav stack like so
[webAPI getDetailsWithParams:params andCompletionBlock:^(id dict) {
//some work on the result
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondVC.useLayoutToLayoutNavigationTransitions = YES;
[self.navigationController pushViewController:secondVC animated:YES];
} andErrorBlock:^(NSError *error) {
}];
but this crashes every time with the following msg
-[UICollectionView _invalidateLayoutWithContext:]: message sent to deallocated instance 0x17a26400
can anyone tell me what I am doing wrong in this case? How can I get the transition effect when pushing from completion block?
EDIT: by changing it to the following I was able to transition to the second viewcontroller.
MyLayout *layout = [[MyLayout alloc] init];
SecondViewController *expandedVC = [[SecondViewController alloc] initWithCollectionViewLayout:layout];
and I also deleted the nib file that went with the file. nib file just consisted of a collection view and it was the file owners view.
While I can now transition I still do not understand why I could not do the previous nib method with in a block. So I would be grateful if someone could shed some light on it for me.
In order to use UICollectionViewController's useLayoutToLayoutNavigationTransitions to make transitions, the layout of the SecondViewController must be known. However, if you use initWithNibName:bundle: initializer, layout is not internally prepared yet, making your desired transitions impossible. As you mentioned in your edited question, you have to use [UICollectionViewController initWithCollectionViewLayout:] to initialize your second UICollectionViewController. Since your xib file has the same name as your class name, SecondViewController.xib is going to be loaded automatically by UIViewController, superclass of UICollectionViewController.
I think you were making UIKit calls from a thread that wasn't the main thread

UINavigationController pushViewController memory management

I have a code like this:
MyViewController *myController = [[MyViewController alloc] init];
[self.myNavController pushViewController:myController animated:YES];
[myController release];
In above case, deallc of MyViewController gets called twice resulting in a crash.
If I remove the last line "[myController release];" everything is fine.
Isn't this against the memory management guidelines?
if i understand truly, you must use initWithNibName against init like below:
MyViewController *myController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
Please try this and reply, best regards.
Are you sure that the MyViewController's dealloc method not release someObj more than once?
You can try to clear away dealloc content,then,run the app again.

.caf file in app not playing in iPod

In my app, i have some .caf audio file (Format: IMA 4:1, Stereo, 44.100 kHz). The problem what i m facing is that these audio file plays on button action (one button for each ringtone).They are playing fine on iphone(ios version 3 to 5 ) but im not able to listen it on ipod. I have done all the sound setting changes as solution described on other site but nothing happens.
Also ipod not stuck in headphone mode(this problem happens sometime when we plug out headphone while any audio file is playing).So please give any solution as soon as possible.
first You import framework then use below code:
#import <AudioToolbox/AudioToolbox.h>
call whenever you want by this method:
[self BackgroundSound:#"sounf file name"];//must have in resource(.wav,.mp3,.caf)
write this function by above method for call that :
- (void)BackgroundSound:(NSString *) name
{
NSString *spath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
AVAudioPlayer* BackgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:spath] error:nil];
BackgroundPlayer.numberOfLoops = -1; // set to -1 to loop repeated
BackgroundPlayer.volume = 0.5; // value is from 0 to 1.0 (float)
[BackgroundPlayer play]; // play
}
work in iPad/iPhone both in my application. i hope ,it's help you also.

AVAudioPlayer EXC_BAD_ACCESS

I am trying to pause the AVAudioPlayer if it is currently playing. When I debug my code and check AVAudioPlayer, I see it is allocated. When I try to access a method/property on it (i.e. myAudioPlayer isPlaying), I get an EXC_BAD_ACCESS error. This happens if the AVAudioPlayer does not have a sound loaded. Is there a way I can do a check to see if it has loaded a sound? I tried accessing myAudioPlayer.data, but I still get the same error.
For me it was because I wasn't calling the one of the designated initialisers. I was instantiating it with AVAudioPlayer() instead of the designated initialisers which are public init(contentsOfURL url: NSURL) throws and public init(data: NSData) throws
As of iOS 13, make sure you are removing the initialization on AVAudioPlayer before asigning it with AVAudioPlayer(contentsOf: URL(...))
i.e. change
var audioPlayer = AudioPlayer() to var audioPlayer: AVAudioPlayer!
I guess you have to use prepareToPlay method to find whether it has loaded or not.
First you have to add AVFoundation Framework.Then,import AVFounadtion framework in .h file.
.h:
AVAudioPlayer *audioPlayer;
.m:
NSURL *url1 = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audio1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
//audioPlayer.numberOfLoops = 0;
[audioPlayer play];
Try this code it may help you..

Hiding controls overlay in mediaplayer framework, sdk4 iphone xcode

I am building a simple & basic video app using the media player framework.
How do I hide the player control overlay in SDK4, xcode so that the user does not have access to play, pause, ff, rw, etc.?
The codes I have implemented simply do not work and either give errors or do nothing at all.
I am stumped and have not been able to find support via research. I also need to take care of a memory leak later.
I've tried:
-(IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"1960" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Video1" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
moviePlayerController.controlStyle=MPMovieControlStyleNone;
try
yourPlayer.controlStyle = MPMovieControlStyleNone;
and change yourPlayer to your name of the player.

Resources