检查ipod音乐是否有问题

时间:2011-04-09 09:57:57

标签: objective-c ios4 media-player nscoder

我想检查是否播放了ipod音乐,所以我添加了mediaplayer框架并导入它:

#import <MediaPlayer/MediaPlayer.h>

然后我进入了NSCoder:

-(id)initWithCoder:(NSCoder *)coder { 

self = [super initWithCoder:coder];

if(self)
{

    if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
    }
    else {

        [self playBgMusic];


    } }

return self; }

但这不起作用。如果正在播放ipod音乐,我启动应用程序,iPod音乐将关闭,播放应用程序中的音乐。

怎么了?

1 个答案:

答案 0 :(得分:1)

  1. 将AVFoundation框架添加到您的项目中(如果没有)。
  2. 将其导入您的app delegate。

    #import <AVFoundation/AVFoundation.h>
    
  3. 将此代码放入您的应用程序:didFinishLaunchingWithOptions:方法,以便在应用启动时运行。

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

  4. 这会设置您的应用使用的音频会话的“类别”,以便与iPod音乐播放器混音。默认情况下,类别设置为“AVAudioSessionCategorySoloAmbient”,不允许混音。请参阅下面的Apple文档。

    Working with Movies and iPod Music

    AVAudioSession Class Reference

相关问题