2012-05-23 24 views
0

我是iphone的新手。我正在使用音頻播放器,在這裏我被擊中了,因爲我面臨着我的項目中的一些問題我有資源文件夾中的2個目錄。在每個目錄中我有6mp3音頻文件。所以我的問題是,當用戶給出的資源文件夾中的第一個目錄中的第三個mp3歌曲的路徑,我們必須連續播放所有歌曲,如4th, 5日,6日mp3files也被放置在該目錄中的資源folder.here是我這個如何播放音樂歌曲一個接一個地放在iphone的資源文件夾中

NSString *chapterString [email protected]"3"; 
    NSString *string = [[NSBundle mainBundle]pathForResource:chapterString ofType:@"mp3" inDirectory:@"raj"]; 
    NSLog(@"string is %@",string); 
    url = [NSURL fileURLWithPath:string isDirectory:YES]; 
    NSError *error; 
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; 

代碼,如果任何機構知道這個,請幫助我..

回答

0

完成歌曲x後,您可以通過NSFileManager將目錄的內容導入到數組中,播放數組中的下一首歌曲歌曲y。

0
  1. 把所有的歌曲在一個陣列
  2. 初始化的全局變量假設X
  3. 編寫如下AVAudioPlayer代表

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 
        if (x<([_downloadedSongArray count]-1)) 
        { 
         x=x+1; 
         [self loadSong]; 
        } 
    } 
    - (void)loadSong 
    { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        _audioDocumentsDirectory = [paths objectAtIndex:0]; 
        _audioDocumentsDirectory = [_audioDocumentsDirectory stringByAppendingPathComponent:@"Songs"]; 
        NSString *selectedSong = [_downloadedSongArray objectAtIndex:x]; 
        _sharerDownloadedString=selectedSong; 
        selectedSong = [selectedSong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
        _audioDocumentsDirectory = [_audioDocumentsDirectory stringByAppendingPathComponent:selectedSong]; 
    
        url = [NSURL fileURLWithPath:_audioDocumentsDirectory]; 
        [_audio stop]; 
        _audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
        [_audio prepareToPlay]; 
        [_audio play]; 
        [Utility setPlaying:YES]; 
        [_audio setDelegate:self]; 
    } 
    

凡_downloadedSongArray是一個數組,其中的所有歌曲都。 loadSong是一種方法,它根據索引從數組中加載next/prev歌曲並分配給AVAudioPlayer。

相關問題