Tejas Patel
Tejas Patel

Reputation: 870

How to select individual song using individual UIButton click using MPMediaPickerController

I have four UIButton to select songs from library using MPMediaPickerController. now what i want is, when i press UIButton1 then it should open picker for select song1. when i press UIButton2 then it should open picker for select song2. and same for UIButton3 and UIButton4 also.

where can i change to implement above mention things.

this is my current code.

-(IBAction)SelectSongClick:(id)sender
{
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
    [picker setDelegate:self];
    [picker setAllowsPickingMultipleItems: NO];

if((UIButton*)sender == btnSelect1)
{
    [playerView1 removeFromSuperview];
    [player pause];
    picker.title = @"1";
}
else if ((UIButton*)sender == btnSelect2)
{
    NSLog(@"select 2 selected");

    [playerView2 removeFromSuperview];
    [player pause];
    picker.title = @"2";
}
else if ((UIButton*)sender == btnSelect3)
{
    NSLog(@"select 3 selected");
    [playerView3 removeFromSuperview];
    [player pause];
    picker.title = @"3";
}
else
{
    NSLog(@"select 4 selected");
    [playerView4 removeFromSuperview];
    [player pause];
    picker.title = @"4";
}

[self presentViewController:picker animated:YES completion:NULL];

}

- (void)mediaPicker:(MPMediaPickerController *) mediaPicker didPickMediaItems:(MPMediaItemCollection *) collection { [self dismissViewControllerAnimated:YES completion:NULL]; MPMediaItem *item = [[collection items] objectAtIndex:0]; NSString *title = [item valueForProperty:MPMediaItemPropertyTitle]; url = [item valueForProperty:MPMediaItemPropertyAssetURL]; [self playURL:url withPickerTag:mediaPicker.title]; NSLog(@"URL 1 : %@",url); self.sendAudio = url; }

Upvotes: 2

Views: 185

Answers (1)

Juri Noga
Juri Noga

Reputation: 4391

If I have understood your question correctly then you want to change method: - (void)mediaPicker:(MPMediaPickerController *) mediaPicker didPickMediaItems:(MPMediaItemCollection *) collection so, that you would be able to get selected button index from it?

Create enum above your @interface ViewController:

typedef enum {
    WaveFormType_1, //you can name these enums as you want
    WaveFormType_2,
    WaveFormType_3,
    WaveFormType_4
}WaveFormType;

Then create a variable WaveFormType waveType; and assign value to it in -(IBAction)SelectSongClick:(id)sender depending on what button was clicked. Then use this value.

- (void)mediaPicker:(MPMediaPickerController *) mediaPicker didPickMediaItems:(MPMediaItemCollection *) collection
{   
    [self dismissViewControllerAnimated:YES completion:NULL];

    MPMediaItem *item = [[collection items] objectAtIndex:0];
    [self.playItem:item]
}

-(void)playItem:(MPMediaItem *)item{
     NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
     NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];

     if (self.waveType == WaveFormType_1){

         // play with the first waveform type
         // label.text = title ..

     }else if(self.waveType == WaveFormType_2){

         // play with the second waveform type
         // label.text = title ..
   }
   // an so on
}

Upvotes: 1

Related Questions