使用AVAudioPlayer iOS 7播放音频 [英] Playing back audio using AVAudioPlayer iOS 7
本文介绍了使用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播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文