AVFoundation可以在IOS 5.0模拟器上播放音频并获取它的详细信息吗?

时间:2012-07-05 07:49:15

标签: iphone xcode ios5

只能使用AVFoundation.framework完成上述操作吗?

我尝试做了一些实验,但我无法真正开始工作

(抱歉新手在这里请告诉我在哪里放置代码示例:ViewController或MainView)

这些是我的代码,(UI组件在里面因为我不想使用界面构建器)

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSError *error;
        buttonPlay = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, 100, 50)];

        buttonPlay.text = @"Play";

        // Get the file path to the song to play.
        NSString* path;
        NSURL* url;

        path = [[NSBundle mainBundle] pathForResource:@"Bring_Me_To_Life.mp3" ofType:nil];
        url = [NSURL fileURLWithPath:path];

        NSData *_objectData = [NSData dataWithContentsOfURL:url];

        //Initialize the AVAudioPlayer.
        audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];

        // Preloads the buffer and prepares the audio for playing.
        audioPlayer.numberOfLoops = 0;
        audioPlayer.volume = 1.0f;
        [audioPlayer prepareToPlay];

        if (audioPlayer == nil)
            NSLog(@"%@", [error description]);
        else
            [audioPlayer play];

        [self addSubview:buttonPlay];
    }
    return self;
}

-(void)touchesBegan:(CGPoint)location{
    if(CGRectContainsPoint(buttonPlay.frame, location)){
        NSLog(@"Dec from Chl %f",[audioPlayer peakPowerForChannel:0]);
        NSLog(@"Length of Clip %f",[audioPlayer duration]);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个 audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];