应用音频不播放

时间:2015-01-18 13:50:01

标签: ios objective-c audio avaudioplayer

因此,在我的文件中,我有以下代码,意图是在后台播放音乐文件:

-(void) BackGroundMusic {
   NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"LostInTheSea" ofType:@"mp3"];
   SystemSoundID soundID;
   AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
   AudioServicesPlaySystemSound(soundID);
}

然后在视图中加载了我有的部分:

[self BackGroundMusic];

我添加了所有工具箱,例如AVFoundation和AudioToolbox,但我听不到任何音频播放。请帮忙!

2 个答案:

答案 0 :(得分:0)

根据AudioServicesPlaySystemSound的{​​{3}}:

使用此功能播放的声音文件必须是:

No longer than 30 seconds in duration

In linear PCM or IMA4 (IMA/ADPCM) format

Packaged in a .caf, .aif, or .wav file

答案 1 :(得分:0)

用户523234解释说,你不应该使用系统声音播放mp3文件。 请改用AVAudioPlayer。这是一个无限循环的例子:

#import "AVFoundation/AVAudioPlayer.h"
#import "AVFoundation/AVAudioSession.h"

-(void)playbgMusic {
    AVAudioPlayer *soundbgMusic;
    NSString *soundPath;
    soundPath= [[NSBundle mainBundle] pathForResource:@"bgMusic" ofType:@"mp3"];
    soundbgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
    soundbgMusic.volume = 1.0;
    soundbgMusic.numberOfLoops = -1;
    [soundbgMusic prepareToPlay];
    soundbgMusic.currentTime = 0.0;
    [soundbgMusic play];
}