(使用AVFoundation.framework)
#import "AVFoundation/AVFoundation.h"所以我遇到了一个问题,我首先在我的类的init方法中尝试了这个代码(也就是首先加载的那个)。
NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops=-1;
[player play];但它不起作用,所以我决定在线程中:在init方法中:
[NSThread detachNewThreadSelector:@selector(playMusic) toTarget:self withObject:nil];和方法本身:
-(void) playMusic {
NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops=-1;
[player play];
}它当然是在*.h文件中声明的。它也不起作用。
我试着调试,在网上
}对于playMusic方法,文件会播放2到3秒,然后停止。如果没有debug,就不能播放。有什么问题吗?
发布于 2012-05-02 03:03:56
AVAudioPlayer将被立即释放。你需要留住球员。因此,将AVAudioPlayer设置为该类的实例变量。
https://stackoverflow.com/questions/10402645
复制相似问题