开发者

Play audio files in iPhone/iPad application?

I want to play sound in my application for that I am using .mp3 file type. 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?

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
                    )开发者_如何学C;

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. (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. 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.)

Finally, you'll need to add the AVFoundation library to your project. To do this, alt click on your project's target in the Groups & Files column in Xcode, and select "Get Info". Then select the General tab, click the + in the bottom "Linked Libraries" pane and select "AVFoundation.framework".


I honestly would suggest using AVAudioPlayer instead of this, but you could try using this alternate code instead. 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜