在控制器中最爬,引入
//引入音頻、視頻庫
#import <AVFoundation/AVFoundation.h>
聲明一個player變量
@interface ViewController ()
@property (nonatomic , strong) AVPlayer *player;
@end
導(dǎo)入歌曲到項目:
demo.png
播放本地音樂:
//播放音樂
AVAudioSession *session = [AVAudioSession sharedInstance];//它是一個單例熬粗,管理音視頻
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//1資源
NSBundle *boundle = [NSBundle mainBundle];
//url 路徑
NSURL *url = [boundle URLForResource:@"冰雨" withExtension:@"mp3"];
_player = [[AVPlayer alloc]initWithURL:url];//初始化播放對象
[_player setVolume:1.0];//設(shè)置聲音大小 最大1.0
[_player play];
播放下一首歌曲:
1,初始化_player
//1資源
NSBundle *boundle = [NSBundle mainBundle];
//url 路徑
NSURL *url = [boundle URLForResource:@"冰雨" withExtension:@"mp3"];
AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:url];
_player = [[AVPlayer alloc]initWithPlayerItem:item];//[[AVPlayer alloc]initWithURL:url];//初始化播放對象
[_player setVolume:1.0];//設(shè)置聲音大小 最大1.0
[_player play];
2,播放下一首:
//1資源
NSBundle *boundle = [NSBundle mainBundle];
//url 路徑
NSURL *url = [boundle URLForResource:@"董小姐" withExtension:@"mp3"];
AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:url];
//播放下一首歌曲
[_player replaceCurrentItemWithPlayerItem:item];