123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- //
- // FUMusicPlayer.m
- // FULive
- //
- // Created by 刘洋 on 2017/10/13.
- // Copyright © 2017年 liuyang. All rights reserved.
- //
- #import "FUMusicPlayer.h"
- #import <AVFoundation/AVFoundation.h>
- @implementation FUMusicPlayer
- {
- AVAudioPlayer *audioPlayer;
- NSString *musicName;
- }
- + (FUMusicPlayer *)sharePlayer{
- static FUMusicPlayer *_sharePlayer;
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _sharePlayer = [[FUMusicPlayer alloc] init];
- _sharePlayer.enable = YES;
- });
-
- return _sharePlayer;
- }
- -(instancetype)init{
- if (self = [super init]) {
- [self setupAudioSession];
- }
- return self;
- }
- - (void)setupAudioSession {
- static BOOL audioSessionSetup = NO;
- if (audioSessionSetup) {
- return;
- }
- [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
- UInt32 doSetProperty = 1;
- AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
- [[AVAudioSession sharedInstance] setActive: YES error: nil];
- audioSessionSetup = YES;
- }
- - (void)setEnable:(BOOL)enable{
- if (_enable == enable) {
- return;
- }
- _enable = enable;
- dispatch_async(dispatch_get_main_queue(), ^{
- if (enable) {
- [self rePlay];
- }else [self pause];
- });
- }
- - (void)playMusic:(NSString *)music{
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([audioPlayer isPlaying]) {
- [audioPlayer stop];
- audioPlayer = nil;
- }
-
- if (music) {
- NSString *path = [[NSBundle mainBundle] pathForResource:music ofType:nil];
- if (path) {
- NSURL *musicUrl = [NSURL fileURLWithPath:path];
- if (musicUrl) {
- musicName = music;
-
- if (self.enable) {
- audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicUrl error:nil];
- audioPlayer.numberOfLoops = 100000;
-
- [audioPlayer play];
- }
- }
- }
- }
- });
- }
- - (void)resume{
- dispatch_async(dispatch_get_main_queue(), ^{
- if (![audioPlayer isPlaying]) {
- [audioPlayer play];
- }
- });
- }
- - (void)rePlay{
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self playMusic:musicName];
- });
- }
- - (void)pause{
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([audioPlayer isPlaying]) {
- [audioPlayer pause];
- }
- });
- }
- - (void)stop{
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([audioPlayer isPlaying]) {
- [audioPlayer stop];
- }
-
- audioPlayer = nil;
- });
- }
- - (float)playProgress {
- if (audioPlayer.duration > 0) {
- return audioPlayer.currentTime / (audioPlayer.duration);
- }else return 0.0;
- }
- - (NSTimeInterval)currentTime {
- return audioPlayer.currentTime;
- }
- @end
|