我需要一些关于我最近启动的应用程序的帮助。我是编码的初学者,所以请帮助我,这意义重大。我需要在用户进入我的应用程序后立即播放一段音乐,我已经关注了其他 youtube 教程,但它们只适用于较旧的 Xcode 版本,所以请帮忙,非常感谢。
最佳答案
把它放在你的 application didFinishLaunching
中如何,但一定要在你的 .h 和 .m 中实例化它。
这样的事情应该可以解决你的问题:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.wav"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;
//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
player.delegate = self;
[player play];
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume = 1.0;
}
}
那么如果你想停止它:
[player stop];
或暂停它:
[player pause];
并将其导入到您的头文件中:
#import <AVFoundation/AVFoundation.h>
编辑:
你当然应该在你的头文件中声明它,然后合成它。
//.h 并添加粗体部分:
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
//.m
@synthesize player;
关于iphone - 如何在我的应用程序启动后立即自动播放音乐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301315/