音乐播放MusicPlay

1.创建单例(继承与NSObject)
Music.h

@interface Music : NSObject
@property (nonatomic, strong)AVPlayer *player;
- (void)playMusicByURLString:(NSString *)urlString;
+ (Music *)shareInstance;
@end

Music.m

#import "Music.h"
static Music *music = nil;(定义一个空指针,没有指向)
@implementation Music
+ (Music *)shareInstance
{
    if (music != nil) {
        return music;
    }
        music = [[Music alloc] init];
        return music;
}

添加播放的方法:
- (void)playMusicByURLString:(NSString *)urlString
{
self.player = [AVPlayer playerWithURL:[NSURL URLWithString:urlString]];
//开始播放
[self.player play];
}
@end


在ViewController.m中

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [[Music shareInstance]   
 playMusicByURLString:
@"http://sc1.111ttt.com/2016/1/02/    24/195242042236.mp3"];
}

你可能感兴趣的:(音乐播放MusicPlay)