0

I'm a new developer and I'm getting to grips with audio in iOS.

This will be tricky to explain, but I'll give it a go. I'm using a media picker so that the user can select a song from their library. I don't want there to be a queue or for it to play immediately. Instead, I want it to appear in a tableview and when the user selects that song in the table view, it will play that when, in another view, a button is pressed.

So basically, I want the user to be able to select a song from their library and it is added to list. They can then selected a song from that list and It will play that when a button is pressed.

If it helps, this is the code I'm using for the media picker and to play audio (and they're not actually connected to each other):

Media Picker:

MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];

        [mediaPicker setDelegate:self];
        [mediaPicker setAllowsPickingMultipleItems:NO];

        mediaPicker.prompt = NSLocalizedString(@"text1", "text2");

        [self presentViewController:mediaPicker animated:YES completion:nil];

Playing Audio:

if (self.panicButtonPressed == NO) {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"Alarm 1"
                                     ofType:@"mp3"]];
alarmSound = [[AVAudioPlayer alloc]
               initWithContentsOfURL:url
               error:nil];
    [alarmSound play];
self.panicButtonPressed = YES;
}

Any help would be greatly appreciated.

Invalid Memory
  • 717
  • 7
  • 23

1 Answers1

0

Once you get the file URL you can use it to access the files metadata (title,artist,etc.). You can then populate your table with this data and the URL.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Thanks for your answer. Could you elaborate so as to show how I do that? I guess I would use the method `- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection` but I'm not sure what to put in it. Would it include something like this: `NSURL *url = [[mediaItemCollection.items objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];` – Invalid Memory Jul 10 '13 at 20:33
  • If you want the 'picked' song to appear in a table view then I think the easiest thing to do would be to store a reference to what they picked in an NSArray and then in your `tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath` method you'd pass the file metadata and URL. In addition to the Apple docs you might check out this brief overview of UITableViewController: http://blog.teamtreehouse.com/introduction-to-the-ios-uitableviewcontroller – Nick Jul 11 '13 at 14:07
  • Sorry for my late reply. Thanks for your help, how would I store the 'picked' song in an NSArray. I only know how to store string in an NSArray `self.booksArray = [NSArray arrayWithObjects:@"Brave new world"`. I understand what you're saying, but I'm unsure as to how to make it happen. I have already tried reading through the Apple Docs but I'm still trying to understand it. I had a look at the link you included. – Invalid Memory Jul 15 '13 at 11:03