一、控制器間數據傳遞
兩個控制器之間數據的傳遞
第一種方法:
工具類中的代碼設計如下:
#import <Foundation/Foundation.h>
@class YYMusicModel;
@interface YYMusicTool : NSObject
/**
* 返回所有的歌曲
*/
+ (NSArray *)musics;
/**
* 返回正在播放的歌曲
*/
+ (YYMusicModel *)playingMusic;
+ (void)setPlayingMusic:(YYMusicModel *)playingMusic;
/**
* 下一首歌曲
*/
+ (YYMusicModel *)nextMusic;
/**
* 上一首歌曲
*/
+ (YYMusicModel *)previousMusic;
@end
#import "YYMusicTool.h"
#import "YYMusicModel.h"
#import "MJExtension.h"
@implementation YYMusicTool
static NSArray *_musics;
static YYMusicModel *_playingMusic;
/**
* @return 返回所有的歌曲
*/
+(NSArray *)musics
{
if (_musics==nil) {
_musics=[YYMusicModel objectArrayWithFilename:@"Musics.plist"];
}
return _musics;
}
+(void)setPlayingMusic:(YYMusicModel *)playingMusic
{
/*
*如果沒有傳入需要播放的歌曲,或者是傳入的歌曲名不在音樂庫中,那么就直接返回
如果需要播放的歌曲就是當前正在播放的歌曲,那么直接返回
*/
if (!playingMusic || ![[self musics]containsObject:playingMusic]) return;
if (_playingMusic == playingMusic) return;
_playingMusic=playingMusic;
}
/**
* 返回正在播放的歌曲
*/
+(YYMusicModel *)playingMusic
{
return _playingMusic;
}
/**
* 下一首歌曲
*/
+(YYMusicModel *)nextMusic
{
//設定一個初值
int nextIndex = 0;
if (_playingMusic) {
//獲取當前播放音樂的索引
int playingIndex = [[self musics] indexOfObject:_playingMusic];
//設置下一首音樂的索引
nextIndex = playingIndex+1;
//檢查數組越界,如果下一首音樂是最后一首,那么重置為0
if (nextIndex>=[self musics].count) {
nextIndex=0;
}
}
return [self musics][nextIndex];
}
/**
* 上一首歌曲
*/
+(YYMusicModel *)previousMusic
{
//設定一個初值
int previousIndex = 0;
if (_playingMusic) {
//獲取當前播放音樂的索引
int playingIndex = [[self musics] indexOfObject:_playingMusic];
//設置下一首音樂的索引
previousIndex = playingIndex-1;
//檢查數組越界,如果下一首音樂是最后一首,那么重置為0
if (previousIndex<0) {
previousIndex=[self musics].count-1;
}
}
return [self musics][previousIndex];
}
@end
該工具類中的代碼設計如下:
YYAudioTool.h文件
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface YYAudioTool : NSObject
/**
*播放音樂文件
*/
+(BOOL)playMusic:(NSString *)filename;
/**
*暫停播放
*/
+(void)pauseMusic:(NSString *)filename;
/**
*播放音樂文件
*/
+(void)stopMusic:(NSString *)filename;
/**
*播放音效文件
*/
+(void)playSound:(NSString *)filename;
/**
*銷毀音效
*/
+(void)disposeSound:(NSString *)filename;
@end
#import "YYAudioTool.h"
@implementation YYAudioTool
/**
*存放所有的音樂播放器
*/
static NSMutableDictionary *_musicPlayers;
+(NSMutableDictionary *)musicPlayers
{
if (_musicPlayers==nil) {
_musicPlayers=[NSMutableDictionary dictionary];
}
return _musicPlayers;
}
/**
*存放所有的音效ID
*/
static NSMutableDictionary *_soundIDs;
+(NSMutableDictionary *)soundIDs
{
if (_soundIDs==nil) {
_soundIDs=[NSMutableDictionary dictionary];
}
return _soundIDs;
}
/**
*播放音樂
*/
+(BOOL)playMusic:(NSString *)filename
{
if (!filename) return NO;//如果沒有傳入文件名,那么直接返回
//1.取出對應的播放器
AVAudioPlayer *player=[self musicPlayers][filename];
//2.如果播放器沒有創建,那么就進行初始化
if (!player) {
//2.1音頻文件的URL
NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil];
if (!url) return NO;//如果url為空,那么直接返回
//2.2創建播放器
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//2.3緩沖
if (![player prepareToPlay]) return NO;//如果緩沖失敗,那么就直接返回
//2.4存入字典
[self musicPlayers][filename]=player;
}
//3.播放
if (![player isPlaying]) {
//如果當前沒處于播放狀態,那么就播放
return [player play];
}
return YES;//正在播放,那么就返回YES
}
+(void)pauseMusic:(NSString *)filename
{
if (!filename) return;//如果沒有傳入文件名,那么就直接返回
//1.取出對應的播放器
AVAudioPlayer *player=[self musicPlayers][filename];
//2.暫停
[player pause];//如果palyer為空,那相當于[nil pause],因此這里可以不用做處理
}
+(void)stopMusic:(NSString *)filename
{
if (!filename) return;//如果沒有傳入文件名,那么就直接返回
//1.取出對應的播放器
AVAudioPlayer *player=[self musicPlayers][filename];
//2.停止
[player stop];
//3.將播放器從字典中移除
[[self musicPlayers] removeObjectForKey:filename];
}
//播放音效
+(void)playSound:(NSString *)filename
{
if (!filename) return;
//1.取出對應的音效
SystemSoundID soundID=[[self soundIDs][filename] unsignedIntegerValue];
//2.播放音效
//2.1如果音效ID不存在,那么就創建
if (!soundID) {
//音效文件的URL
NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil];
if (!url) return;//如果URL不存在,那么就直接返回
OSStatus status = AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
NSLog(@"%ld",status);
//存入到字典中
[self soundIDs][filename]=@(soundID);
}
//2.2有音效ID后,播放音效
AudioServicesPlaySystemSound(soundID);
}
//銷毀音效
+(void)disposeSound:(NSString *)filename
{
//如果傳入的文件名為空,那么就直接返回
if (!filename) return;
//1.取出對應的音效
SystemSoundID soundID=[[self soundIDs][filename] unsignedIntegerValue];
//2.銷毀
if (soundID) {
AudioServicesDisposeSystemSoundID(soundID);
//2.1銷毀后,從字典中移除
[[self soundIDs]removeObjectForKey:filename];
}
}
@end
YYPlayingViewController.m文件
#import "YYPlayingViewController.h"
#import "YYMusicTool.h"
#import "YYMusicModel.h"
#import "YYAudioTool.h"
@interface YYPlayingViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *songLabel;
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
@property(nonatomic,strong)YYMusicModel *playingMusic;
- (IBAction)exit;
@end
#pragma mark-內部的按鈕監聽方法
//返回按鈕
- (IBAction)exit {
//1.禁用整個app的點擊事件
UIWindow *window=[UIApplication sharedApplication].keyWindow;
window.userInteractionEnabled=NO;
//2.動畫隱藏View
[UIView animateWithDuration:0.25 animations:^{
self.view.y=window.height;
} completion:^(BOOL finished) {
window.userInteractionEnabled=YES;
//設置view隱藏能夠節省一些性能
self.view.hidden=YES;
}];
}
@end
提示:一般的播放器需要做一個重置的操作。
當從一首歌切換到另外一首時,應該先把上一首的信息刪除,因此在show動畫顯示之前,應該檢測是否換了歌曲,如果換了歌曲,則應該做一次重置操作。
實現效果(能夠順利的切換和播放歌曲,下面是界面顯示):
五、補充代碼
YYMusicsViewController.m文件
#import "YYMusicsViewController.h"
#import "YYMusicModel.h"
#import "MJExtension.h"
#import "YYMusicCell.h"
#import "YYPlayingViewController.h"
#import "YYMusicTool.h"
@interface YYMusicsViewController ()
@property(nonatomic,strong)YYPlayingViewController *playingViewController;
@end
-(YYPlayingViewController *)playingViewController
{
if (_playingViewController==nil) {
_playingViewController=[[YYPlayingViewController alloc]init];
}
return _playingViewController;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - Table view data source
/**
*一共多少組
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
/**
*每組多少行
*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [YYMusicTool musics].count;
}
/**
*每組每行的cell
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
YYMusicCell *cell=[YYMusicCell cellWithTableView:tableView];
cell.music=[YYMusicTool musics][indexPath.row];
return cell;
}
/**
* 設置每個cell的高度
*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
/**
* cell的點擊事件
*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.取消選中被點擊的這行
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//2.設置正在播放的歌曲
[YYMusicTool setPlayingMusic:[YYMusicTool musics][indexPath.row]];
//調用公共方法
[self.playingViewController show];
// //執行segue跳轉
// [self performSegueWithIdentifier:@"music2playing" sender:nil];
}
@end
完整的代碼
YYPlayingViewController.m文件
#import "YYPlayingViewController.h"
#import "YYMusicTool.h"
#import "YYMusicModel.h"
#import "YYAudioTool.h"
@interface YYPlayingViewController ()
//進度條
@property (weak, nonatomic) IBOutlet UIView *progressView;
//滑塊
@property (weak, nonatomic) IBOutlet UIButton *slider;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *songLabel;
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
//當前播放的音樂的時長
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
//正在播放的音樂
@property(nonatomic,strong)YYMusicModel *playingMusic;
//音樂播放器對象
@property(nonatomic,strong)AVAudioPlayer *player;
//定時器
@property(nonatomic,strong)NSTimer *CurrentTimeTimer;
- (IBAction)exit;
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender;
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender;
@end
#pragma mark-私有方法
//重置正在播放的音樂
-(void)RresetPlayingMusic
{
//1.重置界面數據
self.iconView.image=[UIImage imageNamed:@"play_cover_pic_bg"];
self.songLabel.text=nil;
self.singerLabel.text=nil;
//2.停止播放
[YYAudioTool stopMusic:self.playingMusic.filename];
//把播放器進行清空
self.player=nil;
//3.停止定時器
[self removeCurrentTime];
}
//開始播放音樂數據
-(void)starPlayingMusic
{
//1.設置界面數據
//如果當前播放的音樂就是傳入的音樂,那么就直接返回
if (self.playingMusic==[YYMusicTool playingMusic])
{
//把定時器加進去
[self addCurrentTimeTimer];
return;
}
//存取音樂
self.playingMusic=[YYMusicTool playingMusic];
self.iconView.image=[UIImage imageNamed:self.playingMusic.icon];
self.songLabel.text=self.playingMusic.name;
self.singerLabel.text=self.playingMusic.singer;
//2.開始播放
self.player = [YYAudioTool playMusic:self.playingMusic.filename];
//3.設置時長
//self.player.duration; 播放器正在播放的音樂文件的時間長度
self.durationLabel.text=[self strWithTime:self.player.duration];
//4.添加定時器
[self addCurrentTimeTimer];
}
/**
*把時間長度-->時間字符串
*/
-(NSString *)strWithTime:(NSTimeInterval)time
{
int minute=time / 60;
int second=(int)time % 60;
return [NSString stringWithFormat:@"%d:%d",minute,second];
}
#pragma mark-定時器控制
/**
* 添加一個定時器
*/
-(void)addCurrentTimeTimer
{
//提前先調用一次進度更新,以保證定時器的工作時及時的
[self updateCurrentTime];
//創建一個定時器,每一秒鐘調用一次
self.CurrentTimeTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
//把定時器加入到運行時中
[[NSRunLoop mainRunLoop]addTimer:self.CurrentTimeTimer forMode:NSRunLoopCommonModes];
}
/**
*移除一個定時器
*/
-(void)removeCurrentTime
{
[self.CurrentTimeTimer invalidate];
//把定時器清空
self.CurrentTimeTimer=nil;
}
/**
* 更新播放進度
*/
-(void)updateCurrentTime
{
//1.計算進度值
double progress=self.player.currentTime/self.player.duration;
//2.計算滑塊的x值
// 滑塊的最大的x值
CGFloat sliderMaxX=self.view.width-self.slider.width;
self.slider.x=sliderMaxX*progress;
//設置滑塊上的當前播放時間
[self.slider setTitle:[self strWithTime:self.player.currentTime] forState:UIControlStateNormal];
//3.設置進度條的寬度
self.progressView.width=self.slider.center.x;
}
#pragma mark-內部的按鈕監聽方法
//返回按鈕
- (IBAction)exit {
//0.移除定時器
[self removeCurrentTime];
//1.禁用整個app的點擊事件
UIWindow *window=[UIApplication sharedApplication].keyWindow;
window.userInteractionEnabled=NO;
//2.動畫隱藏View
[UIView animateWithDuration:0.25 animations:^{
self.view.y=window.height;
} completion:^(BOOL finished) {
window.userInteractionEnabled=YES;
//設置view隱藏能夠節省一些性能
self.view.hidden=YES;
}];
}
/**
*點擊了進度條
*/
- (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender {
//獲取當前單擊的點
CGPoint point=[sender locationInView:sender.view];
//切換歌曲的當前播放時間
self.player.currentTime=(point.x/sender.view.width)*self.player.duration;
//更新播放進度
[self updateCurrentTime];
}
- (IBAction)panSlider:(UIPanGestureRecognizer *)sender {
//1.獲得挪動的距離
CGPoint t=[sender translationInView:sender.view];
//把挪動清零
[sender setTranslation:CGPointZero inView:sender.view];
//2.控制滑塊和進度條的frame
self.slider.x+=t.x;
//設置進度條的寬度
self.progressView.width=self.slider.center.x;
//3.設置時間值
CGFloat sliderMaxX=self.view.width-self.slider.width;
double progress=self.slider.x/sliderMaxX;
//當前的時間值=音樂的時長*當前的進度值
NSTimeInterval time=self.player.duration*progress;
[self .slider setTitle:[self strWithTime:time] forState:UIControlStateNormal];
//4.如果開始拖動,那么就停止定時器
if (sender.state==UIGestureRecognizerStateBegan) {
//停止定時器
[self removeCurrentTime];
}else if(sender.state==UIGestureRecognizerStateEnded)
{
//設置播放器播放的時間
self.player.currentTime=time;
//開啟定時器
[self addCurrentTimeTimer];
}
}
@end
調整開始播放音樂按鈕,讓其返回一個音樂播放器,而非BOOL型的。
return player;//正在播放,那么就返回YES
}
把時間轉換為時間字符串的方法:
說明:進度控制
監聽當前的播放,使用一個定時器,不斷的監聽當前是第幾秒。
關于定時器的處理:這里使用了三個方法,分別是添加定時器,移除定時器,和更新播放進度。
注意細節:
(1)移除定時器后,對定時器進行清空處理。
(3)在開始播放音樂的方法中進行判斷,如果當前播放的音樂和傳入的音樂一致,那么添加定時器后直接返回。
(4)重置播放的音樂方法中,停止定時器。
代碼說明(四)
說明:點擊和拖動進度條的處理
(1)點擊進度條
先添加單擊的手勢識別器。
往控制器拖線:
涉及的代碼:
先添加拖拽手勢識別器
往控制器拖線
涉及的代碼:
新聞熱點
疑難解答