Play Resource MP3s [closed]
-
25-06-2021 - |
Question
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?
Solution
.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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow