3

我需要一些关于我最近启动的应用程序的帮助。我是编码的一个非常重要的初学者,所以请帮助我,这将意味着很多。用户进入我的应用程序后,我需要播放一段音乐,我遵循了其他 youtube 教程,但它们仅适用于较旧的 Xcode 版本,因此非常感谢您。

4

3 回答 3

4

把它放在你身上怎么样,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;
于 2012-07-03T01:50:40.953 回答
0

您可以使用AVAudioPlayer. 使用起来非常简单:

NSURL *path = [[NSURL alloc] initFileURLWithPath:[DataPersister getQualifiedPathForFileInDirectory:DataPersisterPathBundle fileName:@"music.mp3"]];
NSError *outError;

AVAudioPlayer *musicSound = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:&outError];
[musicSound play];
[path release];

记得导入

#import <AVFoundation/AVFoundation.h>
于 2012-07-02T21:21:45.327 回答
0
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MUSIC.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
audioPlayer.volume = 1.0;
audioPlayer.currentTime = 2;

使用这个 at- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法肯定会播放音乐

快乐编码 ;)

于 2012-07-03T09:38:40.700 回答