用AVAudioPlayer读取mp3文件

时间:2011-05-09 21:19:05

标签: objective-c avaudioplayer

我想用AVAudioPlayer读取mp3文件

#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;

@property(retain,nonatomic) AVAudioPlayer *player;
@synthesize player

-(IBAction) lirepiste
{
    NSString *file =@"http://media.islamway.net/lessons/othymeen/40nowaweyya/1.mp3";
    player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:file,error:nil]; 
    [player play];
}

没有错误没有崩溃但也没有阅读

2 个答案:

答案 0 :(得分:1)

使您的标头文件看起来像这样

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FirstViewController : UIViewController <AVAudioPlayerDelegate> {
    IBOutlet UIScrollView *LLScrollView;
    AVAudioPlayer *data;
}
- (IBAction) playSound;
@end

并使实现看起来像这样

#import "FirstViewController.h"

@implementation FirstViewController


- (IBAction) playSound {

    NSString *name = [[NSString alloc] initWithFormat:@"SoundName"];
    NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
    if (data) {
        [data stop];
        [data release];
        data = nil;
    }
    data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
    data.delegate = self;
    [data play];
    }

//everything else...


@end

如果你的声音文件名为“example.mp3”,那么在initWithFormat中只输入“example”和文件名。

此代码完美无瑕。祝你好运。

答案 1 :(得分:0)

我按照你的推荐做了,但有两个错误: 架构i386的未定义符号:   “_ OBJC_CLASS _ $ _ AVAudioPlayer”,引自:       ViewController.o中的objc-class-ref ld:找不到架构i386的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

它出了什么问题?

相关问题