0

对 Objective-C 和 Xcode 编程来说非常新,我不太明白为什么我的声音没有播放。我遵循了本教程(http://www.youtube.com/watch?v=2mbCkIVF1-0),但由于某种原因,我无法在 iOS 模拟器中播放我的声音。我使用的是故事板布局而不是 nib 文件,所以这可能是问题所在?我知道如何将我的按钮连接到我拥有的故事板,所以我很困惑,这个问题让我发疯。非常感谢任何帮助,如果我的代码错误,请指出正确的方向,问题可能是微不足道的,无论哪种方式,任何帮助都会很棒!再次感谢

SFViewController.m

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>


@interface SFViewController : UIViewController <AVAudioPlayerDelegate> {

}
-(IBAction)playSound1;
-(IBAction)playSound2;
-(IBAction)playSound3;

@end

SFViewController.h

#import "SFViewController.h"
#import <AVFoundation/AVAudioPlayer.h>


@implementation SFViewController



-(IBAction)playSound1 {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound1" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];

    theAudio.delegate = self;
    [theAudio play];
}

-(IBAction)playSound2 {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound2" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];

    theAudio.delegate = self;
    [theAudio play];
}

-(IBAction)playSound3 {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound3" ofType:@"wav"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];

    theAudio.delegate = self;
    [theAudio play];
}
4

4 回答 4

9

当超出范围时,您的“theAudio”变量会丢失,因此播放器会停止。将变量保留为类的成员:

@implementation SFViewController
{
    AVAudioPlayer* theAudio;
}

-(IBAction)playSound1 {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound1" ofType:@"wav"];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];

    theAudio.delegate = self;
    [theAudio play];
}
于 2014-04-17T22:42:35.503 回答
0

你为什么要传递NULL给你的AVAudioPlayerinit?传入一个指向 an 的 nil 指针,NSError并确保初始化后错误仍然为 nil。我最好的猜测是有一些错误,theAudio实际上是零。

于 2013-08-31T16:54:41.310 回答
0

首先确保您的 IBAction 方法实际上被调用(在这些方法中放置一个 NSLog 语句,看看它们是否被调用..如果不是..我相信你可以自己弄清楚)

第二:创建一个 NSError 对象并传入它的地址以查看问题的性质,如下所示:

 NSError *err= nil
 NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound1" 
                                                  ofType:@"wav"];
 AVAudioPlayer* theAudio = 
      [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
                                             error:err];
 if (!theAudio) {
     NSLog(@"failed playing SeriouslyFunnySound1, error: %@", error);
 }

 theAudio.delegate = self;
 [theAudio play];

err 的内容应该告诉你下一步该做什么

于 2013-08-31T16:56:52.757 回答
0

遇到同样的问题。这是 Swift 中的答案:

class ViewController: UIViewController {

    var soundPlayer: AVAudioPlayer!

    @IBAction func newPomodoButtonTaped(sender: UIButton) {
       let soundPath = NSBundle.mainBundle().pathForResource("click_done", ofType: "wav")!
      do {
            soundPlayer = try AVAudioPlayer(contentsOfURL: NSURL(string: soundPath)!)
            soundPlayer.prepareToPlay()
            soundPlayer.numberOfLoops = 0
            soundPlayer.play()
        } catch let error as NSError {
            print(error)
        }
    }
}
于 2016-08-14T16:01:32.653 回答