我似乎无法在我的应用程序中使用音频?我正在使用AVAudio框架

时间:2013-07-22 12:18:47

标签: audio avaudioplayer audio-player

帮助大家:)我已经尝试了一切,以获得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];    
}

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

1 个答案:

答案 0 :(得分: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]; 
}