【问题标题】:why am i getting progress slider exception error为什么我会收到进度滑块异常错误
【发布时间】:2014-05-05 06:39:01
【问题描述】:

我正在制作一个将播放音频的应用程序。我使用滑块在滑块中以图形方式显示经过的时间和剩余时间。但我不知道如何以编程方式获取值。我使用了以下代码,但是它抛出异常。

 -(void) updateMyProgress
{
  float progress = [avPlayer currentTime]/[avPlayer duration];
  self.myProgressView.progress = progress;
} 

我的 viewcontoller.m 文件中的代码是

#import "ViewController.h"

@interface ViewController ()
{
    AVAudioPlayer *avPlayer;
    AVPlayer *avp;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"naat" ofType:@"mp3"];
   NSURL *url = [NSURL fileURLWithPath:stringPath];
  NSError *error;
   avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
  [avPlayer setNumberOfLoops:2];
  [avPlayer setVolume:self.sliderVolumeOutlet.value];
  [NSTimer scheduledTimerWithTimeInterval:1 target:self      selector:@selector(updateMyProgress) userInfo:nil repeats:YES];


 }
/*-(void) updateMyProgress
{
 CGFloat progress = [avPlayer currentTime]/[avPlayer duration];
self.myProgressView.progress = progress;
}
*/
-(void) updateMyProgress

{

AVPlayerItem *currentItem = avp.currentItem ;

CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time

CGFloat progress = currentTime.value/duration.value;

self.myProgressView.progress = progress; 
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)sliderVolumeAction:(id)sender
{
UISlider *myslider= sender;
[avPlayer setVolume:myslider.value];
}

- (IBAction)stopButton:(id)sender
{
[avPlayer stop];
[avPlayer setCurrentTime:0];
}

- (IBAction)pauseButton:(id)sender
{
[avPlayer pause];
}

- (IBAction)playButton:(id)sender
{
[avPlayer play];
}
@end![enter image description here][1]

附图为异常截图

! [1]:http://i.stack.imgur.com/WsxtI.png

【问题讨论】:

  • 哪行代码引发了异常?哪个期望被抛出?
  • 你可以在这里发布错误信息吗?

标签: ios objective-c avaudioplayer


【解决方案1】:
  • AVPlayer 没有名为“duration”的属性。
  • “持续时间”是 AVPlayerItem 的属性。
  • duration 和 currentTime 都是“CMTime”数据类型
  • 我们必须获取 CMTime 的值,然后更新进度。

-(void) updateMyProgress

{

    AVPlayerItem *currentItem = avPlayer.currentItem;

    CMTime duration = currentItem.duration; //total time
    CMTime currentTime = currentItem.currentTime; //playing time

    CGFloat progress = currentTime.value/duration.value;

    self.myProgressView.progress = progress;
}

【讨论】:

  • 感谢您的快速响应。在实施您发布的方法后,我收到此错误:在“AVAudioPlayer *”类型的对象上找不到属性“currentItem”。请帮助
  • "currentItem" 是 AVPlayer 不是 AVAudioPlayer 的属性
  • 我已使用 AvPlayer 进行了定义,但出现异常。我已经粘贴了 viewcontroller.m 文件。请帮助我。
  • @user3571734,必须粘贴什么?我没有得到你的问题。 developer.apple.com/library/ios/documentation/AVFoundation/…查看文档,你会发现CurrentItem是AVPlayer的属性
  • 我已将 viewcontroller.m 文件粘贴到我的主要问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 2013-02-10
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多