0

I want to create an iPod-like application that will play only MP3 files I have inside the resource folder of the project.

Which way should I do it?

YogevSitton
  • 10,068
  • 11
  • 62
  • 95

2 Answers2

0

.h file

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

.m file

// 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
Community
  • 1
  • 1
Feel Physics
  • 2,783
  • 4
  • 25
  • 38