0

我有一个音频播放器ViewControllerViewController当在另一首歌中选择DidSelectRow一首歌曲时,我称之为。我面临的问题是当我选择一首歌曲时,它会推送到音频播放器视图控制器并开始播放,但是当我按下并从专辑中选择另一首歌曲时,上一首歌曲并没有停止,它仍然在选择新歌曲时播放。现在新选择的歌曲推送到另一个音频播放器实例并播放。而先前选择的歌曲也随之播放。并且对于每个选择的歌曲,都会推送一个新的音频播放器实例,并且所有选择的歌曲都会一起播放。我的专辑控制器具有它的选择行代码如下tableviewviewcontrollerAlbumViewControllerviewcontrollerviewcontrollertableview

 viewContObj=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];  //viewcontroller=audio player controller
   [self.navigationController pushViewController:viewContObj animated:YES];
   viewContObj.url =[NSURL URLWithString:albumURLstring];
  [viewContObj playLiveStream:nil];

音频播放器的播放方法如下。

   -(IBAction)playLiveStream:(id)sender

{ if ( ![streamer isPlaying] )

{   [self createStreamer];
    [self setButtonImageNamed:@"loadingbutton.png"];
    [streamer start];
}
else
{   [self setButtonImageNamed:@"play.png"];
    [streamer pause];
}}
4

3 回答 3

1

对 ViewController 使用单例实例并尝试这样

在 ViewController 中创建单例实例

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
        {
            self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
            if (self) {
                // Custom initialization
            }
            return self;
        }

//1. Customize singletone 

        -(id)initWithCustomDetails:(NSString *)customDetails
        {
            self = [super init];
            if(_sharedMySingleton)
            {
                [_sharedMySingleton.view removeFromSuperview];
                _sharedMySingleton = nil;
            }
            _sharedMySingleton = self;

            return self;
        }

2. exact singletone method for creating instance

 +(ViewController *) getViewControllerInstance
{
    @synchronized([ViewController class])
    {
        if (!_sharedMySingleton)
        {
            _sharedMySingleton = [[self alloc] init];

        }
        return _sharedMySingleton;
    }
    return nil;
}

创建实例时使用

  1. viewContObj=[[ViewController alloc]initWithCustomDetails:@"details"];

而不是这种方法

viewContObj=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

//精确的单例实例创建

  1. viewContObj = [ViewController getViewControllerInstance];

并尝试..

于 2013-03-08T11:55:13.927 回答
0

推送后添加释放

viewContObj=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];  
[self.navigationController pushViewController:viewContObj animated:YES];
viewContObj.url =[NSURL URLWithString:albumURLstring];
[viewContObj playLiveStream:nil];
[viewContObj release];

在 ViewController.m 中:

- (void)dealloc{
    [streamer stop];
    [streamer release];
    [super dealloc]; 
}

好的?

于 2013-03-08T11:14:28.557 回答
0

像这样在 .m 文件的@interface部分中放置一个 ivar

@property (strong, nonatomic) ViewController *playerView;

viewDidLoad方法 alloc init 中如下

playerView=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; 

并在tableView: didSelectRowAtIndexPath:方法中执行以下操作

playerView.url =[NSURL URLWithString:albumURLstring];
[playerView playLiveStream:nil];
[self.navigationController pushViewController:playerView animated:YES];

愿这能解决你的问题:)

于 2013-03-12T10:11:16.660 回答