iphone - MPMoviePlayerController 停止 iPod 音乐播放

标签 iphone ios mpmovieplayercontroller

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

相关文章:

iphone - 测试我发送到 AppStore 的应用程序

ios - NSFetchRequest 从 Core Data iOS 返回错误

iphone - 在 openGL 中检测 3D 对象的触摸位置

ios - 是否可以使用 PDF 远程数据初始化 UIImage?

ios - 当应用程序激活时刷新 Parse.com 数据

ios - UICollectionViewData updateItemCounts 崩溃

iOS - MPMoviePlayerController - 控制大小

ios - MPMoviePlayerController 无法播放本地视频

iphone - Objective-C : call super class

iphone - itemFailedToPlayToEnd - MPMoviePlayerController - iOS7