Tag: avaudioplayer

背景音乐播放不工作在iOS模拟器

我看到很多关于在后台播放audio的问题。 这些问题通常会问为什么audio在模拟器的背景下播放,但是设备根本不会。 就我而言,这是颠倒过来的。 当我在iPad上testing我的示例应用程序时,它完美地工作。 但是,当我在模拟器中开始testing时,它不会在后台继续播放。 这里是我使用的代码: NSURL *url = [NSURL URLWithString:firstSong]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [audioPlayer setNumberOfLoops:-1]; [audioPlayer setCurrentTime:0]; [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance]setDelegate:self]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; [audioPlayer play]; 谁能告诉我为什么这不起作用? 这是一个错误还是什么?

在button上播放声音(AVaudio播放器)返回零string参数

我试图按button播放声音。 以前,我使用的Audiotoolbox工作得很好,具有相同的文件types。 但是,我需要改变使用AvAudioPlayer,因为它不使用铃声音量。 所以上周我开发了一个使用audio播放器播放声音的理解。 我使用完全相同的代码在我的应用程序上工作。 不过,尽pipe尝试无数次这个应用程序将无法使用AVFoundation / AudioPlayer。 我试图添加新的文件,新的button,改变文件的扩展名。 文件名在资源文件夹中。 我没有拼错这个名字。 确切的问题是,当我试图find我的文件时,它返回一个零string参数: 终止应用程序,由于未捕获exception“NSInvalidArgumentException”,原因:' * – [NSURL initFileURLWithPath:]:nilstring参数' 我已经看过关于这个每个论坛的post,但没有一个会帮助我。 我只能认为我的项目中的潜在问题可能是资源文件夹,而不是实际的AVAudioPlayer代码,因为我有这个确切的代码在另一个项目上工作相同。 任何帮助将不胜感激,因为我难住。 这是重要的代码: 这是在我的 @interface MainViewController : UIViewController @property (strong, nonatomic) AVAudioPlayer *theAudio; – (IBAction)test:(id)sender; 这是在我的 @synthesize theAudio; – (IBAction)test:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"beast" ofType:@"wav" ]; self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; [self.theAudio play]; } […]

检查video是否有audio

如果video文件(从服务器stream媒体)有audio,我必须做出决定。 如果它有audio,那么我必须停止已经播放audio文件和开始video,否则播放video已经运行的audio。 我在播放video之前设法暂停audio,但是如何在video中检查audio? 我正在使用AVAudioPlayer播放audio和MPMoviePlayerViewController来播放video。 我在谷歌上search了很多,但没有成功。 任何帮助将不胜感激。

用AVAudioPlayer播放多个声音(一个接一个)

我正在使用AVAudioPlayer制作MP3播放器。 我有多个MP3的声音,并希望一一播放这些声音。 以下是我的应用程序的逻辑: ///// For playing 1st sound mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil]; [mp3Player prepareToPlay]; [mp3Player play]; ///// For playing 2nd sound mp3Player = nil; mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil]; [mp3Player prepareToPlay]; [mp3Player play]; ///// For playing 3rd sound mp3Player = nil; mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url3 error:nil]; [mp3Player prepareToPlay]; [mp3Player play]; etc… […]

如何通过OutputStream发送NSData

你可以在github上查看这个项目: https : //github.com/Lkember/MotoIntercom/ 重要的类是PhoneViewController.swift 我有一个AVAudioPCMBuffer。 然后使用这个函数将缓冲区转换为NSData: func audioBufferToNSData(PCMBuffer: AVAudioPCMBuffer) -> NSData { let channelCount = 1 let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: channelCount) let data = NSData(bytes: channels[0], length:Int(PCMBuffer.frameCapacity * PCMBuffer.format.streamDescription.pointee.mBytesPerFrame)) return data } 根据OutputStream.write的文档,需要将这些数据转换为UnsafePointer <UInt8>。 https://developer.apple.com/reference/foundation/outputstream/1410720-write 这是我迄今为止: let data = self.audioBufferToNSData(PCMBuffer: buffer) let output = self.outputStream!.write(UnsafePointer<UInt8>(data.bytes.assumingMemoryBound(to: UInt8.self)), maxLength: data.length) 当收到这个数据时,使用这个方法将它转换回AVAudioPCMBuffer: func dataToPCMBuffer(data: NSData) […]

内存泄漏 – NSString&NSURL

@property ( nonatomic, strong ) NSURL * urlPath; self.urlPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bark" ofType:@"caf"]]; 运行ARC,部署目标4.3。 仪器泄漏self.urlPath =行。 self.urlPath稍后在视图出现设置AVSoundPlayer之后使用。 现在声音上没有任何泄漏,只有在这个NSURL线上。 audio播放,但是当视图popup时,发生内存泄漏。 任何想法,我已经在这个> 12小时了…

如何在使用AVAudioPlayer框架完成播放后再次播放相同的声音?

我希望我的声音文件随后播放三次。 但是,它只播放一次。 我[crashSound play]被调用时,声音还没有完成播放,所以随后的呼叫都会丢失。 我该如何解决这个问题。 即我怎样才能让它完成当前的播放后再次播放相同的文件? @interface Game() { // code… AVAudioPlayer *crashSound; // code… } @end @implementation Game – (id) init { // code… NSBundle *bundle = [NSBundle mainBundle]; NSURL *crashSoundFile = [bundle URLForResource: @"crashSound" withExtension: @"wav"]; crashSound = [[AVAudioPlayer alloc] initWithContentsOfURL:crashSoundFile error:NULL]; // code… } -(void) play // this is my "main" method […]

应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用时不在UIApplicateStateBackground中

我已经困惑了几个星期,仍然不知道发生了什么事情。 该应用程序设置为背景模式播放audio和提取数据。 该应用程序是背景和屏幕上的锁屏。 方法 application:didReceiveRemoteNotification:fetchCompletionHandler: 当远程推送到达时被调用。 奇怪的是,应用程序状态有时是不同的。 以下是不同会议的两个成果: A)远程推送进来,应用程序状态是UIApplicationStateBackground,这是完全按预期。 这允许.plist中configuration的“所需的背景模式”正常工作: 2015-07-18 13:26:38.273 x[22430:5297207] AppDelegate applicationWillResignActive: called 2015-07-18 13:26:39.007 x[22430:5297207] AppDelegate applicationDidEnterBackground: called 2015-07-18 13:29:04.181 x[22430:5297207] application:didReceiveRemoteNotification:fetchCompletionHandler: applicationState == UIApplicationStateBackground B)远程推送进来,但应用程序状态不是UIApplicationStateBackground。 出于某种原因,应用程序在调用该方法之前开始转换到前台,然后在方法内状态不正确地使用UIApplicationStateInactive而不是UIApplicationStateBackground: 2015-07-18 12:37:26.087 x[22430:5297207] AppDelegate applicationWillResignActive: called 2015-07-18 12:37:26.899 x[22430:5297207] AppDelegate applicationDidEnterBackground: called 2015-07-18 12:39:20.898 x[22430:5297207] AppDelegate applicationWillEnterForeground: called 2015-07-18 12:39:21.014 x[22430:5297207] application:didReceiveRemoteNotification:fetchCompletionHandler: applicationState == […]

AVAudioPlayer会影响系统声音

我使用这个代码来播放短暂的声音效果。 用户单击键盘的返回button时会发生这种情况,这会使键盘声音效果比平时更响。 是否可以在不影响系统声音的情况下播放特定音量的效果? – (void)playCorrectAnswerSound { NSString *path = [[NSBundle mainBundle] pathForResource:@"correct" ofType:@"mp3"]; NSURL *urlPath = [NSURL fileURLWithPath:path]; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:nil]; self.player.volume = 0.04; [self.player prepareToPlay]; [self.player play]; }

在iOS中使用AVAudioPlayer之后,请讲话录音机失败

我在我的项目中使用Apple的SpeakHere示例。 我可以录制语音和玩没有问题.. 当我更改视图控制器,我正在使用AVAudioPlayer播放录制的声音时,会发生问题。如果我再次回到SpeakHereViewControllerlogging一次,然后说“错误:测光失败”。 为什么发生? 提前致谢。