在我的应用程序中,我使用 MPMoviePlayerController 在屏幕上播放循环视频,并使用 MPMusicPlayerController 播放音乐。当我开始播放视频时,音乐停止了。我不确定我做错了什么。我已尝试在我的应用程序委托(delegate)中设置 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];
最佳答案
詹姆斯先生。当我从我的应用程序播放音频文件时,我也遇到了这个问题。 我曾在 AVAudioPlayer 上工作以从我的应用程序播放歌曲
。当我从我的应用程序播放歌曲时,如果 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 delegate am again resuming the 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];
}
}
}
请试试这个,让我知道您的意见。谢谢。一切顺利。
关于iphone - MPMoviePlayerController 停止 iPod 音乐播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466696/