using audio tag for audio playback

This commit is contained in:
Roland Osborne 2022-08-26 11:54:58 -07:00
parent f2f1c0b240
commit 90f08f8093
7 changed files with 139 additions and 67 deletions

View File

@ -188,7 +188,7 @@ async function upload(entry, update, complete) {
}); });
entry.assets.push({ entry.assets.push({
audio: { audio: {
label: asset.label, label: file.label,
full: asset.data.find(item => item.transform === 'acopy;audio').assetId, full: asset.data.find(item => item.transform === 'acopy;audio').assetId,
} }
}); });

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -16,6 +16,8 @@ export function AddTopic({ cardId, channelId }) {
const attachVideo = useRef(null); const attachVideo = useRef(null);
const msg = useRef(); const msg = useRef();
console.log(state);
const keyDown = (e) => { const keyDown = (e) => {
if (e.key === 'Enter' && !e.shiftKey) { if (e.key === 'Enter' && !e.shiftKey) {
msg.current.blur(); msg.current.blur();
@ -58,7 +60,7 @@ export function AddTopic({ cardId, channelId }) {
return <img style={{ height: 128, objectFit: 'contain' }} src={item.url} alt="" /> return <img style={{ height: 128, objectFit: 'contain' }} src={item.url} alt="" />
} }
if (item.audio) { if (item.audio) {
return <AudioFile onLabel={(label) => actions.setLabel(index, label)}/> return <AudioFile onLabel={(label) => actions.setLabel(index, label)} url={item.url} />
} }
if (item.video) { if (item.video) {
return <VideoFile onPosition={(pos) => actions.setPosition(index, pos)} url={item.url} /> return <VideoFile onPosition={(pos) => actions.setPosition(index, pos)} url={item.url} />

View File

@ -1,31 +1,56 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState, useRef } from 'react';
import { Spin, Input } from 'antd';
import ReactResizeDetector from 'react-resize-detector'; import ReactResizeDetector from 'react-resize-detector';
import { SoundOutlined } from '@ant-design/icons'; import { PlayCircleOutlined, MinusCircleOutlined, SoundOutlined } from '@ant-design/icons';
import { AudioFileWrapper, LabelInput } from './AudioFile.styled'; import { AudioFileWrapper } from './AudioFile.styled';
export function AudioFile({ onLabel }) { import background from 'images/audio.png';
const [state, setState] = useState({ height: 0 }); export function AudioFile({ url, onLabel }) {
const updateState = (value) => { const [width, setWidth] = useState(0);
setState((s) => ({ ...s, ...value })); const [playing, setPlaying] = useState(false);
const audio = useRef(null);
const play = (on) => {
setPlaying(on);
if (on) {
audio.current.play();
}
else {
audio.current.pause();
}
} }
return ( return (
<AudioFileWrapper> <AudioFileWrapper>
<ReactResizeDetector handleWidth={false} handleHeight={true}> <ReactResizeDetector handleWidth={false} handleHeight={true}>
{({ height }) => { {({ height }) => {
if (height != state.height) { if (height != width) {
updateState({ height }); setWidth(height);
} }
return ( return <div style={{ height: '100%', width: width }} />
<div class="square" style={{ width: state.height }}>
<SoundOutlined style={{ fontSize: 32, color: '#eeeeee' }} />
<LabelInput placeholder="Label" bordered={false} onChange={(e) => onLabel(e.target.value)}/>;
</div>
)
}} }}
</ReactResizeDetector> </ReactResizeDetector>
<div class="player" style={{ width: width, height: width }}>
<div class="control">
{ playing && (
<div onClick={() => play(false)}>
<MinusCircleOutlined style={{ fontSize: 32, color: '#eeeeee', cursor: 'pointer' }} />
</div>
)}
{ !playing && (
<div onClick={() => play(true)}>
<PlayCircleOutlined style={{ fontSize: 32, color: '#eeeeee', cursor: 'pointer' }} />
</div>
)}
<audio style={{ position: 'absolute', top: 0, visibility: 'hidden' }} src={url} ref={audio} />
</div>
<div class="label">
<Input bordered={false} size="small" onChange={(e) => onLabel(e.target.value)} />
</div>
</div>
</AudioFileWrapper> </AudioFileWrapper>
) )
} }

View File

@ -1,24 +1,45 @@
import styled from 'styled-components'; import styled from 'styled-components';
import { Input } from 'antd'; import Colors from 'constants/Colors';
export const AudioFileWrapper = styled.div` export const AudioFileWrapper = styled.div`
position: relative; position: relative;
height: 100%; height: 100%;
.square { .player {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
justify-content: center;
background-color: #aaaaaa;
}
.background {
height: 100%;
object-fit: contain;
}
.label {
bottom: 0;
position: absolute;
width: 100%;
overflow: hidden;
text-align: center;
color: white;
background-color: #cccccc;
}
.control {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%; height: 100%;
display: flex; display: flex;
justify-content: center;
align-items: center; align-items: center;
background-color: #444444; justify-content: center;
} }
`; `;
export const LabelInput = styled(Input)`
position: absolute;
width: 100%;
bottom: 0;
text-align: center;
color: white;
`

View File

@ -1,75 +1,82 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState, useRef } from 'react';
import { Button } from 'antd'; import { Spin } from 'antd';
import ReactPlayer from 'react-player'
import ReactResizeDetector from 'react-resize-detector'; import ReactResizeDetector from 'react-resize-detector';
import { PlayCircleOutlined, MinusCircleOutlined, SoundOutlined } from '@ant-design/icons'; import { PlayCircleOutlined, MinusCircleOutlined, SoundOutlined } from '@ant-design/icons';
import { AudioAssetWrapper } from './AudioAsset.styled'; import { AudioAssetWrapper } from './AudioAsset.styled';
import background from 'images/audio.png';
export function AudioAsset({ label, audioUrl }) { export function AudioAsset({ label, audioUrl }) {
const [active, setActive] = useState(false); const [active, setActive] = useState(false);
const [dimension, setDimension] = useState({}); const [width, setWidth] = useState(0);
const [playing, setPlaying] = useState(true);
const [ready, setReady] = useState(false); const [ready, setReady] = useState(false);
const [playing, setPlaying] = useState(true);
const [url, setUrl] = useState(null); const [url, setUrl] = useState(null);
const audio = useRef(null);
useEffect(() => { useEffect(() => {
setActive(false); setActive(false);
setPlaying(false); setReady(false);
setPlaying(true);
setUrl(null); setUrl(null);
}, [label, audioUrl]); }, [label, audioUrl]);
const onReady = () => {
if (!ready) {
setReady(true);
setPlaying(false);
}
}
const onActivate = () => { const onActivate = () => {
setUrl(audioUrl); setUrl(audioUrl);
setActive(true); setActive(true);
} }
const Control = () => { const onReady = () => {
if (!ready) { setReady(true);
return <></> }
const play = (on) => {
setPlaying(on);
if (on) {
audio.current.play();
} }
if (playing) { else {
return ( audio.current.pause();
<div onClick={() => setPlaying(false)}>
<MinusCircleOutlined style={{ fontSize: 48, color: '#eeeeee', cursor: 'pointer' }} />
</div>
)
} }
return (
<div onClick={() => setPlaying(true)}>
<PlayCircleOutlined style={{ fontSize: 48, color: '#eeeeee', cursor: 'pointer' }} />
</div>
)
} }
return ( return (
<AudioAssetWrapper> <AudioAssetWrapper>
<ReactResizeDetector handleWidth={false} handleHeight={true}> <ReactResizeDetector handleWidth={false} handleHeight={true}>
{({ height }) => { {({ height }) => {
if (height != dimension.height) { if (height != width) {
setDimension({ height }); setWidth(height);
} }
return <div style={{ height: '100%', borderRadius: 4, width: dimension.height, backgroundColor: '#444444' }} /> return <div style={{ height: '100%', width: width }} />
}} }}
</ReactResizeDetector> </ReactResizeDetector>
<div class="player" style={{ width: dimension.height, height: dimension.height }}> <div class="player" style={{ width: width, height: width }}>
<img class="background" src={background} alt="audio background" />
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}> <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
{ !active && ( { !active && (
<div onClick={() => onActivate()}> <div class="control" onClick={() => onActivate()}>
<SoundOutlined style={{ fontSize: 32, color: '#eeeeee', cursor: 'pointer' }} /> <SoundOutlined style={{ fontSize: 32, color: '#eeeeee', cursor: 'pointer' }} />
</div> </div>
)} )}
{ active && ( { active && !ready && (
<Control /> <div class="control">
<Spin />
</div>
)} )}
<ReactPlayer style={{ position: 'absolute', top: 0, visibility: 'hidden' }} playing={playing} height="100%" width="100%" controls="true" url={url} onReady={onReady} /> { active && ready && playing && (
<div class="control" onClick={() => play(false)}>
<MinusCircleOutlined style={{ fontSize: 32, color: '#eeeeee', cursor: 'pointer' }} />
</div>
)}
{ active && ready && !playing && (
<div class="control" onClick={() => play(true)}>
<PlayCircleOutlined style={{ fontSize: 32, color: '#eeeeee', cursor: 'pointer' }} />
</div>
)}
<audio style={{ position: 'absolute', top: 0, visibility: 'hidden' }} autoplay="true"
src={url} type="audio/mpeg" ref={audio} onPlay={onReady} />
</div> </div>
</div> </div>
<div class="label">{ label }</div> <div class="label">{ label }</div>

View File

@ -1,26 +1,43 @@
import styled from 'styled-components'; import styled from 'styled-components';
import Colors from 'constants/Colors';
export const AudioAssetWrapper = styled.div` export const AudioAssetWrapper = styled.div`
position: relative; position: relative;
height: 100%; height: 100%;
.player { .player {
top: 0;
position: absolute; position: absolute;
top: 0;
left: 0;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
background-color: #aaaaaa;
}
.background {
height: 100%;
object-fit: contain;
} }
.label { .label {
bottom: 0; top: 0;
position: absolute; position: absolute;
width: 100%; width: 100%;
overflow: hidden; overflow: hidden;
text-align: center; text-align: center;
color: white; color: white;
text-overflow: ellipsis; }
white-space: nowrap;
.control {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
} }
`; `;