从另一个应用程序控制 iPod?
我查看了 Apple 的 AddMusic
示例代码,但这并不是我想要的。我正在尝试找到一种方法来开始/停止 iPod 应用程序中的音乐播放,而不是控制应用程序沙箱中包含的内容。
像这样的东西......
____________
| |
| |< || >| | skip back, play/pause, skip forward
| |
| XXXXXXXX | album art
| XXXXXXXX |
| XXXXXXXX |
| XXXXXXXX |
| |
|____________|
当然,应用程序比这更复杂,但现在,我想专注于假设直接控制。
任何在没有作弊的情况下获得推荐的人都会获得奖励积分。
— 图稿加载错误移至一个新问题。
— 文本转语音实现移至一个新问题。
— 歌曲一开始,进度条就会填满 100%。这段代码的工作原理:
// your must register for notifications to use them
- (void)handleNowPlayingItemChanged:(id)notification {
…
// this is what I forgot to do
NSNumber *duration = [item valueForProperty:MPMediaItemPropertyPlaybackDuration];
float totalTime = [duration floatValue];
progressSlider.maximumValue = totalTime;
…
}
// called on a one-second repeating timer
- (void)updateSlider {
progressSlider.value = musicPlayer.currentPlaybackTime;
[progressSlider setValue:musicPlayer.currentPlaybackTime animated:YES];
}
I took a look at Apple's AddMusic
sample code, but that's not quite what I want. I'm trying to find a way to start/stop music playing in the iPod app, not controlling what's contained in my app's sandbox.
Something like this...
____________
| |
| |< || >| | skip back, play/pause, skip forward
| |
| XXXXXXXX | album art
| XXXXXXXX |
| XXXXXXXX |
| XXXXXXXX |
| |
|____________|
Of course the app is more complex than this, but for now, I want to focus on assuming direct control.
Bonus points to anyone who gets the reference without cheating.
— Artwork loading error moved to a new question.
— Text-to-Speech implementation moved to a new question.
— Progress bar fills to 100% as soon as the song starts. This code works:
// your must register for notifications to use them
- (void)handleNowPlayingItemChanged:(id)notification {
…
// this is what I forgot to do
NSNumber *duration = [item valueForProperty:MPMediaItemPropertyPlaybackDuration];
float totalTime = [duration floatValue];
progressSlider.maximumValue = totalTime;
…
}
// called on a one-second repeating timer
- (void)updateSlider {
progressSlider.value = musicPlayer.currentPlaybackTime;
[progressSlider setValue:musicPlayer.currentPlaybackTime animated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

MediaPlayer 框架 就是您正在寻找的。有一个 有用指南,其中还包含 MediaPlayer API 概述的链接。
具体来说,您正在寻找
MPMusicPlayerController
。您的应用程序可以控制专用的应用程序媒体播放器或 iPod 媒体播放器,这似乎是您想要的。一个简单的例子如下(省略标题):
请花时间阅读文档并记下有关家庭共享的注意事项。您还需要注册有关可能在您控制之外发生的玩家状态更改的通知。
The MediaPlayer framework is what you are looking for. There is a useful guide which also contains a link to the MediaPlayer API overview.
Specifically, you're looking for
MPMusicPlayerController
. Your app can control either a dedicated app media player or the iPod media player which appears to be what you want.A simple example would be something like (header omitted):
Do take time to read the documentations and take note of the caveats mentioned with regards to Home Sharing. You'll also want to register for notifications in changes to the player state that can occur from outside your control.