音乐播放AVPlayer

在控制器中,引入

//引入音频、视频库
#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];
  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,253评论 3 119

友情链接更多精彩内容