3

我正在尝试为分配的音乐播放器编写代码,但我一直在尝试让滑块在歌曲播放时更新。我已经做到了,如果用户滑动滑块,歌曲将快退/快进到歌曲的那部分。有谁知道为什么在播放歌曲时滑块不会不断更新?

这是.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
4

7 回答 7

2

检查您已将 IBAction 方法与带有 ValueChanged EX 的滑块连接的连接:

- (IBAction)changeTime: (UISlider *)sender {

在 IB 连接时检查此方法是否与 ValueChanged 连接

于 2013-07-03T16:30:04.653 回答
1

我认为您的问题是您正在从主线程以外的线程更新滑块的值。尝试修改 songCurrentTime 方法,使其在主线程上调用另一个更新滑块值的方法。

- (void)songCurrentTime {
[self performSelectorOnMainThread:@selector(songCurrentTimeMain) withObject:nil waitUntilDone:YES];
}

- (void)songCurrentTimeMain {
timeSlider.value = (self.musicPlayer.currentTime / self.musicPlayer.duration);
}

当然,要获得正确的结果,请确保滑块的最小值为 0,最大值为 1。

此致

于 2013-01-03T22:35:20.343 回答
0

timeSliderself.timeSlider不是一回事。您声明的大多数 iVar 都是不必要的(并且具有误导性),因为您使用属性名称的下划线形式来合成您的属性。例如,您有一个timeSlider_timeSlideriVar 支持的属性,而timeSlideriVar 则完全不同。

使用self.timeSlider.value = ....

于 2012-11-23T15:51:15.660 回答
0

设置滑块值时,该值始终小于或等于 1,因为self.musicPlayer.currentTime始终小于或等于self.musicPlayer.duration。相反,试试这个

timeSlider.value = self.musicPlayer.currentTime;
于 2012-11-23T15:16:54.370 回答
0

我在教程中做了类似的事情,我添加滑块的方式与您所做的相同,除了我使用了

musicPlayer.currentPlaybackTime

作为分配给滑块的值。

(self.musicPlayer.currentTime / self.musicPlayer.duration) 的结果可能是对您的滑块值的不准确分配。

于 2012-11-23T15:23:57.763 回答
0

对我来说,问题不同,我需要为滑块设置初始值,但在设置最大值和最小值之前更新滑块值是一个愚蠢的错误。

错误代码 :

float value = [self getSliderDefaultValue];
[slider setValue:value animated:NO];
slider.maximumValue = numberOfSteps;
slider.minimumValue = 0;

工作代码:

slider.maximumValue = numberOfSteps;
slider.minimumValue = 0;
float value = [self getSliderDefaultValue];
[slider setValue:value animated:NO];

希望它会帮助别人!!!

于 2019-07-12T14:57:32.437 回答
0

在我的情况下,我的 UISlider 上有透明的视图,我俯瞰着它。

于 2019-11-12T21:12:41.147 回答