【发布时间】:2011-11-07 19:26:29
【问题描述】:
您好,我想通过我的服务器播放一个 mp3 文件,例如http://test.com/hi.mp3
如果文件位于代码目录中,则代码将播放该文件。 该代码还一次只启用一种声音。
- (IBAction)oneSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
但是这里的代码使我能够通过 http 服务器播放声音,但我可以一次播放多个声音,我需要会话中的声音,因此一次只能播放 1 个声音。我有 10 个声音。
- (IBAction)oneSound:(id)sender; {
AVPlayer *player = [[AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.mysite.com/hi.mp3"]] retain];
[player play];
}
【问题讨论】:
-
我认为你需要更清楚一点。我认为您是说您需要播放器工作,以便一次只能播放一种声音,但是您应该定义要发生的事情-声音是否应该排队播放一个接一个,是否应该忽略第二种声音而不是如果当前正在播放另一种声音时开始播放?
-
我想用第二段代码创建会话,因为声音是同时播放的,这是我不想要的。我希望声音分开播放。因此,一旦你点击一个声音,它就会播放,一旦你点击另一个声音,前一个声音就会停止,另一个声音就会开始。使用 http mp3。
标签: iphone ios xcode cocoa-touch avplayer