xcode 使用 AVAudioPlayer 帮助停止音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1899186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help stopping audio using AVAudioPlayer
提问by Dasutin
Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file in .caf format. What I'm trying to do is have a play and pause button. Here is a sample of a simple IBAction to play the audio from a button.
好吧,我有两个问题。我正在使用 AVAudioPlayer 播放 .caf 格式的简单音频文件。我想要做的是有一个播放和暂停按钮。这是一个简单的 IBAction 示例,用于从按钮播放音频。
- (IBAction) clear {
NSString *path = [[NSBundle mainBundle] pathForResource:@"clear" ofType:@"caf"];
AVAudioPlayer* myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
myAudio.delegate = self;
myAudio.volume = 1.0;
myAudio.numberOfLoops = 0;
[myAudio play];
}
So when I go ahead and create another IBAction with the method...
因此,当我继续使用该方法创建另一个 IBAction 时...
- (IBAction) stopaudio {
[myAudio stop];
audioPlayer.currentTime = 0;
}
I compile and xcode tells me that myAudio in the stopaudio IBAction is not defined. How do I fix it so I'm able to access myAudio in another IBAction?
我编译,xcode 告诉我 stopaudio IBAction 中的 myAudio 没有定义。如何修复它以便我能够在另一个 IBAction 中访问 myAudio?
My Second question is that I'm using NavigationController to setup a TableView, when I make a selection it takes me to a UIView with a back button to go back to the TableView. What I'm trying to do is to kill any audio that I have playing in the UIView when I go back to the TableView. The code above is part of the code that I'm going to have in the UIView.
我的第二个问题是,我正在使用 NavigationController 来设置 TableView,当我进行选择时,它会将我带到带有后退按钮的 UIView 以返回到 TableView。我想要做的是当我回到 TableView 时杀死我在 UIView 中播放的任何音频。上面的代码是我将要在 UIView 中使用的代码的一部分。
Also one quick question. Lets say I have an audio file that is 1 minute long. Clicking play again will overlap a new instance of that audio before the old instance is done playing. How can I check to see if myAudio is playing and not allow it to be played again until myAudio is finished playing?
还有一个简单的问题。假设我有一个 1 分钟长的音频文件。在旧实例播放完毕之前,再次单击播放将与该音频的新实例重叠。如何检查 myAudio 是否正在播放并且在 myAudio 完成播放之前不允许再次播放?
Thank You!
谢谢你!
回答by Morion
First of all, you should read the documentation. It will remove almost all of your questions. Nevertheless, to have access to your instance of AVAudioPlayer from all methods of your class, you should define it in your .h file. To stop your player on returning to the previous screen, call [myAudio stop]
in your viewWillDisappear:(BOOL)animated
method. About your last question. If you define myAudio
in your .h file, you will be able to check if it is playing now. smth like
首先,您应该阅读文档。它将消除您几乎所有的问题。然而,要从类的所有方法访问 AVAudioPlayer 实例,您应该在 .h 文件中定义它。要阻止您的播放器返回上一屏幕,请调用[myAudio stop]
您的viewWillDisappear:(BOOL)animated
方法。关于你的最后一个问题。如果您myAudio
在 .h 文件中定义,您将能够检查它现在是否正在播放。喜欢
- (IBAction) clear { if(![myAudio isPlaying]){ [myAudio play]; }
}
- (IBAction) clear { if(![myAudio isPlaying]){ [myAudio play]; }
}