gpt4 book ai didi

iphone - 使用 Storyboard并更改 View 时,声音不会停止在新 View 中播放

转载 作者:行者123 更新时间:2023-12-01 18:00:46 26 4
gpt4 key购买 nike

我正在尝试做类似于这个问题中要求的事情:
Playing audio with controls in iOS

我正在使用带有 XCode 4.2 的 Storyboard 。在我的初始 View 页面上,我只有调用其他 View 的按钮,并且没有播放任何声音。这些其他 View 包含一个带有 BarButtonItem 的 UINavigationController 以允许您返回。其他 View 页面将具有允许播放声音的按钮。每个声音都有自己的播放按钮,并且有恢复、暂停和停止按钮。

所有声音都可以正常播放,但如果我按返回按钮返回主页,声音就会继续播放。所需的行为是当通过后退按钮导航回主页时声音停止。

在 Storyboard之前,我可以轻松地编写一个后退按钮来停止音频,但 Storyboard看起来并不那么简单。看来我必须实现方法 prepareForSegue。好的,我可以设置 Segue 标识符,可以在 Attributes Inspector 中设置,引用之前的帖子,我将使用 showPlayer。

但我想我可以在我的声音 View 中编写 prepareForSegue ,如这个很酷的示例所示:
http://www.scott-sherwood.com/?p=219

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if([[segue identifier] isEqualToString:@"showPlayer"]){
SoundPageController *theAudio = (SoundPageController *)[segue ViewController];
theAudio.delegate = self;
}
}

在主页面 ViewController.m 中,我尝试添加:
  @property (strong, nonatomic) AVAudioPlayer *theAudio;

我尝试添加如下内容:
  - (void)viewDidLoad {
[super viewDidLoad];

// this is the important part: if we already have something playing, stop it!
if (audioPlayer != nil)
{
[audioPlayer stop];
}
// everything else should happen as normal.
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

我无法使其正常工作,因此我将更改恢复为原始状态。我将把我的简化代码放在下面,如果您有正确的建议,请告诉我!

View Controller .h:
@interface ViewController : UIViewController {      
}

ViewController.m(几乎是默认的):
    #import "ViewController.h"

#import "AppDelegate.h"

@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{

[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

}

SoundPage1.h:
#进口
#import <AVFoundation/AVAudioPlayer.h>

@interface occupy : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer* theAudio;
}

//-(IBAction)PressBack:(id)sender; now the back button is linked up through storyboard
-(IBAction)pushButton1;
-(IBAction)pushButton2;
-(IBAction)play;
-(IBAction)stop;
-(IBAction)pause;
@end

SoundPage1.m
#import "SoundPage1.h"
#import "AppDelegate.h"
#import "ViewController.h"

@implementation SoundPage1

-(IBAction)pushButton {

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}

-(IBAction)pushButton2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}

// Old code from XCode 3.x when creating a back button that could stop the audio was easy
//-(IBAction)PressBack:(id)sender{
// [theAudio stop];
// ViewController* myappname = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
// [self.navigationController pushViewController:myappname animated:NO];
// }


- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

为按钮添加播放、停止和暂停的代码。

我尝试对此进行格式化,如果难以阅读,请提前抱歉。

感谢您的任何建议!

最佳答案

您应该在 viewWillDisappear 中停止 AVAudioPlayer。

- (void)viewWillDisappear:(BOOL)animated { 
[super viewWillDisappear:animated];
if(theAudio && theAudio.isPlaying) {
[theAudio stop];
}
}

关于iphone - 使用 Storyboard并更改 View 时,声音不会停止在新 View 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10360566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com