首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone未使用type=@" mp3“播放mp3声音

iPhone未使用type=@" mp3“播放mp3声音
EN

Stack Overflow用户
提问于 2010-01-22 01:20:53
回答 1查看 1.3K关注 0票数 2

我正在写一个iPhone应用程序,当某件事发生时,就会播放声音。我尝试使用不同的WAV,MP3,M4A文件,但有间歇性的问题。下面的代码在模拟器中运行良好,但MP3不能在iPhone上播放。(3GS,如果重要的话)。有什么想法吗?我已将mp3文件更改为wav,它将适用于下面提供的文件(knock_knock),但不适用于其他文件。可能是它的编码方式?

任何帮助都是非常感谢的。谢谢

代码如下:

代码语言:javascript
复制
SystemSoundID soundID;
NSString *soundFile;

if (something == somethingelse) {
    soundFile = [[NSBundle mainBundle] pathForResource:@"knock_knock" ofType:@"wav"];
}
else {
   soundFile = [[NSBundle mainBundle] pathForResource:@"strike" ofType:@"mp3"];
}
   AudioServicesCreateSystemSoundID((CFURLRef)
                                 [NSURL fileURLWithPath:soundFile]
                                 , &soundID);
AudioServicesPlaySystemSound(soundID);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-01-22 02:16:37

系统声音必须解压缩。我遇到了这个问题,并将代码更改为使用AVAudioSession和AVAudioPlayer。在我的头顶上,像这样的东西(需要更好的错误检查)

代码语言:javascript
复制
    -(void)load {
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [self path]];
    if (fileURL) {

        AVAudioPlayer *newPlayer =
        [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                               error: nil];
        [fileURL release];
        [self stop];
        self.player = newPlayer;
        [newPlayer release];

        [self.player prepareToPlay];
    }   
}

    -(void)play {

    if (self.player == nil)
        [self load];

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory: AVAudioSessionCategoryAmbient error:&err];
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }
    [audioSession setActive:YES error:&err];

    self.player.currentTime = 0;
    [self.player play]; 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2111293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档