问题描述
我试图快速集成" Windows Media Player 通过 COM 播放来自本地文件系统或 http 源的单个文件 - 但由于其使用的文档和在线资源稀少当没有嵌入到某种 Ole 容器中时,我无法让那个所谓的微不足道的用例工作.
I was trying to "just quickly integrate" the Windows Media Player via COM to play single files from the local file system or http sources - but due to the sparse documentation and online resources to its usage when not embedding into some kind of an Ole container, i couldn't get that supposedly trivial use-case to work.
初始化等工作正常,但实际播放某些文件总是失败.
Initialization etc. works fine, but actually playing some file always fails.
示例代码,从初始化开始(删除了错误处理,基本上翻译自 MSDN 上的 C# 示例,在主线程上执行):
Example code, starting with initialization (error handling stripped, basically translated from the C# example at MSDN, executed on the main thread):
CComPtr<IWMPPlayer> player;
player.CoCreateInstance(__uuidof(WindowsMediaPlayer), 0, CLSCTX_INPROC_SERVER);
CComQIPtr<IWMPCore3> core(player);
CComPtr<IWMPControls> controls;
core->get_controls(&controls);
CComPtr<IWMPPlaylist> playlist;
core->get_currentPlaylist(&playlist);
CComBSTR path("c:\bar.mp3"); // alternatively http://foo/bar.mp3
玩某些东西的第一种方法命令不可用":
The first approach to play something gives "command not available":
core->put_url(path);
// ... waiting after that for WMP to load doesn't make a difference
controls->play(); // returns 0x000D1105 - NS_S_WMPCORE_COMMAND_NOT_AVAILABLE
第二种方法只产生S_OK
s,但实际上没有播放:
The second approach only produces S_OK
s, but nothing is actually played:
CComPtr<IWMPMedia> media;
core->newMedia(path, &media);
playlist->appendItem(media);
controls->playItem(media); // returns S_OK, but doesn't play
我注意到的另一件事是 core->get_playState()
总是返回 wmposMediaOpening
,无论我等待多久.
Another thing i noted is that core->get_playState()
always returns wmposMediaOpening
, no matter how long i wait.
我偶然发现一个线程表明多线程可能无法与 WMP 正常工作,并且此代码在多线程单元中运行.这可能是问题吗?
如果没有,还有什么可能阻止 WMP 播放文件?
I've stumbled upon one thread that suggests multi-threading might not work properly with WMP and this code runs in a multi-threaded apartment. Might that be the problem?
If not, what else could be preventing WMP from playing the files?
显着背景:
WMP 实例是在 DLL 中创建的,浏览器作为主机进程.
Notable background:
The WMP instance is created in a DLL with a browser as the host-process.
更新:
尝试使用 WMP 本身应该使用的普通 DirectShow,会出现一个更具体的问题 - 请参阅该问题.
推荐答案
经过进一步调查,原来这是实际上是由 VS2008s AtlSetPerUserRegistration()
的 VS2005 解决方法引起的,该解决方法始终处于活动状态 - 但应该仅用于包含的 COM 服务器注册/取消注册.
After further investigation, it turned out that this was actually caused by a VS2005 workaround for VS2008s AtlSetPerUserRegistration()
which was always active - but should have been only for the contained COM servers registration/unregistration.
解决方法用 HKEY_CURRENT_USER
覆盖 HKEY_LOCAL_MACHINE
,这显然会导致相当多的组件在进程中创建时失败.
The workaround overrides HKEY_LOCAL_MACHINE
with HKEY_CURRENT_USER
, which obviously results in quite some components failing if they are created in-process.
这篇关于使用以编程方式创建的 Windows 媒体播放器进行基本播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!