iOS开发:简单到爆炸的背景音乐播放器-AVPlayer

#pragma mark - 新建播放器

- (void)createPlayerWithMusicName:(NSString*)musicName{

    // 资源的URL地址

    NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];

    // 创建播放器曲目

    AVPlayerItem*playerItem = [[AVPlayerItemalloc]initWithURL:url];

    // 创建播放器

    self.player= [[AVPlayeralloc]initWithPlayerItem:playerItem];

    [self.playerplay];

}

#pragma mark - 切换歌曲

- (void)changeMusicWithMusicName:(NSString*)musicName {

    NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];

    AVPlayerItem*playerItem = [[AVPlayerItemalloc]initWithURL:url];

    [self.player replaceCurrentItemWithPlayerItem:playerItem];

    // 播放

    [self.playerplay];

}

推荐阅读更多精彩内容