我想创建一个类似iPod的应用程序,它只播放我在项目资源文件夹中的MP3文件。
我应该采用哪种方式?
答案 0 :(得分:0)
#include <AVFoundation/AVFoundation.h>
@interface audioplayer : NSObject
<AVAudioPlayerDelegate>
{
NSTimer * timer;
AVAudioPlayer * audio;
}
@end
// start plying music
@implementation audioplayer
- (void)main
{
[super viewDidLoad];
NSString * path = [[NSBundle mainBundle] pathForResource:@"soundfile" ofType:@"mp3"];
NSURL * url = [NSURL fileURLWithPath:path];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
audio.delegate = self;
[audio play];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playSequence) userInfo:nil repeats:YES];
}
// display the time playing
- (void) playSequence{
if(audio.playing){
NSLog(@"%@", [NSString stringWithFormat:@"%f", audio.currentTime]);
} else {
[timer invalidate];
}
}
@end
答案 1 :(得分:0)