My program is working fine when it is in the foreground, but whenever it goes to background it is stops working.
#musicPlayer = MPMusicPlayerController.iPodMusicPlayer
I am using "applicationMusicPlayer", not "iPodMusicPlayer". Why doesn't it work when it's in the background?
The applicationMusicPlayer doesn't support play music on the background. You can use AVAudioPlayer to play music. In this case you should manage Audio Session for you application.
An example how can it look on the Objective-C ...
- (void)playMusic {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"music" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];
}
Related
I have developed a game in cocos2dx for iOS.Now i integrated banner view for show admob banner ad.
i write code for admob in appController class like that.
NSLog(#"ADMOB");
CGSize winSize = [[CCDirector sharedDirector]winSize];
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(size.width/2-160,
size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
bannerView_.adUnitID =#"a*******";
bannerView_.delegate=self;
[viewController.view bannerView_];
bannerView_.rootViewController = viewController;
[bannerView_ loadRequest:[GADRequest request]];
GADRequest request = [[GADRequest alloc] init];
request.testing = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID, nil];
[bannerView_ loadRequest:request];
Now I have to hide or remove admob banner ad from cpp class. So I have to call C++ class to objective. C++ call and then call to appcontroller class. and suppose to de remove banner view like that
[bannerView removeFromSuperview];
[bannerView setDelegate:nil];
[bannerView release];
bannerView = nil;
but banner view not removing from that call.
please help me to getting out from this issues.
any help will be appreciated.
Is your ad getting displayed properly,
since this line does nothing:-
[viewController.view bannerView_];
Shouldn't be this like
[viewController.view addSubview:bannerView_];
right ive got this code below which will save a integer when the save button is clicked and the app is shut however i need it to do this automatically... how do I go about doing this with ios? quite new to the whole apple coding thing so any help is welcome
-(IBAction)SaveNumber:(id)sender{
[[NSUserDefaults standardUserDefaults] setInteger:Number forKey:#"savenumber"];
}
- (void)viewDidLoad {
Number = [[NSUserDefaults standardUserDefaults] integerForKey:#"savenumber"];
ShowSavedNumber.Text = [NSString stringWithFormat:#"%i",Number];
[super viewDidLoad];
}
The problem is, where you try to save this value? you don't have a file to storage this value in Documents directory inside the App.
You can save any value in your app, but you have to use a file for storage and read, or saving and load via URL on your server. Like file.plist, SqlLight, json with MySql.
So the first simple saving is using a file .plist inside your app, under the AppDelegate.m you have to check your directory if the file exist and copy inside a Documents directory (the only writable folder), but after that you have to learn how to load and save on the same file using Dictionary , MutableDictionary and some other class. I can help you to understant the first steps like a check and create a .plist, read, and first base to save, but for other thing is to much code and yopu have to watch other tutorials.
So create a .plist file and change the structure like this:
Change key has you want, but remember the Dictionary contain more string value, if you want add more Item you have to add other row inside the same Root Array.
Root = Array
Item0 > some string with value
Item1 > some string with value
Now you have to copy this file inside a writable Documents folder when the application lauching, so inside your AppDelegate.m create a function:
- (void)createPlistFile{
NSError *error;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"yourFile.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
}
Under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { add a call: [self createPlistFile];
Ok now you have Check first if the file exist in documents folder, and if is false, the app create a new file.
Now here the standard code to read a new file inside your app:
in your ViewControlle.h write:
#interface ViewController : UIViewController {
NSMutableArray *loadData;
}
#property (nonatomic, strong) NSMutableArray *loadData;
Come back to your ViewController.m and write:
#synthesize loadData;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:#"yourFile.plist"];
NSMutableArray *dataDict = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
self.loadData = dataDict;
Ok now you have create a new PLIST file inside a Documents Directory an correctly load, but now you have to save, this is a basic method to save, but theres really many mode for that and I can't write all here ;)
- (IBAction)save:(id)sender {
NSMutableDictionary *nr2 = [[NSMutableDictionary alloc] init];
[nr2 setValue:emittente.text forKey:#"plistKey"];
[nr2 setValue:link.text forKey:#"plistKey"];
[nr2 setValue:images.text forKey:#"plistKey"];
[nr2 setValue:web.text forKey:#"plistKey"];
[YouMutableDict addObject:nr2];
[YouMutableDict writeToFile:listFav atomically:YES];
}
Ok that's all I hope this help you to start understand how to save Data on app if yes please vote ;)
Changes made to NSUserDefaults are not automatically saved. You must call -synchronize on the NSUserDefaults object in order to save. Generally you want to do this once after you have made all of the updates you plan on performing (if in fact you need to do multiple updates).
Here is Apple's reference: http://developer.apple.com/library/ios/ipad/#documentation/cocoa/reference/foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
I've created a simple XML-Parser to parse my rss Feeds to my app.
The id, currentstatus, picture of the simple to parse from the xml file.
But I can't get the image from the XML file. . i retrieve the all the images from xml file. but the noimage.jpg not displaying. could you please any help me.
XML code
<Agent>
<id>3422</id>
<currentstatus>Logged Off</currentstatus>
<picture>1</picture>
</Agent>
<Agent>
<id>3432</id>
<currentstatus>Logged Off</currentstatus>
<picture>0</picture>
</Agent>
i tried the following code:
UIImage * cellimages = [[UIImage alloc]init];
NSString *string1 = #"http://telecomstats.co.uk/images/LLReaderProfile/";
NSString *cellvalue =[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]picture];
NSString* disable = [[NSString alloc] initWithString:#"0"];
if (cellvalue == disable)
{
cellimages = [UIImage imageWithContentsOfFile:#"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"];
}
else {
cellimages = [string1 stringByAppendingString:[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]id]];
cellimages = [cellimages stringByAppendingString: #".jpg"];
}
cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]];
cell.imageView.image = cellimages;
Does someone can help me?
Thanks for any help and insight.
You're using cellimages which is an UIImage object to store strings in your else-branch. Also when cellvalue == disable is true, you execute this:
cellimages = [UIImage imageWithContentsOfFile:#"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"];
cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]];
There you put an UIImage in [NSURL URLWithString:] which probably will not work; actually this code should produce quite some warnings. Try to properly separate between the URL string and the image object to display. Put all URL stuff in an NSString and get the UIImage afterwards when you have determined the correct URL.
Note that your code has some other problems, especially that you are using dataWithContentsOfURL and imageWirhContentsOfFile both of which will block until the image is retriebes. This will result in application hangs when using a bad internet connection and if the network is too slow, will crash your app. Have a look into asynchronous downloads and using frameworks like AFNetworking.
Okay, I've been trying to display the Game Center leaderboards in my Cocos2d iPhone game.
I have progressed and I got this piece of code:
- (void) showLeaderboard {
tempVC=[[UIViewController alloc] init];
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:leaderboardController animated: YES];
}
}
When I run that in the simulator, I see it becomes portrait mode, so I know something happened. BUT nothing appears. No leaderboards come out. What is wrong?
My application is already linked with the iTunes thingy.
I am authenticated at the beginning of the game sucessfully.
I already submitted scores (with a nice thing called GameKitHelper)
Go to the AppDelegate.m and change:
[window addSubview: viewController.view];
to
window.rootViewController = viewController;
then call it
GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper showLeaderboard];
This works on cocos2d 1.0.0 RC1
I want to play mp3 files on my server with AVAudioPlayer
I've tried this code, but it does not work.
-(IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"http://www.domain.com/audio/test.mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
NSBundle can only select the files in the apps bundle, not the ones on the internet.
Make this the path
NSString *path = #"http://www.domain.com/audio/test.mp3";
and leave everything else the same and it should work
try this:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audio play];