IOS与AVFoundation音乐播放器有关,50%的时间都在工作

时间:2013-09-01 19:45:42

标签: ios objective-c

我对AVFoundation音乐播放器有一些问题。

1)它没有立即启动,我想模拟器上的缓冲速度很慢?

2)它只在50%的时间内以声音开始,另外50%它没有开始,非常不可靠

这是我的班级

Music_Player.h

@interface Music_Player : NSObject <AVAudioPlayerDelegate>

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

@property (nonatomic, retain) NSString *trackPlaying;

@property (nonatomic) BOOL isPlaying;

@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, retain) UISlider *slider;

-(void)initTrack: (NSString *) track;

-(void)startPlayer;
-(void)pausePlayer;
-(void)stopPlayer;

-(void)sliderUp;
-(void)sliderDown;

@end

Music_Player.m

#import "Music Player.h"

@implementation Music_Player


@synthesize audioPlayer;

@synthesize trackPlaying;
@synthesize timer;

@synthesize isPlaying;

@synthesize slider;

-(void)initTrack:(NSString *)track
{
    /* Init slider */

    self.isPlaying = FALSE;
    self.trackPlaying = track;

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *filePath = [mainBundle pathForResource:self.trackPlaying ofType:@"mp3"];
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];

    NSError *error = nil;

    self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
    [self.audioPlayer prepareToPlay];

    /* Set slider max value */
    self.slider.minimumValue = 0;
    self.slider.maximumValue = self.audioPlayer.duration - 5;
}

-(void)startPlayer
{
    if (self.isPlaying == TRUE)
    {
        NSLog(@"Pause clicked");
        [self.audioPlayer pause];
        self.isPlaying = FALSE;

    } else {
        NSLog(@"Play clicked");
        [self.audioPlayer play];
        self.isPlaying = TRUE;

        if (![self.timer isValid]) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
        }
    }
}

- (void)updateTime {

    if (self.isPlaying == TRUE) {

        NSTimeInterval currentTime = self.audioPlayer.currentTime;

        NSLog(@"%f", currentTime);
        // update UI with currentTime;
        slider.value = round(currentTime);
    }
}

-(void)pausePlayer
{
    if (self.isPlaying == TRUE)
    {
        [self.audioPlayer pause];
        self.isPlaying = FALSE;
    }
}

-(void)stopPlayer
{
    if (self.isPlaying == TRUE)
    {
        NSLog(@"Stop clicked");
        [self.audioPlayer stop];
        self.audioPlayer.currentTime = 0;
        self.slider.value = round(self.audioPlayer.currentTime);
        self.isPlaying = FALSE;
    }
}

-(void)sliderUp
{
    if (self.isPlaying == FALSE)
    {
        self.audioPlayer.currentTime = round(slider.value);
        [self.audioPlayer play];
        self.isPlaying = TRUE;
    }
}

-(void)sliderDown
{
    if (self.isPlaying == TRUE)
    {
        self.isPlaying = FALSE;
        [self.audioPlayer stop];
    }
}

/* AUDIO PLAYER DELEGATES */

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"Did finish with, %c", flag);
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Error %@",error);
}

@end

我只是设置了slider属性并使用viewController

中的一个轨道来启动它
/* Init Music */
self.music_player = [[Music_Player alloc] init];
self.music_player.slider = self.slider;

[self.music_player initTrack:@"track1"];

然后我只是将Btn点击和滑块值更改传递给music_player类,可能是什么问题?我将在iPhone tomorow上测试它,那么它只是一个模拟器问题吗?

1 个答案:

答案 0 :(得分:0)

两件事:

  1. initWithData:error:中,被调用后是否设置了错误?

  2. 为什么不使用[AVAudioPlayer initWithContentsOfURL:error:]?例如:

    - (void)initTrack:(NSString *)track
    {
        self.isPlaying = NO;
        self.trackPlaying = track;
    
        NSBundle *mainBundle = ;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:self.trackPlaying ofType:@"mp3"];
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    
        NSError *error = nil;
    
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
    }