【问题标题】:AVAudioPlayer not playing my mp3AVAudioPlayer 不播放我的 mp3
【发布时间】:2012-02-02 00:23:39
【问题描述】:

我正在尝试在我的应用中播放一个简短的音频文件 (mp3)。这是我正在使用的代码:

   AVAudioPlayer *AudioPlayer;
    NSError *error;


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"filename"
                                         ofType:@"mp3"]];

    AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

    AudioPlayer.delegate = self;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

    if (error)
    {
        NSLog(@"Error: %@",
              [error localizedDescription]);
    }

    else
    {
        [AudioPlayer play];

    }

我真的不知道我错过了什么,我所遵循的示例似乎与我正在做的事情相匹配。

编辑:我还应该提到代码运行没有错误,这有一个try catch。

【问题讨论】:

    标签: ios audio avaudioplayer


    【解决方案1】:

    由于 ARC,我遇到了类似的问题。与其在使用它的方法中定义 AVAudioPlayer,不如在其他地方有一个实例变量,例如 UIViewController。这样 ARC 就不会自动释放这个对象并且可以播放音频。

    【讨论】:

    • 非常感谢这个提示,伙计。你拯救了这一天。和平:)
    【解决方案2】:

    我做了一些补充,并得到了它的工作:

    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>
    
    @interface FirstViewController : UIViewController <AVAudioPlayerDelegate> {
    AVAudioPlayer *data;
    UIButton *playButton_;
    }
    
    @property(nonatomic, retain) IBOutlet UIButton *playButton;
    
    -(IBAction)musicPlayButtonClicked:(id)sender;
    
    @end
    
    #import "ViewController.h"
    
    @implementation FirstViewController
    
    @synthesize playButton=playButton_;
    
    - (IBAction)musicPlayButtonClicked:(id)sender {
    
    NSString *name = [[NSString alloc] initWithFormat:@"09 No Money"];
    NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
    if (data) {
        [data stop];
        data = nil;
    }
    data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
    data.delegate = self;
    [data play];
    }
    
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    @end
    

    在 Interface Builder 中,我添加了一个按钮并将其与 IBOutletmusicPlayButtonClicked IBAction 连接。

    别忘了将AVFoundation 框架添加到您的项目中。

    ps 我真的很抱歉我的英语,请放纵一下。

    【讨论】:

      【解决方案3】:

      我自己也遇到过这个问题,直到我发现声音没有通过扬声器发出(相反,它被重定向到通话扬声器)。尝试添加这个以通过扬声器重定向声音:

      UInt32 doChangeDefaultRoute = 1;
              AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                                       sizeof (doChangeDefaultRoute),
                                       &doChangeDefaultRoute);
      

      【讨论】:

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