read mp3 file with AVAudioPlayer
i would like to read mp3 file with AVAudioPlayer
#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];
}
no error no crash but also no read
Make your header file look like this
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface FirstViewController : UIViewController <AVAudioPlayerDelegate> {
IBOutlet UIScrollView *LLScrollView;
AVAudioPlayer *data;
}
- (IBAction) playSound;
@end
and make the implementation look like this
#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
If your sound file is named "example.mp3" then in initWithFormat only type "example" and the file name.
This code works flawlessly. Good Luck.
I did as you recommended to, but have got 2 errors: Undefined symbols for architecture i386: "_OBJC_CLASS_$_AVAudioPlayer", referenced from: objc-class-ref in ViewController.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)
what is wrong with it?
精彩评论