在线音乐播放器单例

原创 2014年12月19日 17:45:23

.h文件里面

<span style="font-size:18px;">#import <Foundation/Foundation.h>
@class AVPlayer;
#define kLocalURL 1
#define kOnLineURL 2
//播放模式
enum {
	AVPlayerOrderLoop = 0,
    AVPlayerRandomLoop = 1,
    AVPlayerSingleLoop = 2,
    AVPlayerListLoop = 3
};
typedef NSInteger AVPlayerLoopStatus;
@interface MusicsPlayer : NSObject
//播放模式
@property(nonatomic,assign)AVPlayerLoopStatus AVPlayerLoopState;
//播放器
@property(nonatomic,retain)AVPlayer *musicPlayer;
@property(nonatomic,assign)NSInteger URLState;
@property(nonatomic,retain)UIImage *picture;
@property(nonatomic,copy)NSString *songName;
@property(nonatomic,copy)NSString *duration;
@property(nonatomic,copy)NSString *singerName;
//当前播放歌曲下标
@property(nonatomic,assign)NSInteger index;
//当前时间
@property(nonatomic,copy)NSString *currentTime;
//当前进程
@property(nonatomic,assign)NSInteger progress;
//总时间
@property(nonatomic,assign)NSInteger totalTimeValue;

//播放列表数据存放数组
@property(nonatomic,retain)NSMutableArray *PlayerListArray;
//单例
+(MusicsPlayer *)shareMusic;

-(void)playWithString:(NSString *)URLSting;
//停止播放
-(void)stop;
//暂停
-(void)pause;
//下一曲
-(void)nextSong;
//上一曲
-(void)frontSong;
//得到播放列表数组
-(void)getDataToPlayerListArrayWithSongListArray:(NSMutableArray *)songListArray;
@end</span>
.m文件
<span style="font-size:18px;">#import "MusicsPlayer.h"
#import "AudioPlayModel.h"
#import <AVFoundation/AVFoundation.h>
#define kSingleLoop 0

@interface MusicsPlayer()
{
    
    AVPlayerItem *_musicItem;
    NSURL *_url;
    NSData *_data;
    BOOL _playingState;
}
@end
@implementation MusicsPlayer
-(void)dealloc{
    [_url release];
    [_picture release];
    [_songName release];
    [_singerName release];
    [_duration release];
    [_currentTime release];
    [super dealloc];
}
static MusicsPlayer *musicPlayer = nil;
+(MusicsPlayer *)shareMusic
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        musicPlayer = [[MusicsPlayer alloc] init];
    });
    return musicPlayer;
}
-(void)playWithString:(NSString *)URLSting
{
    //没有stop,先暂停,再释放
    [self.musicPlayer pause];
    self.musicPlayer = nil;
    self.currentTime = nil;
    self.picture = nil;
    if (URLSting) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"URLSting" object:nil];
        _url = [NSURL URLWithString:URLSting];
        if (!_url) {
            _url = [NSURL fileURLWithPath:URLSting];
        }
        _musicItem = [AVPlayerItem playerItemWithURL:_url];
        self.musicPlayer = [[[AVPlayer alloc] initWithPlayerItem:_musicItem] autorelease];
        [self.musicPlayer play];
        
        [_musicItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew 
              | NSKeyValueObservingOptionOld context:nil];
        //播放状态
        [_musicItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | 
                 NSKeyValueObservingOptionOld context:nil];
        //监测是否播放完(AVPlayerItemDidPlayToEndTimeNotification)
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AutoNextSong) 
            name:@"AVPlayerItemDidPlayToEndTimeNotification" object:_musicItem];
        CMTime interval = CMTimeMake(1, 1);
        //歌曲播放进程
        [self.musicPlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue()
                usingBlock:^(CMTime time) {
            if (self.musicPlayer.rate == 1) {
                _playingState = YES;
            }else{
                _playingState = NO;
            }
            //将CMTime转换为秒(给slider赋值)
            NSInteger currentTime = CMTimeGetSeconds(_musicItem.currentTime);
            self.progress = currentTime;
            //将NSInteger(进程Progress)转换为时间字符串
            self.currentTime = [self getCurrentTimeWithValue:currentTime];
            //获取文件总时长
            self.totalTimeValue = CMTimeGetSeconds(_musicItem.duration);
            //转换为字符串
            self.duration = [self getCurrentTimeWithValue:self.totalTimeValue];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"currentTime" object:nil];
        }];
    }
    
}
//播放玩,自动下一曲
-(void)AutoNextSong
{
    //移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVPlayerItemDidPlayToEndTimeNotification"
         object:nil];
    if (self.PlayerListArray) {
        //顺序
        if (self.AVPlayerLoopState == AVPlayerOrderLoop) {
            if (self.index == self.PlayerListArray.count - 1) {
                
            }else{
                AudioPlayModel *adoModel = self.PlayerListArray[++self.index][0];
                self.songName = adoModel.song_name;
                self.singerName = adoModel.singer_name;
                self.duration = adoModel.duration;
                [self playWithString:adoModel.url];
            }
        }
        //随机
        if (self.AVPlayerLoopState == AVPlayerRandomLoop) {
            NSInteger randomNum = arc4random()%(self.PlayerListArray.count - 1 + 1);
            AudioPlayModel *adoModel = self.PlayerListArray[randomNum][0];
            self.songName = adoModel.song_name;
            self.singerName = adoModel.singer_name;
            self.duration = adoModel.duration;
            [self playWithString:adoModel.url];
        }
        //单曲
        if (self.AVPlayerLoopState == AVPlayerSingleLoop) {
            AudioPlayModel *adoModel = self.PlayerListArray[self.index][0];
            self.songName = adoModel.song_name;
            self.singerName = adoModel.singer_name;
            self.duration = adoModel.duration;
            [self playWithString:adoModel.url];
        }
        //循环
        if (self.AVPlayerLoopState == AVPlayerListLoop) {
            if (self.index == self.PlayerListArray.count - 1) {
                self.index = 0;
                AudioPlayModel *adoModel = self.PlayerListArray[self.index][0];
                self.songName = adoModel.song_name;
                self.singerName = adoModel.singer_name;
                self.duration = adoModel.duration;
                [self playWithString:adoModel.url];
            }else{
                AudioPlayModel *adoModel = self.PlayerListArray[++self.index][0];
                self.songName = adoModel.song_name;
                self.singerName = adoModel.singer_name;
                self.duration = adoModel.duration;
                [self playWithString:adoModel.url];
            }
        }
        
    }
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)
    change context:(void *)context
{
    
    if ([keyPath isEqualToString:@"status"]) {

        if (_musicItem.status == AVPlayerItemStatusReadyToPlay) {
            
        }
        if (_musicItem.status == AVPlayerItemStatusUnknown) {
            
        }
        if (_musicItem.status == AVPlayerItemStatusFailed) {
            
        }
    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){

    }

    
}
//停止播放
-(void)stop
{
    if (_musicPlayer) {
        _musicPlayer = nil;
    }
}
//暂停
-(void)pause
{
    if (_playingState == YES) {
        [_musicPlayer pause];
    }else{
        [_musicPlayer play];
    }
}
//下一曲
-(void)nextSong
{
    if (_musicPlayer) {
        [_musicPlayer pause];
        _musicPlayer = nil;
    }
    if (self.PlayerListArray) {
        if (self.index == self.PlayerListArray.count - 1) {
        }else{
            AudioPlayModel *adoModel = self.PlayerListArray[++self.index][0];
            self.songName = adoModel.song_name;
            self.singerName = adoModel.singer_name;
            self.duration = adoModel.duration;
            [self playWithString:adoModel.url];
        }
    }
}
//上一曲
-(void)frontSong
{
    if (_musicPlayer) {
        [_musicPlayer pause];
        _musicPlayer = nil;
    }
    //确定数组不为空
    if (self.PlayerListArray) {
        if (self.index == 0) {
            //等于零的时候不能上一曲
            AudioPlayModel *adoModel = self.PlayerListArray[self.index][0];
            self.songName = adoModel.song_name;
            self.singerName = adoModel.singer_name;
            self.duration = adoModel.duration;
            [self playWithString:adoModel.url];
        }else{
            AudioPlayModel *adoModel = self.PlayerListArray[--self.index][0];
            self.songName = adoModel.song_name;
            self.singerName = adoModel.singer_name;
            self.duration = adoModel.duration;
            [self playWithString:adoModel.url];
        }
    }
}
//将时间字符串转换为秒
-(NSInteger)getTimeWithString:(NSString *)duration
{
    NSArray *arr = [duration componentsSeparatedByString:@":"];
    NSInteger secend = [arr[0] intValue] * 60 + [arr[1] intValue];
    return secend;
}
//将秒转换为时间字符串
-(NSString *)getCurrentTimeWithValue:(NSInteger )sender
{
    NSString *currentTime = [NSString stringWithFormat:@"%d:%d",sender/60,sender%60];
    return currentTime;
}
//得到播放列表数组
-(void)getDataToPlayerListArrayWithSongListArray:(NSMutableArray *)songListArray
{
    self.PlayerListArray = nil;
    self.PlayerListArray = [NSMutableArray arrayWithArray:songListArray];
}
@end</span>


相关文章推荐

音乐播放器单例

// //  MusicTool.h //  SunshineMusic // //  Created by dllo on 16/1/11. //  Copyright...

Jamendo开源在线音乐播放器源码分析(三)

3、AsyncTask new NewsTask().execute((Void)null);执行一个异步任务,也可以什么参数也不传。异步加载唱片的信息,调用相应的API,后面详说API,加载成功则...

在线音乐播放器的推荐机制

对于我这样一个音乐控来说,学习工作时没有音乐是万万不行的。在网易云音乐云音乐面世之前,我总是在QQ音乐,百度音乐等播放器之间徘徊,当时也不够文艺,不知道豆瓣FM的存在。 第一次接触云音乐被其吸引的原...

绝对给力的源码,在线音乐播放器完整项目

http://www.eoeandroid.com/thread-52016-1-1.html 不好意思各位,因昨天有同事提醒我jamendo是商业公司,发布它的源码可能要追...

开源Jamendo在线音乐播放器源码(四)

上文中我们介绍了com.teleca.jamendo.util.FixedViewFlipper的用法以及作用,现在我们再介绍ListView中的内容,相关布局如下: View Code ...

Android MediaPlayer在线音乐播放器

本文将的是一个在线音乐播放器,文中对相应操作做了比较详细的讲解,已达到实现效果,并了解播放器原理的目的。 源代码: 布局文件: activity_audio_httpplayer: ...

Jamendo开源在线音乐播放器源码分析(一)

Jamendo源码地址:https://github.com/telecapoland/jamendo-android src跟res结构: 之后将会按照流程走向和模块进行分析。 ...

Android Jamendo开源在线音乐播放器源码分析九 ViewFlipper及自定义布局控件的分析

在之前分析过这个ViewFlipper代码: 备注一:ViewFlipper 界面的最上面的mViewFlipper中包含了mGallery、mProgressBar、mFailureBar,...

Android Jamendo开源在线音乐播放器源码分析三 程序首界面

整个程序的入口Activity从Manifest里面可以找到是SplashscreenActivity,界面截图如下: 程序的代码如下: public class Splashscre...

ubuntu在线音乐播放器

之前有试过wine一款播放器,无奈技术不到位.反倒把电脑搞得一塌糊涂.实在有点郁闷.不过最近偶尔搜一下,发现现在已经有了解决方案. 本文转自百度经验,原文链接如下: jingyan.baidu.co...
  • cp3alai
  • cp3alai
  • 2015年06月06日 22:50
  • 1406
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在线音乐播放器单例
举报原因:
原因补充:

(最多只允许输入30个字)