[英]How to play background audio using AVFoundation for iPhone/iPoad Touch with iOS 4?
[英]Can AVFoundation play Audio and get Details of it On the IOS 5.0 Simulator?
可以僅使用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]);
}
}
試試這個audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url錯誤:nil];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.