我正在尝试为分配的音乐播放器编写代码,但我一直在尝试让滑块在歌曲播放时更新。我已经做到了,如果用户滑动滑块,歌曲将快退/快进到歌曲的那部分。有谁知道为什么在播放歌曲时滑块不会不断更新?
这是.h文件
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface AS3MPViewController : UIViewController <MPMediaPickerControllerDelegate>{
IBOutlet UIButton *playButton;
IBOutlet UIButton *stopButton;
IBOutlet UIButton *pauseButton;
IBOutlet UISlider *volumeSlider;
AVAudioPlayer *musicPlayer;
IBOutlet UIButton *pickSongButton;
IBOutlet UISlider *timeSlider;
NSTimer *timer;
}
@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UIButton *stopButton;
@property (nonatomic, retain) UIButton *pauseButton;
@property (nonatomic, retain) UISlider *volumeSlider;
@property (nonatomic, retain) AVAudioPlayer *musicPlayer;
@property (nonatomic, retain) UIButton *pickSongButton;
@property (nonatomic, retain) UISlider *timeSlider;
@property (nonatomic, retain) NSTimer *timer;
- (IBAction) play;
- (IBAction) stop;
- (IBAction) pause;
- (IBAction) changeVolume: (UISlider *)sender;
- (IBAction) chooseSong: (UIButton *)sender;
- (IBAction) changeTime: (UISlider *)sender;
- (void)setTimeSliderOptions;
@end
这是.m文件
#import "AS3MPViewController.h"
@interface AS3MPViewController ()
@end
@implementation AS3MPViewController
@synthesize playButton = _playButton;
@synthesize stopButton = _stopButton;
@synthesize pauseButton = _pauseButton;
@synthesize musicPlayer = _musicPlayer;
@synthesize volumeSlider = _volumeSlider;
@synthesize pickSongButton = _pickSongButton;
@synthesize timeSlider = _timeSlider;
@synthesize timer;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"animals001" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[self.musicPlayer prepareToPlay];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction) play {
[self.musicPlayer play];
}
- (IBAction) stop {
[self.musicPlayer stop];
self.musicPlayer.currentTime = 0;
}
- (IBAction) pause {
[self.musicPlayer pause];
}
- (IBAction) changeVolume: (UISlider *)sender {
self.musicPlayer.volume = [sender value];
}
- (IBAction)chooseSong:(UIButton *)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
[mediaPicker setDelegate:self];
[mediaPicker setAllowsPickingMultipleItems:NO];
mediaPicker.prompt = NSLocalizedString(@"Add Some Songs To Play", "Prompt In Media Item Picker");
[self presentModalViewController: mediaPicker animated:YES];
}
- (void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[self dismissModalViewControllerAnimated:YES];
}
- (void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems: (MPMediaItemCollection *)mediaItemCollection {
MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
//AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
//AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
self.timeSlider.maximumValue = [self.musicPlayer duration];
self.musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.musicPlayer play];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(songCurrentTime) userInfo:nil repeats:YES];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)changeTime: (UISlider *)sender {
self.musicPlayer.currentTime = self.musicPlayer.duration * sender.value;
}
- (void)songCurrentTime {
timeSlider.value = (self.musicPlayer.currentTime / self.musicPlayer.duration);
}
@end