0

在我的应用程序中,我使用 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];
4

1 回答 1

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];
        }

    }
} 

请试试这个,让我知道你的意见。谢谢。一切顺利。

于 2012-11-20T05:34:45.370 回答