0

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

スライダー プロパティを設定し、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でテストするので、シミュレーターの問題でしょうか?

4

1 に答える 1

0

2つのこと:

  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];
    }
    
于 2013-09-01T20:03:04.707 に答える