how to use cocos2d-x to choose music to play from apple iPod,

how to use cocos2d-x to choose music to play from apple iPod,
0.0 0


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 :slight_smile:

here is object-c code:

// Import the interfaces
#import "HelloWorldLayer.h"

@interface HelloWorldLayer (Private)

// 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];

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
    [musicController_ dismissModalViewControllerAnimated: YES];


- (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];



You need to mix c*+ and objc together. Translate the game logics on top of cocos2d-iphone into c**, and left the source about MPMusicPlayerController, MPMediaPickerController to original objc code. Use .mm file to mix them.
But if you want it to work on android, it would be much more complicated. You should use JNI to bind java interface into c*+.


hi Walzer Wang, thank you so much I will give it one shot soon.