我最近开始使用我的应用程序需要一些帮助。我是一个非常大的编码起点,所以请帮助我,这意味着很多。当用户输入我的应用程序时,我需要播放一段音乐,我已经关注了其他youtube教程,但它们仅适用于较旧的Xcode版本,所以请非常感谢你们。
答案 0 :(得分: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&lt; AVAudioPlayerDelegate &gt; {
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
//。米
@synthesize player;
答案 1 :(得分: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>
答案 2 :(得分: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;
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法使用此功能肯定会播放音乐
快乐编码;)