iphone - 如何在我的应用程序启动后立即自动播放音乐?

标签 iphone ios xcode audio

我需要一些关于我最近启动的应用程序的帮助。我是编码的初学者,所以请帮助我,这意义重大。我需要在用户进入我的应用程序后立即播放一段音乐,我已经关注了其他 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/

相关文章:

ios - 应用在启动时崩溃

iphone - Xcode 在自动继续断点处停止

iphone - NSUndoManager 以及如何让重做工作

iOS:如何在设备上运行 XCTest?

xcode - iOS5 iPad UIPopoverController initWithContentViewController NSGenericException

ios - 无法创建配置文件 iOS - 免费配置文件

iphone - 如何从应用程序目录中的视频中抓取单帧?

android - 升级 flutter 版本 2.0 时出错

ios - 如何以编程方式显示具有 Storyboard 中定义的导航 Controller 和标签栏 Controller 的 Controller ?

iOS 正则表达式匹配非空白字符,直到 @ 符号后的第一个非单词字符