2013-11-26 49 views
0

     我是iPhone新手。使用下面的代碼我正在顯示音頻圖像。圖片和音頻我已經存儲在數組中。音頻完成audioPlayerDidFinishPlaying方法後,我正在使用此代碼,我正在顯示音頻的下一個圖像。
     每件事情都很好。但問題在於音頻和圖像完成後,它又是從起點開始。可能是下面的代碼中的一些問題,任何一個可以糾正它PLZ ..如何在iOS中完成循環後停止音頻

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    for (NSString* path in array000) 
    { 
     [blaukypath2 addObject:[UIImage imageWithContentsOfFile:path]]; 
     NSLog(@"%@",path); 
    } 
    currentImage++; 
    NSError *error; 
    if (currentImage >= blaukypath2.count) currentImage = 0; 
    UIImage *blaukyyimag = [blaukypath2 objectAtIndex:currentImage]; 
    [img10 setImage:blaukyyimag]; 
    NSLog(@"%icurrentImage++ DIDF",currentImage); 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[audiopath2 objectAtIndex:currentImage] error:&error] ; 
    audioPlayer.delegate = self; 
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 
} 
+0

如何'currentImage'聲明?是否分配了'blaukypath2'? – trojanfoe

+0

haan ya分配 –

+0

爲什麼你在代碼中使用'currentImage = 0;'? –

回答

0

爲使下一個音頻,停止前一個。

[audioPlayer stop]; 

希望它適合你。

+0

這不就是委託方法'audioPlayerDidFinishPlaying'意味着什麼嗎? – trojanfoe

0

製作一個實例變量int arrayIndex;與= 0

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    if([array000 count] > arrayIndex) { 

     NSString* path = [arrayIndex objectAtIndex:arrayIndex]; 

     [blaukypath2 addObject:[UIImage imageWithContentsOfFile:path]]; 
     NSLog(@"%@",path); 

     currentImage++; 
    NSError *error; 
    if (currentImage >= blaukypath2.count) currentImage = 0; 
    UIImage *blaukyyimag = [blaukypath2 objectAtIndex:currentImage]; 
    [img10 setImage:blaukyyimag]; 
    NSLog(@"%icurrentImage++ DIDF",currentImage); 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[audiopath2  objectAtIndex:currentImage] error:&error] ; 
    audioPlayer.delegate = self; 
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 

    } 

    arrayIndex++; 
} else { 
[audioPlayer stop]; 
} 

試試這個辦法,讓我知道,如果它的工作

+0

爲什麼一個* class *變量? – trojanfoe

+0

否則你不能跟蹤數組索引,本地將每次重置 – Retro

+0

感謝您給replay.Its不工作.. –

1

如果你不想重複比在代碼中移除該行的圖像和音頻初始化..

if (currentImage >= blaukypath2.count) currentImage = 0; 

因爲這一行會讓你的currentImage爲0.所以循環會重新開始。

UPDATE
做這樣的

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    for (NSString* path in array000) 
    { 
     [blaukypath2 addObject:[UIImage imageWithContentsOfFile:path]]; 
     NSLog(@"%@",path); 
    } 
    if (currentImage < blaukypath2.count) 
    { 
     currentImage++; 
     NSError *error; 
     UIImage *blaukyyimag = [blaukypath2 objectAtIndex:currentImage]; 
     [img10 setImage:blaukyyimag]; 
     NSLog(@"%icurrentImage++ DIDF",currentImage); 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[audiopath2 objectAtIndex:currentImage] error:&error] ; 
     audioPlayer.delegate = self; 
     [audioPlayer prepareToPlay]; 
     [audioPlayer play]; 
    } 
} 
+0

嘿前兩個圖像工作,但之後,它crashing.it顯示這樣的錯誤***終止應用程序由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引3超越界限[0..2]」 ***第一擲調用堆棧: (0x31aa22a3 0x3974797f 0x319edb75 0xa5557 0x323b90f5 0x31a77683 0x31a76ee9 0x31a75cb7 0x319e8ebd 0x319e8d49 0x355ac2eb 0x338fe301 0x1ae6d 0x39b7eb20) 的libC++ abi.dylib:終止稱爲拋出異常 –

+0

這意味着' currentImage'超出了'blaukypath2.count'的值。你必須限制它。 – Dilip

+0

檢查我的更新.. – Dilip