2011-05-09 65 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