[英]Play audio files in iPhone/iPad application?
I want to play sound in my application for that I am using .mp3 file type. 我想在我的应用程序中播放声音,因为我使用的是.mp3文件类型。 It is working fine in Simulator but when I am lunching it to my device it is not working and not producing any sound can any one help me why it is not working on device or which file format is better to play audio file in iPhone and iPad?
它在Simulator中可以正常工作,但是当我将其午餐到设备中时,它却无法工作并且不发出任何声音,谁能帮助我为什么它不能在设备上工作,或者哪种文件格式更适合在iPhone和iPad中播放音频文件?
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: [NSString stringWithFormat:@"%@",SoundFileName] withExtension: @""];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
AudioServicesPlaySystemSound (soundFileObject);
Above is mine code to play sound 上面是我的代码播放声音
Thank you. 谢谢。
The system sound IDs won't play if the device is muted, so this is possibly the cause. 如果设备被静音,则系统声音ID将不会播放,因此这可能是原因。 (They're only really for the purpose of user interface effects such as alerts, etc. as per the official documentation .)
(根据官方文档 ,它们仅实际上是为了实现用户界面效果,例如警报等)。
As such, I'd be tempted to use the AVAudioPlayer style approach. 因此,我很想使用AVAudioPlayer风格的方法。 As a sample (no pun intended) implementation:
作为示例(无双关语)实现:
// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>
...
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample_name" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}
// *** In your dealloc... ***
[testAudioPlayer release];
You should also remember to set the appropriate audio category. 您还应该记住设置适当的音频类别。 (See the AVAudioSession
setCategory:error:
method.) (请参阅AVAudioSession
setCategory:error:
方法。)
Finally, you'll need to add the AVFoundation library to your project. 最后,您需要将AVFoundation库添加到您的项目中。 To do this, alt click on your project's target in the Groups & Files column in Xcode, and select "Get Info".
为此,请在Xcode的“组和文件”列中,单击项目的目标,然后选择“获取信息”。 Then select the General tab, click the + in the bottom "Linked Libraries" pane and select "AVFoundation.framework".
然后选择“常规”选项卡,单击底部“链接的库”窗格中的+,然后选择“ AVFoundation.framework”。
I honestly would suggest using AVAudioPlayer
instead of this, but you could try using this alternate code instead. 老实说,我建议使用
AVAudioPlayer
代替它,但是您可以尝试使用此替代代码。 In your example it looks like you forgot to include the extension, which might be the problem also. 在您的示例中,您似乎忘了包括扩展名,这也可能是问题所在。 The simulator is more lenient on naming, so if you forgot the extension it might play on the simulator and not a device.
模拟器在命名方面更为宽容,因此,如果您忘记了扩展名,它可能会在模拟器而不是设备上播放。
SystemSoundID soundFileObject;
CFURLRef soundFileURLRef = CFBundleCopyResourceURL (CFBundleGetMainBundle (),CFSTR ("sound"),CFSTR ("mp3"),NULL );
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject );
AudioServicesPlaySystemSound (soundFileObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.