检测音乐是否正在播放?

发布于 2024-08-21 17:38:42 字数 108 浏览 13 评论 0原文

我的应用程序涉及音乐(iPodMusic),并且有一个 UISwitch 切换播放/暂停。我的目标是能够检测音乐是否正在播放,以便播放/暂停开关可以在音乐播放时说“播放”,如果没有播放音乐则说“暂停”。

My app involves music(iPodMusic), and there is a UISwitch toggling play/pause. My goal is to be able to detect if music is playing, so that therefore the play/pause switch can say 'play' when music is playing and 'pause' if it isn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明媚如初 2024-08-28 17:38:42
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) ...
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) ...
深巷少女 2024-08-28 17:38:42

如果音乐来自您自己的应用程序,请检查 AVAudioPlayer 的 playing 属性

如果音乐来自 iPod,请检查 MPMusicPlayerController 的 nowPlayingItemplaybackState 属性。

If the music is from your own app, check AVAudioPlayer's playing property.

If the music is from iPod, check MPMusicPlayerController's nowPlayingItem or playbackState property.

沧桑㈠ 2024-08-28 17:38:42

MPMusicPlayerController 仅适用于 OS 3.0 或更高版本。如果你运行的是 2.0,那你就不走运了。下面是一个代码片段,用于检查您是否正在运行 3.0 或更高版本,然后才尝试创建 MPMuiscPlayerController

bool playerDetectedAndPlaying = false;
NSString *reqSysVer = @"3.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
    Class MusicPlayerController = NSClassFromString(@"MPMusicPlayerController");
    if (MusicPlayerController){         
        id myMusicPlayerController = [[MusicPlayerController alloc]init];
        id MusicPlayer = [[myMusicPlayerController class] iPodMusicPlayer ];
        if ( [ MusicPlayer playbackState ] == MPMusicPlaybackStatePlaying ) {
            playerDetectedAndPlaying = true;
        }
    }
}

您必须针对 3.0 SDK 进行编译,但如果您将部署目标设置为 2.0,则此代码仍然可以在较旧的设备上运行。

MPMusicPlayerController is only available in OS 3.0 or above. If you're running 2.0 you're out of luck. Here's a code snippet that checks if you're running 3.0 or above and only then attempts to create an MPMuiscPlayerController

bool playerDetectedAndPlaying = false;
NSString *reqSysVer = @"3.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
    Class MusicPlayerController = NSClassFromString(@"MPMusicPlayerController");
    if (MusicPlayerController){         
        id myMusicPlayerController = [[MusicPlayerController alloc]init];
        id MusicPlayer = [[myMusicPlayerController class] iPodMusicPlayer ];
        if ( [ MusicPlayer playbackState ] == MPMusicPlaybackStatePlaying ) {
            playerDetectedAndPlaying = true;
        }
    }
}

You have to compile against a 3.0 SDK, but if you set the deployment target to 2.0, this code still runs on older devices.

淡淡绿茶香 2024-08-28 17:38:42

如果你的 iTunes 声音打开,它会变成 yes,否则如果 iTunes 音乐播放器的声音关闭,它会变成 no ... 所以你可以轻松检查默认的 MusicPlayer 声音是打开还是关闭 你需要添加媒体播放器框架 Just Try It it会正常工作...谢谢...:)

   if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
    {
           NSLog(@"yes itune Player Sound is on");
    }
    else
    {
          NSLog(@"NO itune Player Sound is not on");
    }

it goes to yes if your iTunes sound is on else it goes to no if sound is off of iTunes music player ... So You can check easily default MusicPlayer Sound is On or Off You need to add media-player Framework Just Try It it will work Properly.... Thx...:)

   if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
    {
           NSLog(@"yes itune Player Sound is on");
    }
    else
    {
          NSLog(@"NO itune Player Sound is not on");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文