AVAudioPlayer始终是nill,其isPlaying属性始终为false。播放时文件混合在一起

时间:2013-11-23 16:35:39

标签: ios objective-c avaudioplayer

过去三天我一直在拔头发来解决这个问题。我已经检查了大量的示例代码,阅读了大量的教程,并使用Google搜索并检查了很多关于stackoverflow的问题和答案,但我仍然无法解决问题。有几个类似的问题,如thisthis,但他们也没有任何解决方案。

关于我的项目的一点点:

我有NIKMasterViewControllerNIKDetailViewController。在第一个中,我在表格视图中有一个音频文件列表;选择一行,它会导航到NIKDetailViewController,用户可以在其中查看有关该文件的一些信息并播放音频文件。 我在AVAudioPlayer中定义了NIKMasterViewController属性,并将其设置为: NIKMasterViewController.h

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

NIKMasterViewController.m

@synthesize audioPlayer;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        NIKDetailViewController *detailViewController = (NIKDetailViewController *) segue.destinationViewController;
        [detailViewController setAudioPlayer:audioPlayer];

        [detailViewController setFeedEntry:[[[self feedParser] feedItems] objectAtIndex:indexPath.row]];

    } else {
        NSLog(@"Segue Identifier: %@", segue.identifier);
    }

}

这就是AVAudioPlayerNIKMasterViewController的全部内容。现在我的NIKDetailViewController我有AVAudioPlayer的另一个属性:

NIKDetailViewController.h

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

现在在我的.m文件中,我有一个名为streamAudio的方法,在viewDidLoad中调用以准备音频播放,我有一个if条件,要求检查{ {1}}是audioPlayer,如果没有,如果nill为真,那么audioPlayer.isPlaying是播放器,但它永远不会被调用,当我导航回Master VC时点击另一行播放另一个文件,第二个文件在播放第一个文件时开始播放,一切都混淆了。

任何帮助都会得到真正的赞赏,因为我几乎要在几小时后无法解决这个问题后停止编程!

stop

NIKDetailViewController.m

注意:我已尝试将详细信息VC中的属性设置为@synthesize audioPlayer; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization selectedItem = [[NSString alloc]init]; } return self; } #pragma mark - Managing the Audio Playback - (IBAction)togglePlayingState:(id)button { //Handle the button pressing [self togglePlayPause]; } - (void)playAudio { //Play the audio and set the button to represent the audio is playing [audioPlayer play]; [playPauseButton setImage:[UIImage imageNamed:@"player_pause"] forState:UIControlStateNormal]; } - (void)pauseAudio { //Pause the audio and set the button to represent the audio is paused [audioPlayer pause]; [playPauseButton setImage:[UIImage imageNamed:@"player_play"] forState:UIControlStateNormal]; } - (void)togglePlayPause { //Toggle if the music is playing or paused if (!audioPlayer.playing) { [self playAudio]; } else if (audioPlayer.playing) { [self pauseAudio]; } } - (void)streamAudio { currentFileName = [[feedEntry podcastDownloadURL] lastPathComponent]; NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* path = [documentPath stringByAppendingPathComponent:currentFileName]; NSURL* audioURL = [NSURL fileURLWithPath: path]; if (audioPlayer != nil) { if (audioPlayer.isPlaying) { [audioPlayer stop]; //THIS IS NEVER CALLED } audioPlayer = nil; //THIS IS NEVER CALLED } audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; // Set a timer which keep getting the current music time and update the UISlider in 1 sec interval playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES]; // Set the maximum value of the UISlider seekSlider.maximumValue = audioPlayer.duration; currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)audioPlayer.currentTime / 60, (int)audioPlayer.currentTime % 60, nil]; remainingTime.text = [NSString stringWithFormat:@"%d:%02d", (int)(audioPlayer.duration - audioPlayer.currentTime) / 60, (int)(audioPlayer.duration - audioPlayer.currentTime) % 60, nil]; // Set the valueChanged target [seekSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged]; audioPlayer.delegate = self; [audioPlayer prepareToPlay]; //Add the audio to the memory. } - (void)updateSlider { // Update the slider about the music time seekSlider.value = audioPlayer.currentTime; } - (IBAction)sliderChanged:(UISlider *)sender { // Fast skip the music when user scrolls the slider [audioPlayer stop]; [audioPlayer setCurrentTime:seekSlider.value]; audioPlayer.delegate = self; [audioPlayer prepareToPlay]; [audioPlayer play]; } // Stop the timer when the music is finished (Need to implement the AVAudioPlayerDelegate in the Controller header) - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { // Music completed if (flag) { [playbackTimer invalidate]; } } - (IBAction)forwardAudio:(id)sender { int currentTime = [audioPlayer currentTime]; [audioPlayer setCurrentTime:currentTime+10]; } - (IBAction)rewindAudio:(id)sender { int currentTime = [audioPlayer currentTime]; [audioPlayer setCurrentTime:currentTime-10]; } //Make sure we can recieve remote control events - (BOOL)canBecomeFirstResponder { return YES; } - (void)remoteControlReceivedWithEvent:(UIEvent *)event { //if it is a remote control event handle it correctly if (event.type == UIEventTypeRemoteControl) { if (event.subtype == UIEventSubtypeRemoteControlPlay) { [self playAudio]; } else if (event.subtype == UIEventSubtypeRemoteControlPause) { [self pauseAudio]; } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) { [self togglePlayPause]; } } } #pragma mark - view life cycle - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //Once the view has loaded then we can register to begin recieving controls and we can become the first responder [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; //End recieving events [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self streamAudio]; //Make sure the system follows our playback status - to support the playback when the app enters the background mode. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end ,但之后,我收到警告,并且在播放文件之前该属性已释放。

1 个答案:

答案 0 :(得分:1)

所以......我可以通过创建singleton的{​​{1}}来解决此问题。这是如何:

  1. 首先,我从audioplayer类中删除了与audioPlayer相关的所有代码,其中包含NIKMasterViewController声明并在audioPlayer中设置。
  2. 我创建了一个名为prepareForSegue的新类。
  3. NIKAudioPlayer

    NIKAudioPlayer.h
  4. #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @interface NIKAudioPlayer : NSObject <AVAudioPlayerDelegate> { AVAudioPlayer *currentPlayer; } @property (nonatomic, strong) AVAudioPlayer *currentPlayer; +(NIKAudioPlayer *) sharedPlayer; -(void)playURL:(NSURL*)url; @end

    NIKAudioPlayer.m
  5. 现在,在我需要播放音频文件的代码中的其他地方(我的情况为#import "NIKAudioPlayer.h" @implementation NIKAudioPlayer @synthesize currentPlayer; +(NIKAudioPlayer *) sharedPlayer { static NIKAudioPlayer* sharedPlayer; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedPlayer = [[NIKAudioPlayer alloc] init]; }); return sharedPlayer; } -(void)playURL:(NSURL*)url { [currentPlayer stop]; currentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [currentPlayer prepareToPlay]; } @end ),我从NIKDetailViewController调用sharedPlayer

    NIKAudioPlayer
  6. 简而言之,将[[NIKPlayer sharedPlayer] playURL:audioURL]; [[NIKPlayer sharedPlayer].currentPlayer prepareToPlay]; 中的所有audioPlayer替换为NIKDetailViewController,甚至将其投射并随处使用:

    [NIKPlayer sharedPlayer].currentPlayer