Tag: avaudioplayer

error handlingAVAudioPlayer的contentsOfURL:错误:在Swift 2中

我跟着一个教程重新: 如何在Swift中创build一个MP3播放器 ,我遇到了一个语法在Swift 1.2和Swift 2.0之间变化的地方。 我遇到了以下方法的error handling问题: player = AVAudioPlayer(contentsOfURL: url, error: &error) 我知道我需要使用try和catch来“Swift2-ify”它。 我已经做了一个“苹果橙子”翻译的Swift 1.2代码,但是我很难把它做成“苹果苹果”。 以下是Swift 1.2中教程的相关方法/声明。 var player: AVAudioPlayer? func queueTrack(){ if (player != nil) { player = nil } var error:NSError? let url = NSURL.fileURLWithPath(tracks[currentTrackIndex] as String) player = AVAudioPlayer(contentsOfURL: url, error: &error) if let hasError = error { //TODO: SHOW ALERT […]

AVAudioPlayer始终是nill,它的isPlaying属性总是假的。 文件在播放时混合

为了解决这个问题,我已经把头发拉了三天。 我已经检查了很多示例代码,阅读了大量的教程,并且在stackoverflow上search了很多很多的问题和答案,但是我仍然无法解决这个问题。 有这样或那样的几个类似的问题,但他们也没有任何解决scheme。 所以关于我的项目一点点: 我有一个NIKMasterViewController和一个NIKDetailViewController 。 在第一个我有一个表视图中的audio文件列表; select一行,导航到NIKDetailViewController ,用户可以看到关于该文件的一些信息并播放audio文件。 我已经在NIKMasterViewController定义了一个AVAudioPlayer属性,并将其设置为: NIKMasterViewController.h : @property (nonatomic, strong) AVAudioPlayer *audioPlayer; NIKMasterViewController.m : @synthesize audioPlayer; – (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; NIKDetailViewController *detailViewController = (NIKDetailViewController *) segue.destinationViewController; [detailViewController setAudioPlayer:audioPlayer]; [detailViewController setFeedEntry:[[[self feedParser] feedItems] objectAtIndex:indexPath.row]]; } else { NSLog(@"Segue Identifier: %@", […]

如何通过NSData播放audio?

我转换了这个path(文件://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav),这是一个audio文件即录制的。 我将此path转换为NSData。 NSData是:例如: <00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 64617461 1cf50200 32003200 e2ffe2ff 3cff3cff 08fe08fe 44fe44fe 04fe04fe e6fde6fd 95fd95fd 96fe96fe b9feb9fe …………………………………………………………………………………………………………f7fef7fe 96ff96ff bdffbdff d6ffd6ff 92009200 23012301 b200b200 79007900 5c015c01 fe01fe01 f101f101 fc01fc01 7b027b02 36023602 > 我想玩这个NSData, 我喜欢下面 NSString *urlString = [[NSString […]

在iOS中同时录制和播放audio

我正试图在录制的同时播放录制的内容。 目前我正在使用AVAudioRecorder进行录制, AVAudioPlayer进行播放。 当我试图同时播放内容时,没有任何东西在播放。 请find我正在做的伪代码。 如果我停止录音后一切正常,一切正常。 AVAudioRecorder *recorder; //Initializing the recorder properly. [recorder record]; NSError *error=nil; NSUrl recordingPathUrl; //Contains the recording path. AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordingPathUrl error:&error]; [audioPlayer prepareToPlay]; [audioPlayer play]; 你能请任何人让我知道你的想法或想法?

如何收到远程通知时播放声音?

我试图在接收到远程通知时自动播放声音文件(这不是我的应用程序包的一部分,不是通知声音)。 当收到通知时,无论应用程序处于前台还是后台,我都希望发生这种情况。 我使用惊人的audio引擎作为核心audio库的包装。 在我的App Delegate的AEAudioFilePlayer ,我创build了一个audio控制器并将AEAudioFilePlayer添加到它,如下所示: NSURL *file = [NSURL fileURLWithPath:sourceFilePath]; AEAudioFilePlayer *notificationPlayer = [AEAudioFilePlayer audioFilePlayerWithURL:file audioController:_notificationController error:NULL]; notificationPlayer.completionBlock = ^{ // Remove self from channel list after playback is complete [_notificationController removeChannels:[NSArray arrayWithObjects:notificationPlayer,nil]] ; }; [_notificationController addChannels:[NSArray arrayWithObjects:notificationPlayer,nil]]; 但是,声音不起作用! 我的日志显示以下错误: 2014-08-18 06:21:28.003 MyApp[394:60b] TAAE: Setting audio session category to AVAudioSessionCategoryPlayback 2014-08-18 06:21:28.019 MyApp[394:60b] Couldn't […]

IOS:在后台播放声音

我正在一个小应用程序,我想要手机播放来自我的位置控制器的事件的声音文件。 问题是,当应用程序处于后台模式时,AVAudioPlayer不启动audio文件,但代码已经调度,获得NSLog输出。 另一件事是它在模拟器,但不是在手机上。 不是吗? 这是我的代码来播放文件: – (void)tryPlayTaleSound:(NSString*)fileName { NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"]; NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath]; NSError *error; backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error]; // Check to see if iPod music is already playing //UInt32 propertySize = sizeof(otherMusicIsPlaying); //AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &otherMusicIsPlaying); // Play the music if no other music is playing […]

如何在Swift中从AVAudioPlayer获取locking屏幕/控制中心的audio控制

新的iOS开发,所以这里。 我有一个应用程序正在播放audio – 我正在使用AVAudioPlayer按名称加载应用程序的资产中的单个文件。 我不想查询用户的库,只有提供的文件。 很好,但是,我希望用户能够从locking屏幕暂停和调整音量。 func initAudioPlayer(file:String, type:String){ let path = NSBundle.mainBundle().pathForResource(file, ofType: type)! let url = NSURL(fileURLWithPath: path) let audioShouldPlay = audioPlaying() do{ try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) let audioPlayer:AVAudioPlayer = try AVAudioPlayer(contentsOfURL: url) audioPlayer.volume = slider.value audioPlayer.numberOfLoops = -1 audioPlayer.prepareToPlay() if(audioShouldPlay){ audioPlayer.play() // let mpic = MPNowPlayingInfoCenter.defaultCenter() // mpic.nowPlayingInfo = [MPMediaItemPropertyTitle:"title", MPMediaItemPropertyArtist:"artist"] } […]

如何在iOS中使用AirPlay进行屏幕镜像(不是来自控制面板)?

我正在使用MPVolumeView来显示AirPlaybutton。 我需要通过audio镜像来向Apple TV显示自定义壁纸。 audio在Apple TV上正常TV但壁纸图像未显示。 我正在使用AVAudioController播放audio。 我已经从video播放应用程序中检查了屏幕镜像正在工作的YouTube应用程序。 是否有可能使用AVAudioController在应用程序中筛选镜像? 帮我解决这个问题。 提前致谢。

如何在iOS上使用AVAudioPlayer播放指定持续时间的声音?

我想在IOS的声音文件中播放指定的时间。 我在AVAudioPlayerfind一个方法,寻求播放的开始(playAtTime :),但我找不到一个直接的方式来指定声音文件结束之前的结束时间。 有没有办法做到这一点?

iOS Swift中简单的低延迟audio播放

我是iOS的初学者,我试图用Swiftdevise一个鼓组应用程序。 我用一个buttondevise了一个视图,并编写了下面的代码,但它有一些问题: 当我像滚鼓一样快速地触摸button时,有些声音会丢失。 仍然在“滚筒滚动”中,每次触摸button都会中断声音,而不是让样本播放直到结束。 例如,在cy片中这很糟糕。 即使再次触摸button,我也想听完所有的声音。 触摸和声音之间存在延迟。 我知道AVAudioPlayer并不是低延迟audio的最佳select,但是作为初学者,很难在没有代码示例或Swift教程的情况下学习OpenAL , AudioUnit 。 问题与此类似: 我应该使用哪个框架在iOS中播放低延迟的audio文件(WAV,MP3,AIFF)? 。 代码: override func viewDidLoad() { super.viewDidLoad() // Enable multiple touch for the button for v in view.subviews { if v.isKindOfClass(UIButton) { v.multipleTouchEnabled = true } } // Init audio audioURL = NSBundle.mainBundle().URLForResource("snareDrum", withExtension: "wav")! do { player = try AVAudioPlayer(contentsOfURL: audioURL) […]