2011-05-09 48 views
1

我想读取MP3文件与AVAudioPlayer读取MP3文件与AVAudioPlayer

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

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

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

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

回答

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”和文件名。

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

0

我照你建议,但都得到了2个错误:为i386硬件架构 未定义的符号: “_OBJC_CLASS _ $ _ AVAudioPlayer”,从引用:在ViewController.o LD objc级-REF:符号(S)没有找到i386硬件架构 铛:错误:连接命令退出码1失败(使用-v看到调用)

有什么错呢?

+0

我忘了将AVFoundation框架添加到我的项目中,对不起=) – 2013-01-15 08:50:59