AVAudioPlayer,AVPlayer 使用


這兩者簡單使用,話不多說,我們直接來看代碼吧。


#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h> // xcode6.0以後 直接導入該框架即可

@interface ViewController : UIViewController
{
    __weak IBOutlet UILabel *_timeLabel;//時間顯示Label
}
@property (weak, nonatomic) IBOutlet UISlider *Slider; //播放進度滑動條
@property (weak, nonatomic) IBOutlet UISlider *voliSlider; //播放進度滑動條

@end




#import "ViewController.h"

@interface ViewController ()
{
    AVAudioPlayer *_player; //只能播放本地音樂
//    AVPlayer *_player;  //能播放本地音樂 和 流媒體音樂
    //記錄播放時間
    NSTimer *timer;
//    BOOL isPlaying;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//———————————————————————————AVAudioPlayer————————————————————————————————

    //    NSURL *url1=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music1.mp3" ofType:nil]];
    NSURL *url2=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Taylor Swift - Blank Space [mqms].mp3" ofType:nil]];
    //只能播放本地音樂
    _player=[[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil];
    //播放總時間
    _Slider.maximumValue=_player.duration;
    
    
//———————————————————————————AVPlayer————————————————————————————————————
    
//    NSString *urlPath=@"http://qqmusic.4ftt.com/m/101369814.mp3?20150318133115";
//    NSURL *url=[NSURL URLWithString:urlPath];
//    //能播放本地音樂 和 流媒體音樂
//    _player=[[AVPlayer alloc] initWithURL:url];
    
//———————————————————————————————————————————————————————————————————————
    
    /*
      
       AVAudioPlayer 與 AVPlayer 共同屬性
     
     */
    
    //播放次數 默認爲0(播放一次)1 即是 播放2次 -1或者無窮大的數 爲無限循環
    _player.numberOfLoops=1;
    //播放音量默認1
    _player.volume=.5;
    //播放音量
    _voliSlider.value=.5;
    
//    [_player play]; //播放
//    [_player stop]; //停止
//    [_player pause]; //暫停
    
   
}

//播放事件
- (IBAction)PlayerAction:(UIButton *)sender {
    
    if (_player.isPlaying) {
        
        [_player pause];
        
        [timer invalidate];
        
    }else{
        
        [_player play];
        //開啓定時器
        timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        
       
    }
    
}

//定時器事件
- (void)timerAction:(NSTimer *)time
{
    //播放滑塊的值設定爲音樂播放當前時間
    _Slider.value=_player.currentTime;

    NSInteger timerun=(NSInteger)_player.currentTime;
    //時間 67 格式轉化爲 文本 01:07 顯示樣式
    if (timerun%60<10)
    {
        if (timerun/60>0) {
        
            NSString *test=[NSString stringWithFormat:@"0%ld:0%ld",timerun/60,timerun-60*(timerun/60)];
            _timeLabel.text=test;
            
        }else
        {
            NSString *test=[NSString stringWithFormat:@"0%ld:0%ld",timerun/60,timerun];
            _timeLabel.text=test;

        }
    }else if(timerun%60>=10)
    {
        if (timerun/60>0) {
            
            NSString *test=[NSString stringWithFormat:@"0%ld:%ld",timerun/60,timerun-60*(timerun/60)];
            _timeLabel.text=test;
            
        }else
        {
            NSString *test=[NSString stringWithFormat:@"0%ld:%ld",timerun/60,timerun];
            _timeLabel.text=test;
        }
    }

   
}

//播放進度滑動事件
- (IBAction)Slider:(UISlider *)sender {
    
    //播放當前時間等於滑動條滑動的值
    _player.currentTime=sender.value;

}

//播放音量滑動事件
- (IBAction)voliAction:(UISlider *)sender {
    
    _player.volume=sender.value;
    
}



@end







上述控件都在 Main.storyboard 畫好了 兩個UISlider也連接好了事件,相信這些簡單操作難不倒讀者吧,好了大家去試一試吧。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章