播放资源MP3

时间:2012-08-11 14:47:45

标签: iphone objective-c xcode avaudioplayer mpmusicplayercontroller

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

我应该采用哪种方式?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)