i want to play music from ipod, does anyone know how to choose music to play…
i know in cococs2d-object, i find the code, but the code can not use in cocos2d-x…
any suggestions are appericated
here is object-c code:
// Import the interfaces
#import "HelloWorldLayer.h"
@interface HelloWorldLayer (Private)
-(void)showMusicPicker;
@end
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[musicPlayer retain];
[self showMusicPicker];
}
return self;
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
if (mediaItemCollection) {
[musicPlayer setQueueWithItemCollection: mediaItemCollection];
[musicPlayer play];
}
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque animated:YES];
[musicController_ dismissModalViewControllerAnimated: NO];
//[musicController_ release];
[[CCDirector sharedDirector] resume];
//[[CCDirector sharedDirector] setLandscape: YES];
[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationPortrait];
NSLog(@"mediaPicker\n");
}
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[musicController_ dismissModalViewControllerAnimated: YES];
NSLog(@"mediaPickerDidCancel\n");
}
- (void) showMusicPicker
{
//[[CCDirector sharedDirector] pause];
musicController_ = [[UIViewController alloc] init];
[musicController_ setView:[[CCDirector sharedDirector] openGLView]];
[musicController_ setModalTransitionStyle: UIModalTransitionStyleCoverVertical];
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
//picker.prompt = @"Select songs to play";
// The media item picker uses the default UI style, so it needs a default-style
// status bar to match it visually
[[UIApplication sharedApplication] setStatusBarHidden:NO animated: YES];
// Dismiss the media item picker.
[musicController_ dismissModalViewControllerAnimated: YES];
// Apply the chosen songs to the music player's queue.
//[self updatePlayerQueueWithMediaCollection: mediaItemCollection];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated: NO];
//[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft];
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown];
[musicController_ presentModalViewController: picker animated: YES];
[picker release];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
[musicPlayer release];
}
@end