码迷,mamicode.com
首页 > 其他好文 > 详细

AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件

时间:2015-02-02 22:56:38      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

下面是ipad上的调试效果

 技术分享
 


下面是代码,代码中都有注释:

#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 

AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件

标签:

原文地址:http://www.cnblogs.com/dishuichuanshi/p/4268712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!