做一个音乐播放器应用!上一首和下一首曲目

时间:2011-10-14 15:29:38

标签: ios objective-c audio avaudioplayer playback

如何在不使用ipod库的情况下为iPhone制作应用程序,并完成下一首歌和上一首歌?

我认为最好的方法是用歌曲做一个数组,但我不知道怎么做。有人可以帮助我吗?

我的应用程序正在运行,有暂停,播放,当前时间和音量滑块...但下一首歌的这部分我真的不知道!

- (void)viewDidLoad {

    //initialize string to the path of the song in the resource folder
    NSString *myMusic = [[NSBundle mainBundle]pathForResource:@"gastando" ofType:@"mp3"];
    player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:myMusic] error:NULL];
    player.numberOfLoops = 0;
    player.volume = slider.value;
    timeSlider.maximumValue = player.duration;

    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeLoader) userInfo:nil repeats:YES];

    [super viewDidLoad];        
}

1 个答案:

答案 0 :(得分:2)

    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    //get all media
    MPMediaQuery *everything = [[MPMediaQuery alloc] init];
    //empty song list
    NSMutableArray *songList = [[NSMutableArray alloc] init ];

    for (int i = 0 ; i < [[everything items] count] ; i++){
        MPMediaItem *media = (MPMediaItem*)[[everything items] objectAtIndex:i];

        NSInteger mediaType = [[media valueForProperty:MPMediaItemPropertyMediaType] intValue];
        //only want audio
        if (mediaType <= MPMediaTypeAnyAudio) {
            [songList addObject:media];
        }
    }
    //create music collection out of it
    MPMediaItemCollection *musicCollection = [MPMediaItemCollection collectionWithItems:songList];
    //set the playlist
    [musicPlayer setQueueWithItemCollection:musicCollection];
    [everything release];
    [songList release];