开始使用
Copy a setup prompt with the install steps and the full markdown guide for this plugin.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-jw-player`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/zh/docs/plugins/jw-player/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
npm install @capgo/capacitor-jw-playernpx cap syncyarn add @capgo/capacitor-jw-playernpx cap syncpnpm add @capgo/capacitor-jw-playernpx cap syncbun add @capgo/capacitor-jw-playernpx cap sync您需要 JW Player 账户和许可证密钥才能使用此插件。如果还没有账户,请在 JW Player 注册。
将您的 JW Player 许可证密钥添加到 Info.plist:
<key>JWPlayerLicenseKey</key><string>YOUR_LICENSE_KEY</string>Android
Section titled “Android”将您的 JW Player 许可证密钥添加到 android/app/src/main/res/values/strings.xml:
<resources> <string name="jw_player_license_key">YOUR_LICENSE_KEY</string></resources>import { JWPlayer } from '@capgo/capacitor-jw-player';
// 播放单个视频await JWPlayer.playVideo({ mediaUrl: 'https://example.com/video.mp4', title: 'My Video', description: 'Video description', poster: 'https://example.com/poster.jpg'});
// 播放播放列表await JWPlayer.playPlaylist({ playlistUrl: 'https://example.com/playlist.json'});
// 监听播放器事件JWPlayer.addListener('playerReady', () => { console.log('Player is ready');});
JWPlayer.addListener('play', (data) => { console.log('Video started playing');});
JWPlayer.addListener('pause', (data) => { console.log('Video paused');});
JWPlayer.addListener('complete', (data) => { console.log('Video playback completed');});
JWPlayer.addListener('error', (error) => { console.error('Player error:', error);});API 参考
Section titled “API 参考”playVideo(options)
Section titled “playVideo(options)”playVideo(options: VideoOptions) => Promise<void>以全屏模式播放单个视频。
| 参数 | 类型 |
|---|---|
options | VideoOptions |
playPlaylist(options)
Section titled “playPlaylist(options)”playPlaylist(options: PlaylistOptions) => Promise<void>以全屏模式播放播放列表。
| 参数 | 类型 |
|---|---|
options | PlaylistOptions |
pause()
Section titled “pause()”pause() => Promise<void>暂停当前视频。
play()
Section titled “play()”play() => Promise<void>恢复视频播放。
seek(options)
Section titled “seek(options)”seek(options: { position: number }) => Promise<void>跳转到视频的特定位置。
| 参数 | 类型 |
|---|---|
options | { position: number } |
setPlaybackSpeed(options)
Section titled “setPlaybackSpeed(options)”setPlaybackSpeed(options: { speed: number }) => Promise<void>设置播放速度。
| 参数 | 类型 |
|---|---|
options | { speed: number } |
setVolume(options)
Section titled “setVolume(options)”setVolume(options: { volume: number }) => Promise<void>设置音频音量(0.0 到 1.0)。
| 参数 | 类型 |
|---|---|
options | { volume: number } |
selectAudioTrack(options)
Section titled “selectAudioTrack(options)”selectAudioTrack(options: { trackIndex: number }) => Promise<void>按索引选择音频轨道。
| 参数 | 类型 |
|---|---|
options | { trackIndex: number } |
selectCaptionTrack(options)
Section titled “selectCaptionTrack(options)”selectCaptionTrack(options: { trackIndex: number }) => Promise<void>按索引选择字幕轨道。
| 参数 | 类型 |
|---|---|
options | { trackIndex: number } |
stop()
Section titled “stop()”stop() => Promise<void>停止播放并关闭播放器。
VideoOptions
Section titled “VideoOptions”| 属性 | 类型 | 描述 |
|---|---|---|
mediaUrl | string | 视频文件的 URL |
title | string | 视频标题(可选) |
description | string | 视频描述(可选) |
poster | string | 海报/缩略图的 URL(可选) |
autoStart | boolean | 自动开始播放(可选,默认:true) |
PlaylistOptions
Section titled “PlaylistOptions”| 属性 | 类型 | 描述 |
|---|---|---|
playlistUrl | string | JW 播放列表 JSON 的 URL |
autoStart | boolean | 自动开始播放(可选) |
playerReady- 播放器已初始化并准备就绪play- 视频播放开始pause- 视频播放暂停complete- 视频播放完成error- 播放器遇到错误seek- 用户跳转到不同位置time- 播放时间更新bufferChange- 缓冲状态改变
// 监听时间更新JWPlayer.addListener('time', (data) => { console.log('Current time:', data.position); console.log('Duration:', data.duration);});
// 监听缓冲变化JWPlayer.addListener('bufferChange', (data) => { console.log('Buffering:', data.buffering);});
// 完成后移除监听器const listener = await JWPlayer.addListener('play', (data) => { console.log('Playing');});
// 稍后...listener.remove();await JWPlayer.playVideo({ mediaUrl: 'https://example.com/video.mp4', title: 'Video with Subtitles', poster: 'https://example.com/poster.jpg', tracks: [ { file: 'https://example.com/captions-en.vtt', label: 'English', kind: 'captions' }, { file: 'https://example.com/captions-es.vtt', label: 'Spanish', kind: 'captions' } ]});自定义播放控制
Section titled “自定义播放控制”import { JWPlayer } from '@capgo/capacitor-jw-player';
// 开始播放await JWPlayer.play();
// 10 秒后暂停setTimeout(() => { JWPlayer.pause();}, 10000);
// 跳转到 30 秒await JWPlayer.seek({ position: 30 });
// 设置播放速度为 1.5 倍await JWPlayer.setPlaybackSpeed({ speed: 1.5 });
// 设置音量为 50%await JWPlayer.setVolume({ volume: 0.5 });- 始终提供海报图片以获得更好的用户体验
- 通过适当的错误消息优雅地处理播放器错误
- 在组件卸载时清理事件监听器
- 在 iOS 和 Android 设备上测试视频播放
- 使用适当的视频格式(推荐 MP4)
- 在视频初始化时实现加载状态
- 在流式传输时考虑网络条件
视频无法播放
Section titled “视频无法播放”- 验证您的 JW Player 许可证密钥是否正确
- 检查视频 URL 是否可访问且有效
- 如果使用自定义服务器,请确保正确的 CORS 标头
- 测试不同的视频格式
- 插件始终以全屏模式播放
- 确保应用中具有全屏的适当权限
- 为目标设备使用适当的视频质量
- 考虑使用自适应流媒体以获得更好的性能
- 实现适当的缓冲指示器