MPMoviePlayerController停止iPod音乐播放

时间:2012-11-20 04:40:42

标签: iphone ios mpmovieplayercontroller

在我的应用程序中,我使用MPMoviePlayerController在屏幕上播放循环视频,我使用MPMusicPlayerController播放音乐。当我开始播放视频时,音乐停止。我不确定我做错了什么。我已经尝试在我的应用程序中设置AudioSesion,但它似乎没有用。

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];

这是我的代码:

self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
MPMediaQuery* query = [MPMediaQuery songsQuery];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"Lost" forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonEqualTo]];
[query setGroupingType:MPMediaGroupingAlbum];

//Pass the query to the player
[self.musicPlayer setQueueWithQuery:query];
[self.musicPlayer play];

NSBundle *bundle = [NSBundle mainBundle];

NSString *moviePath = [bundle pathForResource:@"movie" ofType:@"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

// Play the movie.
[self.moviePlayer setRepeatMode:MPMovieRepeatModeOne];

1 个答案:

答案 0 :(得分:2)

詹姆斯。。当我从我的应用程序播放音频文件时,我也遇到了这个问题。 I was worked on AVAudioPlayer to play song from my application。当我从我的应用程序播放一首歌时,如果iPod播放的歌曲已经停止,并且从未暂停或再次播放。所以我在我的应用程序中使用了以下代码,它具有工作魅力。

我不知道这会解决你的问题。试试这个,让我知道你的意见。

 musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
 if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) 
  {
       NSLog(@"Music Playing now");
       musicPlayerPaused = YES; //BOOL
       [musicPlayer pause]; // Here am pausing the iPod Music from here..
  } 

 // Playing the message sound here using the AVAudioPlayer...
 [sendMessageSound play];

AVAudioPlayer 委托我再次恢复iPodMusic,

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if (flag == YES) 
    {
        // IF the iPod song paused by us... we have to play it again after our Sound plays successfully...
        if (musicPlayerPaused == YES) 
        {
            musicPlayerPaused = NO;
            [musicPlayer play];
        }

    }
} 

请尝试使用,并告诉我您的意见。谢谢。祝一切顺利。