0

我想创建一个类似 iPod 的应用程序,它只播放项目资源文件夹中的 MP3 文件。

我应该采取哪种方式?

4

2 回答 2

0

.h 文件

#include <AVFoundation/AVFoundation.h>
@interface audioplayer : NSObject 
<AVAudioPlayerDelegate>
{
    NSTimer * timer;
    AVAudioPlayer * audio;
}
@end

.m 文件

// 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
于 2012-08-11T15:46:40.303 回答