2012-04-12 16 views
7

我在MP音樂播放器中出現錯誤的播放狀態。 在播放歌曲時我正處於暫停狀態。 我的應用程序在ios 4中工作正常,但我有這個問題在iOS 5。 任何人都可以幫助我?在MP音樂播放器控制器中出現錯誤的播放狀態ios 5

我的代碼在這裏。

[musicPlayer stop]; 
if (userMediaItemCollection) 
{ 
    userMediaItemCollection=nil; 
} 
musicPlayer.nowPlayingItem=nil; 

userMediaItemCollection=[MPMediaItemCollection collectionWithItems:[mediaItemCollection items]]; 

[musicPlayer setQueueWithItemCollection:userMediaItemCollection]; 
[musicPlayer setNowPlayingItem:  
[[userMediaItemCollectionitems]objectAtIndex:indexOfCurrentObject]]; 
[self enablePrevAndNextButtons]; 

[musicPlayer play];   
} 

-(void)playbackStateDidChanged:(NSNotification *)notification 
{ 

if (musicPlayer.playbackState!=MPMusicPlaybackStatePlaying) 
{ 
    [playPauseButton setBackgroundImage:[UIImage imageNamed:@"play_iPad.png"] forState:UIControlStateNormal]; 
} 
else if(musicPlayer.playbackState==MPMusicPlaybackStatePlaying) 
{ 
    [playPauseButton setBackgroundImage:[UIImage imageNamed:@"pause_iPad.png"] forState:UIControlStateNormal]; 
} 
+0

可以表現出一定的一段代碼在這裏..? – Kamarshad 2012-04-12 07:18:50

+0

@Kamarshad用代碼編輯的問題。 – 2012-04-12 07:42:17

+0

@VivekParikh - 請檢查以下問題。 http://stackoverflow.com/questions/8854923/did-mpmusicplayercontroller-change-with-ios-5 – itsaboutcode 2012-11-03 12:19:32

回答

0

我有同樣的問題,但是當我播放/暫停很多時候我的應用程序在後臺,我報告的錯誤的蘋果,並希望儘快得到一個amswer,我想知道如果我的編碼是錯誤的或者有一個API問題。如果這是您遇到的錯誤,這可能會有所幫助。我來到了一個解決辦法,其即使心不是最好的解決辦法,我的應用程序是可以接受的:

在viewDidLoad中,補充一點:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; 
[notificationCenter addObserver: self 
         selector: @selector (handle_ApplicationDidBecomeActive:) 
          name: UIApplicationDidBecomeActiveNotification 
         object: nil]; 

然後,創建一個handle_ApplicationDidBecomeActive方法,並添加此:

- (void) handle_ApplicationDidBecomeActive: (id) notification 
{ 
    if (musicPlayer.playbackState!=MPMusicPlaybackStatePlaying) 
    { 
     [playPauseButton setBackgroundImage:[UIImage imageNamed:@"play_iPad.png"] forState:UIControlStateNormal]; 
     [musicPlayer pause]; 
    } 
    else if(musicPlayer.playbackState==MPMusicPlaybackStatePlaying) 
    { 
     [playPauseButton setBackgroundImage:[UIImage imageNamed:@"pause_iPad.png"] forState:UIControlStateNormal]; 
     [musicPlayer pause]; 
    } 
} 

(不要把這個代碼的playbackStateDidChanged方法中,因爲這可能會產生一個死循環)

這將同步喲狀態你的按鈕和音樂播放器到API報告的那個。在有巧合的情況下,不會有任何類型的影響,在其他情況下,玩家會相應地暫停/播放。

0

我經歷了與iOS 5的發佈我發現playbackState屬性不會得到更新同樣的問題,而是在延遲之後,所以它沒有當你playbackStateDidChanged方法運行設置。

我的解決方法是把我自己的實例變量,名爲musicPlayerRecentlyStartedPlaying每當我開始播放。然後,我用從一個計時器調用的方法來檢查這兩個變量和playbackState屬性來看看玩家真正玩:

- (void)playMusic { 
    [self.musicPlayer play]; 
    self.musicPlayerRecentlyStartedPlaying = TRUE; 
    self.musicControlsUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateMusicControls:) userInfo:nil repeats:TRUE]; 
} 

- (void)stopMusic { 
    [self.musicPlayer stop]; 
    self.musicPlayerRecentlyStartedPlaying = FALSE; 
    [self.musicControlsUpdateTimer invalidate]; 
} 

- (void)updateMusicControls:(NSTimer *)timer { 
    BOOL playing = (([self.musicPlayer playbackState] == MPMusicPlaybackStatePlaying)&&(self.musicPlayer.nowPlayingItem)); 
    if (!playing) { 
     // check to see if we recently started playing 
     if (self.musicPlayerRecentlyStartedPlaying) { 
      playing = TRUE; 
     } 
    } else { 
     // once the property is updated, we no longer need this 
     self.musicPlayerRecentlyStartedPlaying = FALSE; 
    } 
} 

您可能不需要從一個計時器調用updateMusicControls,但我這樣做,因爲音樂播放時,我還更新了進度條的位置。

+0

得到修復無論我等待多久檢查播放狀態,直到我在應用程序處於活動狀態時暫停/重新開始播放時纔會有錯誤。 – 2013-01-27 21:33:26

+0

我的代碼假設您只是在應用內播放和暫停音樂。我不認爲原始問題提到了後臺操作,儘管其他答案指的是這種情況。 – arlomedia 2013-01-28 00:15:43

+0

讓我最頭疼的場景是有人離開應用程序來改變歌曲。即使新歌正在播放,給予應用程序的通知和播放狀態也會顯示歌曲已暫停播放。 – 2013-01-28 12:51:14

3

這些變通辦法都沒有解決我的應用程序的問題。這是iOS中的一個錯誤,在Apple修復它之前,我的應用程序將無法正常運行。

我有一個音樂播放器,播放/暫停按鈕。當播放音樂時,該按鈕顯示「暫停」圖標。當音樂暫停時,按鈕會顯示「播放」圖標 - 就像所有音樂應用程序一樣。我可以通過以下操作隨時複製錯誤:在我的應用程序 1.播放音樂(播放/暫停按鈕會顯示正確的「暫停」圖標) 背景我的應用程序,並鎖住我的手機〜10分鐘 3.雙擊Home,打從iPod暫停按鈕控制 4.解鎖我的手機,然後再次打開我的應用程序 5.音樂將被停止,但我的應用程序仍顯示「暫停」圖標,當它應該這樣「玩」

我已經做了廣泛的調試和記錄,以確保該更新我的播放方法/暫停按鈕總是在我的應用程序被激活調用。問題是,當我重新進入我的應用程序時,即使音樂停止/暫停,MPMusicPlayer的播放狀態仍然會設置爲MPMusicPlaybackStatePlaying。

我在一年前提交了一份關於這方面的錯誤報告,並沒有從Apple那裏聽到任何消息。如果其他人將提交一個它將不勝感激。

4

我也向Apple報告了這個bug。通過執行以下操作,我能夠100%地重現它:

啓動使用MPMusicPlayerController的應用程序。 啓動「音樂」應用程序。 點擊播放,跳過,跳過,暫停,播放,暫停 打開原始應用程序並且MPMusicPlayerController的MPMusicPlaybackState將不正確。

這裏沒有提出的解決方案爲我工作。確實有效的解決方案是在這些情況下專門跟蹤錯誤發生的時間並更新UI。

當接收到UIApplicationDidBecomeActiveNotification通知(見matbur更多這方面的細節後),看看音頻是否實際上是不玩時MPMusicPlaybackState說這是:

-(BOOL) isPlaybackStateBugActive { 
    MPMusicPlaybackState playbackState = self.musicPlayer.playbackState; 
    if (playbackState == MPMusicPlaybackStatePlaying) { 
     AudioSessionInitialize (NULL, NULL, NULL, NULL); 
     UInt32 sessionCategory = kAudioSessionCategory_AmbientSound; 
     AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory); 
     AudioSessionSetActive (true); 

     UInt32 audioIsPlaying; 
     UInt32 size = sizeof(audioIsPlaying); 
     AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &size, &audioIsPlaying); 

     if (!audioIsPlaying){ 
      NSLog(@"PlaybackState bug is active"); 
      return YES; 
     } 
    } 

    return NO; 
} 

不要忘了導入AudioToolbox框架。

+0

我應該如何處理這些信息?我怎樣才能得到正確的狀態?或者我該如何重置播放器? – MetaImi 2012-11-27 23:50:53

+0

我懂了。如果audioIsPlaying爲true,則即使音樂播放器狀態不正確,您也可以使用musicplayer暫停方法暫停音樂。反之亦然。因此,通過此代碼片段,您可以修復mpmusicplayercontroller – MetaImi 2012-11-28 00:07:59

0

該代碼使用時,前面的按鈕點擊上一首歌曲將發揮

- (IBAction)playPreviousSongInList:(id)sender { 

     static NSTimeInterval skipToBeginningOfSongIfElapsedTimeLongerThan = 3.5; 

     NSTimeInterval playbackTime = self.musicPlayer.currentPlaybackTime; 
     if (playbackTime <= skipToBeginningOfSongIfElapsedTimeLongerThan) { 

      [self.musicPlayer skipToPreviousItem]; 

     } else { 

      [self.musicPlayer skipToBeginning]; 
     } 
    }