只能使用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]);
}
}
答案 0 :(得分:0)
试试这个 audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];