IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件

首先要导入AVFoundation框架及

#import <AVFoundation/AVFoundation.h>头文件

注意:要在真机上调试


下面是ipad上的调试效果


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

//
//  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.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


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值