如何尽快自动播放音乐,我的应用程序启动? [英] How to auto play music as soon as my app starts?

查看:138
本文介绍了如何尽快自动播放音乐,我的应用程序启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,我的应用我最近已经开始。我是一个非常大的起动器的编码,所以请你帮助我,这将意味着很多。我需要一首乐曲作为用户进入我的应用程序,我按照其他YouTube教程,尽快发挥,但他们只对老式的X code版本的工作,所以请大家帮忙非常感谢你。

I need some help with my app I have recently started. I am a very big starter to coding so please do help me, it'll mean a lot. I need a piece of music to play as soon as the user enters my app, i have followed other youtube tutorials but they only work for the older Xcode versions, so please help thank you so much.

推荐答案

怎么样把它在你应用didFinishLaunching ,但一定要在你的.h实例并。米

How about putting it in you application didFinishLaunching but be sure to instantiate it in you .h and .m.

这样的事情应该做你的问题:

Something like this should do your problem:

- (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;
    }
}

这时如果你想停止它:

Then if you want to stop it:

[player stop];

或暂停:

[player pause];

和还导入它在你的头文件:

and also import it in your header file:

#import <AVFoundation/AVFoundation.h>

编辑:

您应ofcourse声明它在你的头,然后合成它。

You should to ofcourse declare it in your header, then synthesize it.

//小时,添加粗体部分:

//.h and add the bold part:

@interface的ViewController:UIViewController的&LT; AVAudioPlayerDelegate > {

@interface ViewController : UIViewController <AVAudioPlayerDelegate> {

AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;

//:M

@synthesize player;

这篇关于如何尽快自动播放音乐,我的应用程序启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆