0

帮助大家:) 我已经尽一切努力让 AVAudioPlayer 的音频在我的应用程序中工作。由于某种原因,它不会在 iPhone 模拟器上播放。代码如下——

#import "ViewController.h"
#import "AVFoundation/AVFoundation.h"

@interface ViewController ()
{
    AVAudioPlayer *avPlayer;
}

@end

@implementation ViewController
@synthesize myProgressView;
@synthesize sliderVolumeOutlet;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"audioapp" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:stringPath];

    NSError *error;

    avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    [avPlayer setNumberOfLoops:1];

    [avPlayer setVolume:self.sliderVolumeOutlet.value];
    [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];    
}

歌曲本身在项目文件中,我真的不知道我做错了什么。

4

1 回答 1

0

检查错误并尝试

-(IBAction) playAudio{
        NSError *error;
        NSURL *url = [NSURL fileURLWithPath:self.audioName];

        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.delegate = self;
        if (error)
            NSLog(@"Error: %@", [error localizedDescription]);
        else
            [audioPlayer play]; 
}
于 2013-07-22T12:22:22.990 回答