iphone - 我的应用程序中的音频,播放暂停 slider

标签 iphone ios xcode audio avaudioplayer

伙计们,我在将 UISlider 合并到我的音频应用程序中时遇到错误,任何建议都会很好,这是我的第一个应用程序,我是新手......

轨道播放得很好,停止得也很好,但是当我尝试跳过应用程序崩溃时,我确信我错过了一些东西,只是需要对代码有一些新的了解,希望有人能提供建议......

NoGraceViewController.h

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

@interface NoGraceViewController : UIViewController<AVAudioPlayerDelegate> {
    AVAudioPlayer *audioPlayer;
    UISlider *volumeControl;
}

@property(nonatomic,retain)IBOutlet UISlider *volumeControl;
@property(nonatomic,retain) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) IBOutlet UITextField *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *skipSlider;

-(IBAction) nograceButton;

-(IBAction)playbutton;

-(IBAction)stopbutton;

-(IBAction)skipSliderMoved;

@end

NoGraceViewController.m

#import "NoGraceViewController.h"

@interface NoGraceViewController ()

@end

@implementation NoGraceViewController
@synthesize skipSlider;
@synthesize timeLabel;

-(IBAction) nograceButton { 
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"http://itunes.apple.com/us/album/no-grace-original-mix-single/id392597812?uo=4"]];
}

@synthesize audioPlayer;

- (IBAction)playbutton
{
    [audioPlayer play];

}

-(IBAction)stopbutton
{
    [audioPlayer stop];
}

- (IBAction)skipSliderMoved:(UISlider *)sender {
    self.audioPlayer.currentTime = self.audioPlayer.duration*sender.value;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"001.mp3" ofType:nil]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@", 
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;
        [audioPlayer prepareToPlay];
    }

}

- (void)viewDidUnload {
    [self setSkipSlider:nil];
    [self setTimeLabel:nil];

    self.volumeControl=nil;
    self.audioPlayer=nil;
}

-(void)updateDisplay { 
    self.skipSlider.value = self.audioPlayer.currentTime / self.audioPlayer.duration; 
    NSString *timeString = [[NSString alloc] initWithFormat:@"%d:%02d", (int)   self.audioPlayer.currentTime / 60,(int) self.audioPlayer.currentTime % 60];
    self.timeLabel.text = timeString;
}

@end

最佳答案

试试这个:

self.audioPlayer.currentTime = (self.audioPlayer.duration / 100) * sender.value;

关于iphone - 我的应用程序中的音频,播放暂停 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11021247/

相关文章:

ios - iOS 7.1 中的 SDWebImage 崩溃

ios - 将 NSMutableDictionary 设置为 nil 是否会释放对其所有内容的引用?

iphone - 我想在 Iphone 中将 NSData 转换为双倍值

c++ - 使用 gdb 调试 C++

xcode - xcode 构建后关闭 React Packer

iphone - (iPhone) 如何使用 OS 4.0 MapKit 实现可拖动图钉?

iphone - Storyboard没有堆叠?

iPhone - 释放资源时出现问题(xib/view/viewcontroller)

iphone - iOS:当 iphone 处于 sleep 模式或当我按下主页按钮时执行 NSURLConnection

xcode - 包含启动图像的 Assets 目录有什么问题?