QTmovie can't play video - qtkit

I'm cocoa beginner , maybe my question is a little stupid :(
I setup a rtsp server, build in localhost.
I use qtmovie connect to the localhost to play a video.
I have two buttons which can switch channel and channel 2 in my rtsp server.
It's fine every time when I click channel 1 and click play button, the qtmovieview controller bar will show negotiate->connecting->setup->playing.
After playing channel 1, when I click channel 2 and play button, the qtmovieview controllerbar only show "connecting", and nothing happen. But sometimes it works !!
Is there any memory allocation problem in the qtmovie? or am I missing something?
Below is part of my code.
(void)play:(id)sender{
NSLog(#"Play");
NSURL *url3=[NSURL URLWithString:#"rtsp://127.0.0.1:8554/"];
qtmovie=[QTMovie movieWithURL:url3 error:nil];
#try {
[_movieView setControllerVisible: YES];
[_movieView setMovie: qtmovie];
[qtmovie play];
[_movieView display];
}
#catch (NSException *exception) {
NSLog(#"%#",[exception name]);
}
}
-(IBAction)clickchannel1:(id)sender
{
NSTimer * timer=nil;
[_label setStringValue:#"Channel1 Playing"];
if(channel==2){
[qtmovie stop];
[_movieView setMovie:nil];
[timer invalidate];
timer = nil;
CloseChannel(channel);
}
channel = 1;
OpenChannel(channel);
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(showBufferLevel:) userInfo:nil repeats:YES];
}
-(IBAction)clickchannel2:(id)sender;
{
[testThread release];
NSTimer * timer=nil;
[_label setStringValue:#"Channel2 Playing"];
if(channel==1){
[_movieView setMovie:nil];
[timer invalidate];
timer = nil;
CloseChannel(channel);
}
//[qtmovie play];
channel = 2;
OpenChannel(channel);
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(showBufferLevel:) userInfo:nil repeats:YES];
}
Thank you for your help !!

Related

error when returning to game menu after game over [duplicate]

I'm threeaing some tasks like this :
RootViewController
- (void)viewDidLoad {
[NSThread detachNewThreadSelector:#selector(findSomething) toTarget:self withObject:nil];
}
- (void) findSomething {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
doMoreThings
[pool release];
}
- (void) doMoreThings {
doMoreMoreMoreThings on different objects
}
- (void) foundSomething:(NSFoundThing*)foundObj {
do your stuff
}
oneObject
- (void) doMoreMoreMoreThings {
do things
[self performSelectorOnMainThread:#selector(foundSomething:) withObject:thingFound waitUntilDone:NO];
}
gives
-[KMLParser foundSomething:]: unrecognized selector sent to instance 0x5888080
What is the problem ?
The threading is irrelevant. Some of the code you're not showing us is making it so you are sending the foundSomething: selector to an object that doesn't handle that message. Route the message to an object that does handle it, and your problem will go away.
See also "Unrecognized selector sent to instance".

NSTimer is not firing

I had everything working fine also the timers where running. But when i ran into weird problems i had to restructure my cocos2d scene.
Now i am not able to fire the NSTimers anymore, the BonusTimetimer below is not even fired once.
Thanks in advance, below is the code.
BonusTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(CountTimeBonus:) userInfo:nil repeats:YES];
// (lately i added the line below, but it does not help)
[[NSRunLoop mainRunLoop] addTimer:BonusTimeTimer forMode:NSDefaultRunLoopMode];
-(void)CountTimeBonus:(NSTimer *) sender {
NSLog (#"Method -> Countimebonus");
if ( (scoreTotal + 37) < TargetScore){
scoreTotal = scoreTotal + 37;
TimeBonus = TimeBonus-37;
NSString *level_timebonus = [NSString stringWithFormat:#"%d", TimeBonus];
[labelTimeBonus setString: level_timebonus];
NSString *scorestr = [NSString stringWithFormat:#"%d", scoreTotal];
[labelMainScore setString: scorestr];
[[SimpleAudioEngine sharedEngine] playEffect:#"light_switch_.mp3"];
}
else {
// add the Last few points and finish BonusTimer
scoreTotal = scoreTotal + TimeBonus;
TimeBonus=0;
NSString *level_timebonus = [NSString stringWithFormat:#"%d", timebonusgrayed];
[labelTimeBonus setString: #"" ];
[labelTimeBonusGrayed setString: level_timebonus];
NSString *scorestr = [NSString stringWithFormat:#"%d", scoreTotal];
[labelMainScore setString: scorestr];
[[SimpleAudioEngine sharedEngine] playEffect:#"light_switch_.mp3"] ;
[BonusTimeTimer invalidate];
BonusTimeTimer = nil;
timeBonusisdone = true;
timeBonusisactive = false;
}
}
I've never really drilled down that rabbit hole (ie why this is), but there is a persistent rumour around here that NSTimer and cocos2d do not mix well. Instead, I use cocos' own methods
[self schedule:#selector(CountTimeBonus:) interval:.01];
// and to invalidate this
[self unschedule:#selector(CountTimeBonus:)];
the CountTimeBonus signature will be :
-(void) CountTimeBonus:(ccTime) dt {
}
Thanks YvesLeBorg that worked for me.
There are a few more things to consider,
1) the cocos2d code:
[self schedule:#selector(myTimer:) delay:.01];
has a syntax problem, i used this instead (below):
[self schedule:#selector(myTimer:) interval:.01];
2) I got the NSTimer working again, but when i place the NStimer call in a do-while loop it won't work.
Thanks again and best of luck
Johan

run another animation after running an animation with ccbanimationmanager

I'm running cocos2d with cocosbuilder 2.1 and I use the cocosbuilder animation delegate (CCBAnimationManagerDelegate::completedAnimationSequenceNamed) to get notified when an animation has completed and take actions like firing another cocosbuilder animation.
It runs fine the first time the foodfactoryshow animation is run from the delegate and after the animation is completed it also runs restoration animation correctly. However, when restoration animation is completed, the parameter name for -(void) completedAnimationSequenceNamed method is NULL!?
-(void) completedAnimationSequenceNamed:(NSString*)name
{
if ([name isEqualToString:#"foodfactoryshow"])
{
[manager runAnimationsForSequenceNamed:#"restoration"];
}
if ([name isEqualToString:#"restoration"])
{
[self colorLayerChanged];
self.gameLayer.isLock = true;
}
}
Is this a bug or am I not supposed to run animations from the CCBAnimationManagerDelegate::completedAnimationSequenceNamed method!?
Thanks in advance for your help.
I believe it is a CCBReader bug. There is an open issue about it in the CocosBuilder github page (https://github.com/cocos2d/CocosBuilder/issues/121). It should be fixed in the latest version of CocosBuilder + CCBReader, however, if you want to use the 2.1 version you can change the "sequenceCompleted" method of CCBAnimationManager to the following:
- (void) sequenceCompleted
{
NSString *completedSequenceName = [runningSequence.name copy];
int nextSeqId = runningSequence.chainedSequenceId;
runningSequence = NULL;
if (nextSeqId != -1)
{
[self runAnimationsForSequenceId:nextSeqId tweenDuration:0];
}
[delegate completedAnimationSequenceNamed:completedSequenceName];
[completedSequenceName release];
}

cocos2d-iphone: init method does not work properly

I'm studying Strougo/Wenderlich tutorial (space Viking project). I got troubles with chapter 4.
In RadarDish.m:
-(void)initAnimations
{
[self setTransmittingAnim: [self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
}
-(void)changeState:(CharacterStates)newState {
[self stopAllActions];
id action = nil;
[self setCharacterState:newState];
switch (newState) {
.
.
case kStateIdle:
action = [CCAnimate actionWithAnimation:transmittingAnim
restoreOriginalFrame:NO];
break; }
if (action != nil) {
[self runAction:action];
}
}
-(id)init
{
self=[super init];
if (self!=nil) {
.
.
[self initAnimations];
.
.
}
return self;
}
Exact the same code as in the tutorial. Failure:
*** Assertion failure in -[CCAnimate initWithAnimation:], /Users/macowner/Documents/examples/SpaceViking/SpaceViking/libs/cocos2d/CCActionInterval.
Using debugger with breakpoints, i noticed that value of transmittingAnim = nil.
So, if i put line with
[self setTransmittingAnim:
[self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
into case of
-(void)changeState then animation works correctly.
Why [self initAnimations] from (id)init is not called?
Im using cocos2d v.2 templates.
Great thanks in advance.
I had problems because I have been building project using cocos 2d v.2.0, while tutorial is based on cocos 2d templates v.1.x.x If you are going to follow the book "Learning Cocos2D", I strongly recommend you loading cocos2d-iphone version 1.0.1. Here is the link download cocos2d 1.x.x branch
if you still want to use latest cocos2d templates, I can give you some advice:
Follow the instructions in this link cocos2d v2.0 migration guide
You are going to have a lot of deprecations and changes to fix, so use this link to understand how to fix those deprecations and changes.
Now few words about the solution of the problem I mentioned here. In each of the GameObjects, EnemyObjects, and PowerUps, I added a method to override initWithFrameName.
-(id) initWithSpriteFrameName:(NSString*)frameName{
if ((self=[super init])) {
if ((self = [super initWithSpriteFrameName:frameName])) {
CCLOG(#"### RadarDish initialized");
[self initAnimations]; // 1
characterHealth = 100.0f; // 2
gameObjectType = kEnemyTypeRadarDish; // 3
[self changeState:kStateSpawning]; // 4
}
}
return self;
}
    This allows the GameObject and GameCharacter init methods to run before the CCSprite's initWithSpriteFrameName method to run.
    
    The Viking GameObject had to have a slightly different solution because it is initialized with initWithSpriteFrame rather than initWithSpriteFrameName. However, the override implementation is basically the same as the example of RadarDish above.

Update text on CCLabelTFF end in bad access?

I'm doing a little game in Coco2D and I have a countdown clock Note: As I am just trying to fix a bug, I am not working on cleanup so the timer can stop, etc.
Here is my code I'm using to setup the label and start the timer:
timer = [CCLabelTTF labelWithString:#"10.0000" fontName:#"Helvetica" fontSize:20];
timerDisplay = timer;
timerDisplay.position = ccp(277,310);
[self addChild:timerDisplay];
timeLeft = 10;
timerObject = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateTimer) userInfo:nil repeats:YES];
Note: timeLeft is a double
This is updateTimers's code:
-(void)updateTimer {
NSLog(#"Got Called!");
timeLeft = timeLeft -0.1;
[timer setString:[NSString stringWithFormat:#"%f",timeLeft]];
timerDisplay = timer;
timerDisplay.position = ccp(277,310);
[self removeChild:timerDisplay cleanup:YES];
//[self addChild:timerDisplay];
if (timeLeft <= 0) {
[timerObject invalidate];
}
}
When I run this I toggle between crashing on this this:
[timer setString:[NSString stringWithFormat:#"%f",timeLeft]];
and in the green arrow thing it gives Thread 1: EXEC_BAD_ACCESS (code=2, address=0x8)
and
0x197a7ff: movl 16(%edi), %esi and in the green arrow thing it gives Thread 1: EXEC_BAD_ACCESS (code=2, address=0x8)
It looks like your "timer" (not a very good name for a text object IMHO) is being auto-released because it was initialized with a convenience method. So when your timer fires it is likely attempting to manipulate a freed object. There are two ways to fix it: a. alloc and initialize the object yourself, or b. add a "retain" message to it such as:
[[CCLabelTTF labelWithString:#"10.0000" fontName:#"Helvetica" fontSize:20] retain];
In both cases you will then be responsible to release the object in your dealloc or similar method when you are done with it:
[timer release];