Skip to content

Commit

Permalink
feat: ✨ add mobile player
Browse files Browse the repository at this point in the history
  • Loading branch information
caorushizi committed Aug 11, 2024
1 parent 9ddd748 commit 3d92a5a
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 166 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"build:plugin": "pnpm -F plugin run build",
"build:main": "pnpm -F main run build",
"build:renderer": "pnpm -F renderer run build",
"build:mobile": "pnpm -F mobile run build",
"beta": "pnpm run build && pnpm -F mediago run pack",
"release": "pnpm run build && pnpm -F main run release",
"docs:dev": "vitepress dev docs",
Expand Down
6 changes: 5 additions & 1 deletion packages/main/scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function getConfig(): esbuild.BuildOptions {
function buildOptions(
entry: string,
platform: esbuild.Platform,
target: string
target: string,
): esbuild.BuildOptions {
return {
...getConfig(),
Expand Down Expand Up @@ -80,6 +80,10 @@ export function getReleaseConfig(): Configuration {
from: "./app/plugin",
to: "plugin",
},
{
from: "./app/mobile",
to: "mobile",
},
{
from: "./app/bin/",
to: "bin",
Expand Down
4 changes: 3 additions & 1 deletion packages/main/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ async function clean() {
return deleteSync([
mainResolve("app/build"),
mainResolve("app/bin"),
mainResolve("app/mobile"),
mainResolve("app/plugin"),
mainResolve("release"),
]);
}
Expand Down Expand Up @@ -91,7 +93,7 @@ async function pack() {
}

// 开发环境
export const dev = gulp.series(clean, copy, chmodBin, watchTask);
export const dev = gulp.series(copy, chmodBin, watchTask);
// 构建打包
export const build = gulp.series(clean, copy, chmodBin, buildTask);
// release
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/helper/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export const m3u8DownloaderBin = resolveBin("N_m3u8DL-RE");

// plugin path
export const pluginPath = resolveStatic("plugin/index.js");
// mobile path
export const mobileDir = resolveStatic("mobile");

// user agent
export const pcUA =
Expand Down
15 changes: 13 additions & 2 deletions packages/main/src/services/VideoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import path from "path";
import express from "express";
import ElectronLogger from "../vendor/ElectronLogger.ts";
import cors from "cors";
import { mobileDir } from "../helper/variables.ts";
import { getLocalIP } from "../helper/index.ts";

@injectable()
export class VideoService {
private port = 3222;
private videoDir: string;
private localIp: string;

constructor(
@inject(TYPES.ElectronStore)
private readonly store: StoreService,
@inject(TYPES.ElectronLogger)
private readonly logger: ElectronLogger,
) {
this.videoDir = this.store.get("local");
this.localIp = getLocalIP();
}

// 使用glob搜索视频文件
Expand All @@ -34,7 +39,7 @@ export class VideoService {
const fileName = path.basename(file);
return {
title: fileName,
url: `http://localhost:${this.port}/${encodeURIComponent(fileName)}`,
url: `http://${this.localIp}:${this.port}/${encodeURIComponent(fileName)}`,
};
});
return videos;
Expand All @@ -45,11 +50,17 @@ export class VideoService {

app.use(cors());

app.get("/", async (req, res) => {
app.get("/", (req, res) => {
res.sendFile(path.join(mobileDir, "index.html"));
});

app.get("/api", async (req, res) => {
const videos = await this.getVideoFiles();
res.json(videos);
});

app.use(express.static(mobileDir));

// 使用serve-handler处理静态文件请求的中间件
app.use(async (req, res) => {
return serveHandler(req, res, {
Expand Down
11 changes: 10 additions & 1 deletion packages/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@
"preview": "vite preview"
},
"dependencies": {
"ahooks": "^3.8.0",
"antd-mobile": "^5.37.1",
"axios": "^1.7.2",
"clsx": "^2.1.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"tailwind-merge": "^2.3.0",
"xgplayer": "^3.0.19"
},
"devDependencies": {
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"@vitejs/plugin-react-swc": "^3.5.0",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"postcss": "^8.4.39",
"tailwindcss": "^3.4.4",
"typescript": "^5.2.2",
"vite": "^5.3.1"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/mobile/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
42 changes: 0 additions & 42 deletions packages/mobile/src/App.css

This file was deleted.

79 changes: 50 additions & 29 deletions packages/mobile/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { useMount } from "ahooks";
import { List, NavBar } from "antd-mobile";
import axios from "axios";
import { useRef, useState } from "react";
import Player from "xgplayer";
import "xgplayer/dist/index.min.css";
import { cn } from "./utils";

function App() {
const [count, setCount] = useState(0)
const [data, setData] = useState<any[]>([]);
const player = useRef<Player>();
const [currentVideo, setCurrentVideo] = useState("");

useMount(async () => {
const { data } = await axios.get<any[]>("/api");
setData(data);
setCurrentVideo(data[0].url);

player.current = new Player({
id: "mse",
url: data[0].url,
fluid: true,
lang: "zh-cn",
});
});

const handleVideoClick = (url: string) => {
if (!player.current) return;
setCurrentVideo(url);
player.current.src = url;
player.current?.play();
};

return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
<div className="flex flex-col h-full w-full overflow-hidden">
<NavBar back={null}>视频播放</NavBar>
<div id="mse" />
<List header="播放列表" className="flex-1 overflow-auto">
{data.map((item) => {
return (
<List.Item
onClick={() => handleVideoClick(item.url)}
className={cn({
"text-blue-500": item.url === currentVideo,
})}
>
{item.title}
</List.Item>
);
})}
</List>
</div>
);
}

export default App
export default App;
1 change: 0 additions & 1 deletion packages/mobile/src/assets/react.svg

This file was deleted.

71 changes: 5 additions & 66 deletions packages/mobile/src/index.css
Original file line number Diff line number Diff line change
@@ -1,68 +1,7 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
@tailwind base;
@tailwind components;
@tailwind utilities;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
#root {
height: 100vh;
}
6 changes: 6 additions & 0 deletions packages/mobile/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { twMerge } from "tailwind-merge";
import { type ClassValue, clsx } from "clsx";

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
8 changes: 8 additions & 0 deletions packages/mobile/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
15 changes: 13 additions & 2 deletions packages/mobile/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
server: {
port: 8556,
strictPort: true,
host: true,
},
plugins: [react()],
build: {
outDir: path.resolve(__dirname, "../main/app/player"),
outDir: path.resolve(__dirname, "../main/app/mobile"),
emptyOutDir: true,
rollupOptions: {},
},
envDir: "../..",
envPrefix: "APP",
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
});
Loading

0 comments on commit 3d92a5a

Please sign in to comment.