使用AVAudioPlayer iOS 7播放音频

编程入门 行业动态 更新时间:2024-10-28 12:31:32
本文介绍了使用AVAudioPlayer iOS 7播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力遵循Apple有关使用 AVAudioPlayer 类播放小型$ code> .wav 文件的文档。我也不确定基本音频播放需要什么工具箱。到目前为止我已导入:

I'm struggling to follow Apple's documentation regarding playing back a small .wav file using the AVAudioPlayer class. I'm also not sure what toolboxes I need for basic audio playback. So far I have imported:

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

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

I don't seem to have any errors. However, I am not getting any audio.

推荐答案

您在这里遇到ARC问题。当myPlayer超出范围时,它正在被清理。创建一个强大的属性,分配AVAudioPlayer,你可能已经全部设置好了!

You're having an ARC issue here. myPlayer is being cleaned up when it's out of scope. Create a strong property, assign the AVAudioPlayer and you're probably all set!

@property(nonatomic, strong) AVAudioPlayer *myPlayer; ... // create new audio player self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil]; [self.myPlayer play];

更多推荐

使用AVAudioPlayer iOS 7播放音频

本文发布于:2023-07-11 11:52:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:音频   AVAudioPlayer   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!