0

我是 iphone 新手。我在这里开发音频播放器,我在中间感到震惊,因为我在项目中遇到了一些问题,我在资源文件夹中有 2 个目录。在每个目录中我有 6mp3 音频文件。所以我的问题是当用户在资源文件夹的第一个目录中提供第三首 mp3 歌曲的路径时,我们必须连续播放所有歌曲,如第 4、5、6 个 mp3files 也放置在资源文件夹的该目录中。这是我的代码为了这

    NSString *chapterString =@"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];

如果有人知道这一点,请帮助我..

4

2 回答 2

0

NSFileManager在完成歌曲 x..播放数组中的下一首歌曲歌曲 y 后,您可以通过 将目录的内容放入一个数组中。

于 2012-05-23T07:12:14.480 回答
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 是一种根据索引从数组中加载下一首/上一首歌曲并分配给 AVAudioPlayer 的方法。

于 2012-05-23T08:09:59.850 回答