0

2 つの ViewController があり、それぞれにオーディオを再生および停止するためのボタンがいくつかあります。このオーディオは、2 つの ViewController からアクセスできるように別のモデルにデプロイされていますが、それが正しい方法かどうかはわかりません...

#import <UIKit/UIKit.h> 
#import "AudioController.h"

@interface NowViewController : UIViewController

@property (nonatomic, strong) AudioController *AudioModel;

- (IBAction)PauseButton:(UIButton *)sender;
- (IBAction)PlayButton:(UIButton *)sender;

@end

#import "NowViewController.h"

@interface NowViewController ()

@end

@implementation NowViewController

- (void)viewDidLoad{

    [super viewDidLoad];
    self.AudioModel = [[AudioController alloc] init];

}

- (IBAction)PlayButton:(UIButton *)sender {

    [self.AudioModel PlayAudio];

}

- (IBAction)PauseButton:(UIButton *)sender {

    [self.AudioModel PauseAudio];
}

@end

#import <UIKit/UIKit.h>

#import "AudioController.h"

@interface CategoriesViewController : UIViewController

@property (nonatomic, strong) AudioController *AudioModel;

- (IBAction)PauseButton:(UIButton *)sender;
- (IBAction)PlayButton:(UIButton *)sender;

@end

#import "NowViewController.h"

@interface NowViewController ()

@end

@implementation NowViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.AudioModel = [[AudioController alloc] init];
}

- (IBAction)PlayButton:(UIButton *)sender {
    [self.AudioModel PlayAudio];
}

- (IBAction)PauseButton:(UIButton *)sender {
    [self.AudioModel PauseAudio];
}

@end

私が抱えている問題は、あるページからオーディオを再生して別のページに移動すると、オーディオを停止できないことです。同様に、マルチタスクからの再生・一時停止ができません。AudioModel のコードは次のとおりです。

#import <Foundation/Foundation.h>   
#import <AVFoundation/AVFoundation.h>

@interface AudioController : NSObject{

    AVPlayer *Player;
}

- (void)PlayAudio;
- (void)PauseAudio;

@end

#import "AudioController.h"

@implementation AudioController

- (void)PlayAudio {
    Player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:url]];
    [Player play];
}

- (void)PauseAudio {

    [Player pause];
}

NSString *url = @"http://jo.my/xxx";

@end

助けてくれてありがとう!

4

1 に答える 1

0

各コントローラーAudioControllerのメソッドで、クラスの 2 つの異なるインスタンスを作成しています。viewDidLoad

問題はいくつかの方法で解決できます。たとえば、AudioControllerクラスをシングルトンに変換します。

于 2013-08-03T22:41:16.017 に答える