ios - 如何使用按钮根据页码播放不同的声音?

标签 ios objective-c avaudioplayer

这里是编程新手。使用本指南设置我的页面:http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

我用来播放音频的代码是:

NSString *path    = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
theAudio          = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];

我的目标是在按下按钮时播放不同的声音文件,每个声音对应于页码。该按钮在整个页面中都是不变的,只是试图更改将其连接到每个页面的新声音的代码。

最佳答案

您需要跟踪当前页面。委托(delegate)方法 pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: 将告诉您用户是否更改了页面,因此您可以更新当前页码变量。它看起来像某种东西,

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{

    if(completed)
    {

        //update variable and play the audio file respectively
    }
}  

关于ios - 如何使用按钮根据页码播放不同的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27812657/

相关文章:

ios - 如何从 appdelegate 中的 dispatch main async 返回 didFinishLaunchingWithOptions 中的 Bool?

未连接到计算机时的 iPhone 内存泄漏监控

objective-c - 对 nil 对象进行快速枚举

c# - 通过 MonoTouch 和 C# 将 AddTarget 用于 UIButton

iphone - 在 iOS 中实现背景音频并排除故障

ios - Swift 3 iOS - 与 AVAudioPlayer 同步暂停

ios - 点击手势识别器妨碍选择 Collection View 单元格

objective-c - 如何更改导航栏颜色

swift - 从 AVAudioPlayer 设置 MPNowPlayingInfoCenter currentTime 和 SongLength

ios - 如何知道哪个 View 被推送到某个 View