0

I have two applications that use the audiotoolbox framework to play short mp3 files. The deployment target is: 4.0 And the Base SDK is: iOS 5.0 SDK

I have an iphone 4 with iOS 5.0 and an iPhone 3GS with iOS 5.0. Both of them are playing the sounds. A friend of mine that have iPad with iOS 4.3, can't play any of the sounds. And another friend with iPhone 4 with iOS 4.2 can't play the sounds as well. It looks like its something with the iOS but I don't really have any clue what is the real reason for it not to play.

Here is an example of my code for playing a file:

H. file (only relevant code):

#import <AudioToolbox/AudioToolbox.h> 

SystemSoundID soundID1;

M. file (only relevant code):

@synthesize soundID1

- (IBAction)one:(id)sender
{    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID1);
    AudioServicesPlaySystemSound (soundID1);
}

- (void)dealloc { AudioServicesDisposeSystemSoundID (self.soundID1);}
4

1 回答 1

1

为什么不使用 AVAudioPlayer 播放声音文件。

这是代码。

#import <AVFoundation/AVAudioPlayer.h>

- (void)viewDidLoad
{

    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
    1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path1] error:NULL];
    1.delegate = self;
    [1 setVolume:1.0];
    [1 prepareToPlay];

}

- (IBAction)one:(id)sender
{

    if (1 && 1.playing) {
        [1 stop];
        [1 setCurrentTime:0];
        [1 play];
    }else {
        [1 play];
    }

}

由于 iOS 5 使用 ARC,它不需要发布。但是,如果您使用较旧的 iOS,则在 dealloc 方法上创建一个版本。

于 2011-12-05T14:18:35.517 回答