AudioToolBox无法在某些设备上播放

时间:2022-11-28 19:44:49

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

我有两个使用audiotoolbox框架播放短mp3文件的应用程序。部署目标是:4.0并且Base SDK是: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.

我有iOS 5.0的iphone 4和iOS 5.0的iPhone 3GS。他们俩都在播放声音。我的一个朋友拥有带iOS 4.3的iPad,无法播放任何声音。而另一位使用iOS 4的iPhone 4的朋友也无法播​​放声音。它看起来像iOS的东西,但我真的不知道它不玩的真正原因是什么。

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

以下是我播放文件的代码示例:

H. file (only relevant code):

H.文件(仅相关代码):

#import <AudioToolbox/AudioToolbox.h> 

SystemSoundID soundID1;

M. file (only relevant code):

M.文件(仅相关代码):

@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);}

1 个解决方案

#1


1  

Why don't you use AVAudioPlayer toplay a sound file.

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

Here is the Code.

这是守则。

#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];
    }

}

since iOS 5 using ARC, it doesn't need to release. But, if you use older iOS, then create a release on dealloc method.

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

#1


1  

Why don't you use AVAudioPlayer toplay a sound file.

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

Here is the Code.

这是守则。

#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];
    }

}

since iOS 5 using ARC, it doesn't need to release. But, if you use older iOS, then create a release on dealloc method.

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