I am trying to make an app that plays MP3 songs. My program flow is like this:
I have an XML file here in my website: http://jeewanaryal.web44.net/SongsXML/songsListXML.xml. It has song titles and the actual URL of those songs on the Internet. I have the following code for my Pop songs section:
NSString *urlString = @"http://jeewanaryal.web44.net/SongsXML/songsListXML.xml";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
NSDictionary *dict = [XMLReader dictionaryForXMLData:data error:nil];
NSArray *arrayOfSongs = [[NSArray alloc] initWithArray:[[[dict objectForKey:@"list"] objectForKey:@"songs"] objectForKey:@"song"]];
StartStopSound = [UIButton buttonWithType:UIButtonTypeRoundedRect];
StartStopSound.frame = CGRectMake(60, 360, 200, 30);
[StartStopSound setTitle:@"बजाउनुहोस" forState:UIControlStateNormal];
[StartStopSound addTarget:self action:@selector(playSong) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:StartStopSound];
back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
back.frame = CGRectMake(20, 320, 100, 30);
[back setTitle:@"पछाडीको गीत " forState:UIControlStateNormal];
[back addTarget:self action:@selector(playSong) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
next = [UIButton buttonWithType:UIButtonTypeRoundedRect];
next.frame = CGRectMake(200, 320, 100, 30);
[next setTitle:@"अगाडीको गीत" forState:UIControlStateNormal];
[next addTarget:self action:@selector(playSong) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:next];
NSData *myData = [[NSData alloc] init ];
NSString *urlString1 = [[NSString alloc] init];
for (NSDictionary *fruitDict in arrayOfSongs) {
UILabel *songTitle = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 300, 40)];
songTitle.text = [NSString stringWithFormat:@"%@",[[fruitDict objectForKey:@"title"] objectForKey:@"text"]];
[self.view addSubview:songTitle];
urlString1 = [NSString stringWithFormat:@"%@",[[fruitDict objectForKey:@"url"] objectForKey:@"text"]];
//NSLog(@"\n\n -- URL STRING : %@ \n\n",urlString);
}
myData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString1]];
player = [[AVAudioPlayer alloc] initWithData:myData error:nil];
[player play];
[player numberOfLoops];
[player currentTime];
This works fine for a single song; that is, it only plays the last URL of my XML file. I want to make all songs to be played one by one, making a “Next” and “Previous” button on my app.
Will you please tell me how I can implement that? I am using XMLReader.h and XMLReader.m file to parse the data from XML into my app.
You would need to store arrayOfSongs somewhere in the class. The “back” and “next” buttons would need separate actions (e.g.
playPreviousSong,playNextSong). These methods would then look something like this:This is a crude implementation just to show how it’d fit together. Basically you need to store the list of songs and keep track of which song is “selected”/playing, then just select the next/previous one.
Edit: To answer your question in the comments, you have already extracted the array of song URLs, you simply need to keep a reference to it in the class. E.g.
Then in the
@implementationinstead of creating a arrayOfSongs variable, simply reference the array from the instance variable:Then in, for example, the playPreviousSong method: