Introduction to h5 custom audio (problem and solution) and h5audio
For h5 activity, you need to insert audio, but you need to customize the style.
Html
Copy XML/HTML Code to clipboard
- <! -- Cur indicates the current time. max indicates the total duration. input indicates the progress bar. -->
- <Span class = 'cur'> </span> <input type = "range" min = 0 max = 100 class = 'range' value = 0> <span class = 'max '> </span>
Css
Copy the content to the clipboard using CSS Code.
- /* Progress bar */
- . Range {
- Width: 5.875rem;
- Height: 0.15rem;
- Background: #22.16e4;
- Border-radius: 0.25rem;
- -Webkit-appearance: none! Important;
- Position: absolute;
- Top: 3.55rem;
- Left: 6rem;
- }
- /* Progress slider */
- . Range:-webkit-slider-thumb {
- Width: 0.5rem;
- Height: 0.5rem;
- Background: # fff;
- Border: 1px solid # f18900;
- Cursor: pointer;
- Border-radius: 0.25rem;
- -Webkit-appearance: none! Important;
- }
Js
Copy the content to the clipboard using JavaScript Code
- // Convert seconds to format
- Function timeToStr (time ){
- Var m = 0,
- S = 0,
- _ M = '00 ',
- _ S = '00 ';
- Time = Math. floor (time %3600 );
- M = Math. floor (time/60 );
- S = Math. floor (time % 60 );
- _ S = s <10? '0' + s: s + '';
- _ M = m <10? '0' + m: m + '';
- Return _ m + ":" + _ s;
- }
- // Triggers a playback event
- $ ('. Play'). on ('click', function (){
- Var audio = document. getElementById ('ao ');
- Audio. play ();
- SetInterval (function (){
- Var t = parseInt (audio. currentTime );
- $ (". Range"). attr ({'max ': 751 });
- Metrics ('.max'0000.html (timeToStr (751 ));
- $ (". Range"). val (t );
- $ ('. Cur'). text (timeToStr (t ));
- },1000 );
- });
- // Listen to the slider, which can be dragged
- $ (". Range"). on ('change', function (){
- Document. getElementById ('ao'). currentTime = this. value; $ (". range"). val (this. value );
- });
The above can basically achieve custom audio playback, but there is a problem when dragging the progress bar, the computer is OK, but on the mobile phone can be dragged, however, the total duration of the audio is several minutes less than that of normal playback, resulting in inaccurate playback after the drag progress. The test shows that the duration obtained on the mobile phone is different from that obtained on the computer, resulting in inaccurate playback position after sliding. The reason is that the uploaded audio is compressed by me, so the duration obtained on the mobile phone is different from the normal one. Therefore, after the audio is compressed, its duration on the mobile phone will change (not on the computer). Pay attention to it later. If there is anything that can be used to compress the audio or get the normal duration method on the mobile phone, please tell me, haha.
The h5 custom audio (Problem and Solution) is a part of the Content shared by Alibaba Cloud. I hope you can give us a reference and support for our guests.