【问题标题】:Objective C - IOS - Xcode 4.6.2 - can't hear sound from mp3 filesObjective C - IOS - Xcode 4.6.2 - 听不到 mp3 文件的声音
【发布时间】:2013-09-20 11:32:55
【问题描述】:

我正在学习如何在按下按钮时播放 mp3 声音的教程。

我创建了一个按钮 (playSound)。

我将它添加到视图控制器界面:

- (IBACTION)playSound:(id)sender;

在实现中,我声明了所需的头文件,并编写了以下内容:

#import "AudioToolbox/AudioToolbox.h"
#import "AVFoundation/AVfoundation.h"

- (IBAction)playSound:(id)sender {

    //NSLog(@"this button works");
    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"];
    //NSLog(@"%@", audioPath);
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
    //NSLog(@"%@", audioURL);
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];

}

我没有收到任何错误。 NSlogs 记录的 URL 很好,现在我不知道在哪里进一步查看。我还检查了我的 MP3 声音是否损坏,但事实并非如此。我听到的只是 1 秒钟的噼啪声。然后它就停止了。

【问题讨论】:

    标签: ios objective-c xcode4.6


    【解决方案1】:

    您声明了一个局部变量audioPlayer 来保存指向播放器的指针。一旦您的按钮处理程序返回播放器在有机会播放您的声音文件之前被释放。声明一个属性并使用它来代替局部变量。

    在 YourViewController.m 文件中

    @interface YourViewController ()
    @property (nonatomic, strong) AVAudioPlayer *audioPlayer;
    @end
    

    或在 YourViewController.h 文件中

    @interface YourViewController : UIViewController
    @property (nonatomic, strong) AVAudioPlayer *audioPlayer;
    @end
    

    然后在您的代码中将audioPlayer 替换为self.audioPlayer

    【讨论】:

    • 这修复了它!这是一个很大的问题。非常感谢!并感谢所有试图帮助我的人:-)
    【解决方案2】:

    试试这个对我有用

    在.h中

    AVAudioPlayer * _backgroundMusicPlayer;
    

    在.m

    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"Theme" ofType:@"mp3"];
        NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
        NSError *error;
        _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
        [_backgroundMusicPlayer setDelegate:self];  // We need this so we can restart after interruptions
        [_backgroundMusicPlayer setNumberOfLoops:-1];
        [_backgroundMusicPlayer play];
    

    【讨论】:

      【解决方案3】:

      已编辑

      NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file" 
                                                                    ofType:@"type of your audio file example: mp3"];
          NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
          audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
          audioPlayer.numberOfLoops = -1;
          audioPlayer.delegate=self;
          [audioPlayer play];
      

      试试这个,让我知道。 确保为 audioPlayer 设置代理。

      【讨论】:

      • 这对我也不起作用。您能解释一下添加 audioPlayer.numberOfLoops = -1; 的想法吗?到代码?
      • 是的。我检查了文件是否损坏。它工作得很好。 - URL:文件://localhost/Users/kevinvanhengst/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CF8BC92D-AEEE-4185-96F5-650572A3BFC1/WillemsKantineSoundboard.app/audio.mp3
      • 我检查了该文件是否已添加到“复制捆绑资源”中,并且它也在那里。我有点迷路了..
      • 已经为音频播放器设置了代理?
      【解决方案4】:

      首先在你的项目中重新添加你的音乐文件,然后试试这个代码 通过日志,您可以看到错误。

      NSError *error;
      
      NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bg_sound" ofType:@"mp3"]];
      
      AVAudioPlayer   *audioPlayer = [[AVAudioPlayer alloc]
                                    initWithContentsOfURL:url
                                    error:&error];
      if (error){
      //Print Error
      NSLog(@"Error in audioPlayer: %@",
                [error localizedDescription]);
      } else {
      audioPlayer.delegate = self;
      [audioPlayer prepareToPlay];
      [audioPlayer setNumberOfLoops: -1];
      [audioPlayer play];
      audioPlayer.volume=1.0;
      
      }
      

      确保您的音乐文件已正确添加到项目中

      【讨论】:

      • 我得到的结果与我的代码完全相同。我听到了 1 秒钟的噼啪声,就是这样。有没有可能是我的软件有问题?我的代码对你有用吗?
      • 是的,这段代码对我有用,你在设备上试过了吗?您的音频播放时长是多少,您可以使用其他音频文件测试您的代码。
      • 我正在使用一个相当大的音频文件 atm。大约 6mb。但是用 4kb 的声音文件测试后没有区别。
      • 已经做过几次了。如果这能解决问题,那就太好笑了。尝试在真实设备上部署它,然后看看它会做什么。如果代码在您的机器上运行,而不是在我的机器上运行,那显然是软件问题。谢谢你帮助我! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多