繁体   English   中英

目标C本地化的音频文件不起作用

[英]Objective C Localized Audio Files not working

我一直在努力学习目标c,并且让我的应用或多或少地完成了我想要它做的所有事情。 按下某些按钮时,我可以正确播放音频。

本地化音频文件时遇到了一些困难。

我已经对应用程序的其他元素进行了本地化,没有问题,例如,通过相同的本地化方式,我使用了不同的图像,按钮背景和文本,但是当我对音频进行本地化时,我遇到了一些问题,并且找不到在线解决方案我通常会的

因此,正如我所说的,该问题仅在文件本地化时发生,它会影响原始语言(在文件本地化之前的工作位置)和其他语言(在这种情况下为法语)

所以我在按钮上使用的代码是:

- (IBAction)domesticButtonClicked:(id)sender
{

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

    resourcePath = [resourcePath stringByAppendingString:@"/localize-test.wav"];

    // NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    domesticSound = [[AVAudioPlayer alloc] initWithContentsOfURL:
                     [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        domesticSound.delegate = self;
        [domesticSound play];
        domesticSound.numberOfLoops = 0;
        domesticSound.currentTime = 0;
        domesticSound.volume = 1.0;

    }

}


}

因此,当它运行时,与其播放声音,不如在我的输出中看到以下内容:

失败原因:操作无法完成。 (OSStatus错误2003334207。)

谁能帮我这个忙?

提前致谢。

编辑:

感谢Marcus Adams,您的建议确实有效,但是我无法在某些按钮上使用它,因为我无法每次都声明它。

因此,以这种方式声明音频文件的原因是可以根据单击按钮的次数来更改音频文件。 也许这将有助于解决我的问题。

- (IBAction)domestic03ButtonClicked:(id)sender
{

    static int imageNumber = 0;

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];


    if (imageNumber == 0) {
        domestic03ImageContainer.image = domestic300;
        resourcePath = [resourcePath stringByAppendingString:@"/smb_coin1.wav"];
        imageNumber++;
    }

    else if (imageNumber == 1) {
        domestic03ImageContainer.image = domestic301;
        resourcePath = [resourcePath stringByAppendingString:@"/smb_coin2.wav"];
        imageNumber++;
    }

    else if (imageNumber == 2) {
        domestic03ImageContainer.image = domestic302;
        resourcePath = [resourcePath stringByAppendingString:@"/smb_coin3.wav"];
        imageNumber++;
    }

    else if (imageNumber == 3) {
        domestic03ImageContainer.image = domestic303;
        resourcePath = [resourcePath stringByAppendingString:@"/smb_coin4.wav"];
        imageNumber++;
    }

    else if (imageNumber == 4) {
        domestic03ImageContainer.image = domestic304;
        resourcePath = [resourcePath stringByAppendingString:@"/smb_coin5.wav"];
        imageNumber = 0;
    }


    // NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    domesticSound = [[AVAudioPlayer alloc] initWithContentsOfURL:
                     [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        domesticSound.delegate = self;
        [domesticSound play];
        domesticSound.numberOfLoops = 0;
        domesticSound.currentTime = 0;
        domesticSound.volume = 1.0;

    }

    domestic03ImageContainer.Alpha = 1;

}

如果domesticSound == nil,则只应检查err 对于带有NSError ** in / out参数的API,通常是这样。 (不保证在成功时会写入错误 。)

您还应该将err初始化为nil!

假设您已将资源直接放置在正确的本地化文件夹中,那么您应该可以仅将此路径用作路径:

NSString* resourcePath = [[NSBundle mainBundle] pathForResource:@"localize-test"
                                                         ofType:@"wav"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM