IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
下面是ipad上的调试效果
下面是代码,代码中都有注释:
[cpp]
//
// 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>
@inte易做图ce 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>
@inte易做图ce 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"
@inte易做图ce 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.curre
补充:移动开发 , IOS ,