你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS學習:AVAudioPlayer播放音樂文件及讀取ipod庫中的音樂文件

IOS學習:AVAudioPlayer播放音樂文件及讀取ipod庫中的音樂文件

編輯:關於IOS

下面是ipad上的調試效果

IOS學習:AVAudioPlayer播放音樂文件及讀取ipod庫中的音樂文件
下面是代碼,代碼中都有注釋:

 

//  
//  RootViewController.h  
//  SoundDemo  
//  
//  Created by on 13-6-21.  
//  Copyright (c) 2013年 DoubleMan. All rights reserved.  
//  
 
#import <UIKit/UIKit.h>  
#import <AVFoundation/AVFoundation.h>  
#import <MediaPlayer/MediaPlayer.h>  
 
@interface RootViewController : UIViewController <AVAudioPlayerDelegate> 
{ 
    AVAudioPlayer *player; 
} 
 
@property (nonatomic, retain) AVAudioPlayer *player; 
@property (nonatomic, retain) UISlider *slider; 
@property (nonatomic, retain) NSTimer *timer; 
 
@end 

//
//  RootViewController.h
//  SoundDemo
//
//  Created by on 13-6-21.
//  Copyright (c) 2013年 DoubleMan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface RootViewController : UIViewController <AVAudioPlayerDelegate>
{
    AVAudioPlayer *player;
}

@property (nonatomic, retain) AVAudioPlayer *player;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) NSTimer *timer;

@end

[cpp]
//  
//  RootViewController.m  
//  SoundDemo  
//  
//  Created by on 13-6-21.  
//  Copyright (c) 2013年 DoubleMan. All rights reserved.  
//  
 
#import "RootViewController.h"  
 
@interface RootViewController () 
 
@end 
 
@implementation RootViewController 
 
@synthesize player; 
@synthesize slider; 
@synthesize timer; 
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
        // Custom initialization  
         
         
    } 
    return self; 
} 
 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
     
    UIButton *musicPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    musicPlay.frame = CGRectMake(10, 10, 90, 35); 
    [musicPlay setTitle:@"Play" forState:UIControlStateNormal]; 
    [musicPlay addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:musicPlay]; 
     
    UIButton *pause = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    pause.frame = CGRectMake(115, 10, 90, 35); 
    [pause setTitle:@"Pause" forState:UIControlStateNormal]; 
    [pause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:pause]; 
     
    UIButton *stop = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    stop.frame = CGRectMake(220, 10, 90, 35); 
    [stop setTitle:@"stop" forState:UIControlStateNormal]; 
    [stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:stop]; 
     
    slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 65, 300, 20)]; 
    [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:slider]; 
     
    //   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"找一個相愛的理由-晨熙-艾歌" ofType:@"wav"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    // 設置循環次數,-1為一直循環  
    player.numberOfLoops = -1; 
    // 准備播放  
    [player prepareToPlay]; 
    // 設置播放音量  
    player.volume = 50; 
    // 當前播放位置,即從currentTime處開始播放,相關於android裡面的seekTo方法  
    player.currentTime = 15; 
    // 設置代理  
    player.delegate = self; 
    int dur = player.duration; 
    slider.maximumValue = dur; 
     
    // 一秒一次更新播放進度  
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES]; 
     
    // 從ipod庫中讀出音樂文件  
//    MPMediaQuery *everything = [[MPMediaQuery alloc] init];  
//    // 讀取條件  
//    MPMediaPropertyPredicate *albumNamePredicate =  
//    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType];  
//    [everything addFilterPredicate:albumNamePredicate];  
//      
//    NSLog(@"Logging items from a generic query...");  
//    NSArray *itemsFromGenericQuery = [everything items];  
//    for (MPMediaItem *song in itemsFromGenericQuery) {  
//        NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];  
//        NSLog (@"%@", songTitle);  
//    }  
//      
//    [everything release];  
} 
 
// 更新播放進度  
- (void)updateSlider { 
    slider.value = player.currentTime; 
} 
 
// 進度滑塊變化時,跳轉到進度播放  
- (void)sliderValueChange:(UISlider *)mSlider { 
    player.currentTime = mSlider.value; 
    NSLog(@"value: %.0f", mSlider.value); 
} 
 
// 停止  
- (void)stop { 
    player.currentTime = 0; 
    [player stop]; 
} 
 
// 暫停  
- (void)pause { 
    [player pause]; 
    NSLog(@"pause"); 
} 
 
// 開始播放  
- (void)playMusic { 
    NSLog(@"start play"); 
    [player play]; 
} 
 
#pragma mark - AVAudioPlayerDelegate  
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
    // 播放完成時調用   只有當播放結束時才會調用,循環播放時不會調  
    [timer invalidate]; 
    NSLog(@"audioPlayerDidFinishPlaying"); 
} 
 
/* if an error occurs while decoding it will be reported to the delegate. */ 
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { 
    // 解碼出錯時調用  
} 
 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated.  
} 
 
- (void)dealloc 
{ 
    [player stop]; 
    [player release]; 
    [slider release]; 
    [timer release]; 
    [super dealloc]; 
} 
 
@end 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved