【问题标题】:Update Song time label更新歌曲时间标签
【发布时间】:2016-06-04 20:46:03
【问题描述】:

该应用程序是一个音乐播放器,我需要显示一个trackTimeLabel,它会在歌曲期间更新播放时长。

因此,每秒更新一次标签的播放曲目持续时间。

现在我正在做一个 hack,我只是计算与歌曲无关的秒数,但这不是一个好的解决方案:

- (void)viewWillAppear:(BOOL)animated {
    currentItem = [musicPlayer nowPlayingItem];
    _titleLabel.text = [self currentItemValue:MPMediaItemPropertyTitle];
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerDidTick:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void) timerDidTick:(NSTimer*) theTimer{
    long currentPlaybackTime = musicPlayer.currentPlaybackTime;
    int currentHours = (int)(currentPlaybackTime / 3600);                         
    int currentMinutes = (int)((currentPlaybackTime / 60) - currentHours*60);     // Whole minutes
    int currentSeconds = (currentPlaybackTime % 60);                        
    _trackTimeLabel.text = [NSString stringWithFormat:@"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
}

Apple 有一个 MPMediaItem 类,我可以得到一个 MPMediaItemPropertyPlaybackDuration 用于轨道,但我似乎无法得到任何使用它的东西。

【问题讨论】:

  • 您是否想要获得在曲目播放期间剩余或已用时间的实际实时读数?
  • @CraigSmith 没错,对不起,如果我解释得不够好,但你明白了。有什么想法吗?

标签: ios objective-c avaudioplayer mpmediaitem


【解决方案1】:

第 1 步

在你的 ViewController.h 中

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController
{
    AVAudioPlayer * player;
}

@property (weak, nonatomic) IBOutlet UISlider *seekSlider;

@property (weak, nonatomic) IBOutlet UILabel *lbl;

第 2 步

在您的 ViewController.m 中

    - (void)viewDidLoad {
    NSURL * fileURL = [[NSBundle mainBundle] URLForResource:@"01 - Saturday Saturday - DownloadMing.SE" withExtension:@"mp3"];

    NSError * error = nil;

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:&error];



    if(error)
        NSLog(@"Error : %@ ", error);

    [player prepareToPlay];

    player.volume = 0.5;

    self.seekSlider.maximumValue = player.duration;
}
-(void)timerMethod:(NSTimer *)timer
{


    float progress = player.currentTime;
    //    if(!self.seekSlider.isFocused)
    self.seekSlider.value = progress;

    _lbl.text = [NSString stringWithFormat:@"%.f:%.2d", (progress / 60), ((int)progress % 60 )];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)play_pause_clicked:(id)sender {


    if(player.playing)
    {
        [player pause];
        [(UIButton*)sender setTitle:@"Play" forState:UIControlStateNormal];
    }
    else
    {
        [player play];
        [(UIButton*)sender setTitle:@"Pause" forState:UIControlStateNormal];

        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];


    }


}


- (IBAction)seekPlayer:(id)sender {

    player.currentTime = [(UISlider*)sender value];


}
- (IBAction)changeVolume:(id)sender {

    player.volume = [(UISlider*)sender value];


}

这对我来说是完美的工作代码......试试这个,我希望这会对你有所帮助:)

【讨论】:

  • 谢谢!无论如何,我的时间都是 0:00。另外,我只是用NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; 调用它还是用它做一些不同的事情?
  • 你没有解决你的问题吗??...如果没有,现在是什么错误??
  • 无论歌曲的播放时间是什么时候,我都会得到 0:00 作为时间。我应该从哪里调用这些方法?
  • 见我更新的答案兄弟..这肯定会帮助你
  • 那没用,我有大部分的东西。感谢您的帮助-
猜你喜欢
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多