在控制器中,引入
//引入音频、视频库
#import <AVFoundation/AVFoundation.h>
声明一个player变量
@interface ViewController ()
@property (nonatomic , strong) AVPlayer *player;
@end
导入歌曲到项目:
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];//设置声音大小 最大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];//设置声音大小 最大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];
网友评论