2015-05-28 52 views
0

当前对象的索引我有一个名为poemcollection如何知道有名字

poemcollection = [[NSMutableArray alloc] initWithObjects: 
        [[NSBundle mainBundle] pathForResource:@"twinkle_twinkle" ofType:@"mp3"], 
        [[NSBundle mainBundle] pathForResource:@"baa_baa_black_sheep" ofType:@"mp3"], 
        [[NSBundle mainBundle] pathForResource:@"johny_johny" ofType:@"mp3"], 

按钮点击 我想知道哪些对象是从这个数组和索引此对象的当前正在播放的阵列。 我想在日志打印此二者的名称和当前文件在播放器中播放

+0

NSString * mediaFileName = [[self.player.url absoluteString] lastPathComponent]; – dilip

+0

我已经试过这段代码获取当前mp3.it的作品的作品,但我怎么知道从poemcollection数组这个当前mp3的索引? – dilip

+0

为什么不循环遍历数组,并将每个数组对象的文件名与路径中的文件名进行比较?如果有一场比赛,你有你的索引。 – Zhang

回答

0
[poemcollection indexOfObject:YOUR_CURRENT_PLAYING_OBJECT]; 
0

像这样的事情指数?

-(void)printCurrentlyPlaying 
{ 

    NSString *mediaFileName = [[self.player.url absoluteString] lastPathComponent]; 

    BOOL found = NO; 

    int poemIndex = -1; 

    for(int i = 0; i < poemcollection.count && !found; i++) 
    { 
     NSString *filePath = poemcollection[i]; 

     NSString *fileName = [filePath lastPathComponent]; 

     if([fileName isEqualToString:mediaFileName]) 
     { 
      found = YES; 
      poemIndex = i; 
     } 
    } 

    if(found) 
    { 
     NSLog(@"Currently playing: %@, track number: %d", mediaFileName, poemIndex); 
    } 

} 
+0

非常感谢,它工作正常 – dilip

0

你可以保持一个映射字典的媒体名称,e.g -

poemCollection = @[ @{"Twinkle twinkle": [[NSMutableArray alloc] initWithObjects: 
        [[NSBundle mainBundle] pathForResource:@"twinkle_twinkle" ofType:@"mp3"]}, 
        @{@"Baa baa black sheep": [[NSBundle mainBundle] pathForResource:@"baa_baa_black_sheep" ofType:@"mp3"]}, 
        @{"Johny Johny": [[NSBundle mainBundle] pathForResource:@"johny_johny" ofType:@"mp3"]}]; 

//And read the title like below : 
NSString *title = [poemCollection[index] allKeys][0] 
NSString *musicFile = [poemCollection[index] allValues][0] 
0

试试这个代码获取当前文件名称和对象。

NSString *currentFile = [[self.player.url absoluteString] lastPathComponent]; 
[poemcollection indexOfObject:currentFile];