【问题标题】:How to play a sound in objective C iphone coding如何在Objective C iphone编码中播放声音
【发布时间】:2010-11-18 14:36:40
【问题描述】:

我正在开发一个应用程序,它使用一些硬件传感器在屏幕上提供数据,有一个随数字更新的标签。我想在数字超过 100 时播放声音。例如,假设它正在读取数字,然后它突然找到了一个好位置(或其他什么),然后我想要播放声音或点亮灯光。我是一个绝对的初学者,如果答案对一个绝对的初学者来说很容易理解,那就太好了。

【问题讨论】:

    标签: iphone objective-c audio


    【解决方案1】:

    我正在使用系统 AudioToolbox.framework 在我的简单游戏中播放声音。我将此静态函数添加到通用 MyGame 类中:

    + (SystemSoundID) createSoundID: (NSString*)name
    {
      NSString *path = [NSString stringWithFormat: @"%@/%@",
                         [[NSBundle mainBundle] resourcePath], name];
    
    
      NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
      SystemSoundID soundID;
      AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
      return soundID;
    } 
    

    我将“Morse.aiff”文件添加到项目资源中,并在(任何)类初始化中使用以下内容对其进行了初始化:

    self.mySound = [MyGame createSoundID: @"Morse.aiff"];
    

    然后我用这个电话播放了声音:

    AudioServicesPlaySystemSound(mySound);
    

    另外,别忘了导入 AudioServices.h 文件。

    这个音频工具箱也可以播放不同的声音格式。

    【讨论】:

    • 请记住,您需要调用 AudioServicesDisposeSystemSoundID 以避免内存泄漏! (因为 SystemSoundID 来自较低级别的框架)
    【解决方案2】:

    h

    #import <AVFoundation/AVFoundation.h>
    
    @interface CMAVSound : NSObject {
        AVAudioPlayer *audioPlayer;
    }
    
    - (id)initWithPath:(NSString*)fileNameWithExctension;
    - (void)play;
    
    @end
    

    #import "CMAVSound.h"
    
    @implementation CMAVSound
    
    -(void)dealloc {
        [audioPlayer release];
    }
    
    - (id)initWithPath:(NSString*)fileNameWithExctension {
        if ((self = [super init])) {
            NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], fileNameWithExctension]];
    
            NSError *error;
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
            if (audioPlayer == nil) {
                NSLog(@"%@", [error description]);
            }
        }
        return self;
    }
    
    - (void)play {
        [audioPlayer play];
    }
    
    @end
    

    【讨论】:

      【解决方案3】:

      查看AVAudioPlayer 类的文档。它允许您播放声音剪辑。如果您在实现时遇到问题,请向我们展示一些代码。

      【讨论】:

      • 好的,谢谢,我会看看,接下来会展示,我知道这很愚蠢,但你具体想要什么?
      【解决方案4】:

      如果声音长达 5 秒并且不需要立体声输出,我建议您使用系统声音来执行此操作。这是比任何其他解决方案都简单且更好的解决方案。 Apple 示例代码以名称 SysSound 提供

      编辑1 或者也许教程可以帮助你更多 http://howtomakeiphoneapps.com/2009/08/how-to-play-a-short-sound-in-iphone-code/

      【讨论】:

      • 这很棒,def。有帮助。知道当它超过某个数字时如何使它工作吗?
      • 没有看到任何代码很难说清楚,但是让我们猜猜你的数字是整数,你的代码中应该有 if 语句来比较值(将它放在你分配值的地方)然后在 if 语句下放置你的系统声音,它会播放
      • 这解释了不同的播放器以及何时使用它们:developer.apple.com/library/ios/#documentation/AudioVideo/…
      【解决方案5】:
      【解决方案6】:

      在这里你可以找到 AudioServicesPlaySystemSound 的列表

      http://iphonedevwiki.net/index.php/AudioServices

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        • 2015-03-01
        相关资源
        最近更新 更多