0

I am confused as to how to play a list of songs back locally. I'm trying to build an app that allows the user to select a song from a list, then continue playing that list where they left off. Unless they select a different song, then it plays from that song forward.

I have read and tried multiple tutorials on how to play audio files using AVFoundation, but they only seem to let me be able to play one sound.

I have tried MPMusicPlayer, but that won't work because I only want to play the files that come with the app, not from the user's music library.

Here's what I have so far from that tutorial:

iPhone Music Player

I feel stuck and confused as to how to play the songs locally in the list. How do I build this?

ingh.am
  • 25,981
  • 43
  • 130
  • 177
Courtney Stephenson
  • 912
  • 2
  • 18
  • 43

1 Answers1

1

You should probably look into using a UITableView before attempting an application that requires this.

I've written this from memory so please test it and confirm it all works...

Make sure your view controller implements the methods from the table view delegates, and declare a UITableView obj and a array like so:

@interface YourTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
  IBOutlet UITableView *theTableView;
  NSMutableArray *theArray;
}

Make sure you link them in your storyboard. You should see theTableView as defined above.

When you application loads, write this (somewhere like viewDidLoad would be fine):

theArray = [[NSMutableArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", nil];

You do not need to declare how many sections there are in your table view, so for now ignore this until later. You should however, declare how many rows there are:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [theArray count]; // Return a row for each item in the array
}

Now we need to draw the UITableViewCell. For simplicity we will use the default one, but you can make your own quite easily.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // This ref is used to reuse the cell.
  NSString *cellIdentifier = @"ACellIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

  if(cell == nil)
  { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }

  // Set the cell text to the array object text
  cell.textLabel.text = [theArray objectAtIndex:indexPath.row];

  return cell; 
}

Once you have the table displaying the track names, you can use the method:

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(indexPath.row == 0)
  {
    NSString *arrayItemString = [theArray objectAtIndex:indexPath.row];
    // Code to play music goes here...
  }
}

In the NSMutableArray we declared at the top, you do not have to add NSString's to the array. You can make your own object if you want to store multiple strings for example. Just remember to modify where you call the array item.

Finally, to play the audio, try using the answer in this SO answer.

Also, while it's not necessary, you could use a SQLite database to store the tracks you wish to play in a list rather than hard coding the list. Then fill the NSMuatableArray after calling the database.

Community
  • 1
  • 1
ingh.am
  • 25,981
  • 43
  • 130
  • 177
  • I did everything you listed, but nothing is showing up in the table view. Do I need to link anything else in the storyboard? – Courtney Stephenson Feb 27 '13 at 15:24
  • You need to link the `UITableView` to `theTableView` and you need to setup the `UITableView` delegates (`UITableViewDataSource` and `UITableViewDelegate`) in your storyboard. – ingh.am Feb 27 '13 at 16:15