ios - 更新歌曲时间标签

标签 ios objective-c avaudioplayer mpmediaitem

该应用是一个音乐播放器,我需要显示一个 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,但我似乎无法使用它。

最佳答案

第一步

在你的 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];


}

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

关于ios - 更新歌曲时间标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634856/

相关文章:

ios - 恢复 iOS 团队配置文件

iphone 按钮文本颜色在单击时自行改变?

ios - 通过 Segue 转发数据(objective-c)

iphone - 以编程方式检查连接到设备的耳机?

ios - 无法导入 AVFoundation.framework,所以应用无法播放音乐?

ios - 居中 UILabel 不起作用?

iphone - 修改文件 IOS 的 UTF 时间

ios - 每次重新加载表格时图像闪烁(iOS,objective-C)

ios - AVAudioPlayer 速率到 BPM 转换

ios - 核心数据中的部分为零值