gpt4 book ai didi

ios - 使用 AVAudioPlayer iOS 7 播放音频

转载 作者:行者123 更新时间:2023-11-28 20:04:23 25 4
gpt4 key购买 nike

我正在努力遵循 Apple 关于使用 AVAudioPlayer 类播放小型 .wav 文件的文档。我也不确定基本音频播放需要哪些工具箱。到目前为止,我已经导入了:

AVFoundation.framework
CoreAudio.framework
AudioToolbox.framework

这是我的代码 .h.m:

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

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

- (IBAction)playAudio:(id)sender;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)playAudio:(id)sender {

NSLog(@"Button was Pressed");

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"XF_Loop_028" ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

// create new audio player
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[myPlayer play];

}
@end

我似乎没有任何错误。但是,我没有收到任何音频。

最佳答案

您遇到了 ARC 问题。 myPlayer 超出范围时正在清理。创建一个强大的属性,分配 AVAudioPlayer,你可能已经准备就绪!

@property(nonatomic, strong) AVAudioPlayer *myPlayer;

...

// create new audio player
self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
[self.myPlayer play];

关于ios - 使用 AVAudioPlayer iOS 7 播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22569699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com