【ios学习笔记】背景音乐循环播放

来源:互联网 发布:bfgs算法matlab代码 编辑:程序博客网 时间:2024/05/16 02:42

//引入

#import <AVFoundation/AVFoundation.h>

//并在借口文件中添加要实现的代理

@interface ViewController :UIViewController<AVAudioPlayerDelegate>


//建全局变量myplayer

AVAudioPlayer *myplayer;


//获取mp3路径,音乐的名字为1.mp3

NSString *path = [[NSBundlemainBundle] pathForResource:@"1"ofType:@"mp3"];

//转为url

NSURL *url = [[NSURLalloc] initFileURLWithPath:path];


//初始化myplayer

myplayer = [[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];

//设置代理

myplayer.delegate =self;

//实现代理的循环播放函数

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    [myplayer play];

    

}