I have an mp3 audio file located under this link:
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
How can I get its duration in my flutter app without getting the whole audio ?
Just it's length duration? I want the quickest possible way to get it.
You could go with the just_audio package.
import 'package:just_audio/just_audio.dart';
final player = AudioPlayer(); // Create a player
final duration = await player.setUrl( // Load a URL
'https://example.com/bar.mp3'); // Schemes: (https: | file: | asset: )
Related
I am using the Flutter video_player package here: https://pub.dev/packages/video_player
How do I get the duration of the video? I can see there is a position property, so I would need that to get the current value in time.
But how do I get the total duration of the video? The video is from a URL.
I am making a custom player so I need these two values.
For getting the Total duration you can use the video controller
VideoPlayerController _controller = VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
});
Duration durationOfVideo = _controller.value.duration;
You can also directly store the integer instead of duration as below image
You can create a function to calculate the video Dutarion and format it.
getVideoPosition() {
var duration = Duration(milliseconds: videoController.value.position.inMilliseconds.round());
return [duration.inMinutes, duration.inSeconds].map((seg) => seg.remainder(60).toString().padLeft(2, '0')).join(':');
}
After craeate this function, you just need to call it to show the duration.
Ex.:
Text(getVideoPosition())
When I get an unity audio clip from firebase by url
var request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
yield return request.SendWebRequest();
var clip = DownloadHandlerAudioClip.GetContent(request);
print("success..." + clip.length);
audio clip real length is 4.86, but after download clip.length is 8.1
my upload audioclip used unity micphone record , and use "SavWav" Convert to mp3 data,
download audioclip length is a unity bug bug not fixed ,
i use SavWav.TrimSilence(clip, 0, (clip_)=>{...} get the correct length
Hope that helps
I'm trying to create an app generating a continuos sinewave of various frequency (controlled by the user) and I'm trying to play the data as it's generated in real time.
I'm using just_audio right now to play bytes generated using wave_generator, as follows (snippet from issue):
class BufferAudioSource extends StreamAudioSource {
final Uint8List _buffer;
BufferAudioSource(this._buffer) : super(tag: "Bla");
#override
Future<StreamAudioResponse> request([int? start, int? end]) {
start = start ?? 0;
end = end ?? _buffer.length;
return Future.value(
StreamAudioResponse(
sourceLength: _buffer.length,
contentLength: end - start,
offset: start,
contentType: 'audio/wav',
stream: Stream.value(List<int>.from(_buffer.skip(start).take(end - start))),
),
);
}
}
And I'm using the audio source like this:
StreamAudioSource _source = BufferAudioSource(_data!);
_player.setAudioSource(_source);
_player.play()
Is there a way I could feed the data to the player as soon as I generate it on the fly, using a sinewave generator, so that if the user changes the frequency, the playback will reflect the change as soon as it happens?
I tried looking online and on the repository github but I couldn't find anything.
What is the best option to:
Record audio from microphone,
Store the audio as files in memory,
Being able to play those files ?
Is there one package that is convenient to record and play? Does it works on all platforms (web compatible)? What is the best strategy to store them in memory?
Here is a package you can use audio_recorder
For record and storing part here is sample examples (read the package documentation)
// Import package
import 'package:audio_recorder/audio_recorder.dart';
// Check permissions before starting
bool hasPermissions = await AudioRecorder.hasPermissions;
// Get the state of the recorder
bool isRecording = await AudioRecorder.isRecording;
// Start recording
await AudioRecorder.start(path: _controller.text, audioOutputFormat: AudioOutputFormat.AAC);
// Stop recording
Recording recording = await AudioRecorder.stop();
print("Path : ${recording.path}, Format : ${recording.audioOutputFormat}, Duration : ${recording.duration}, Extension : ${recording.extension},");
play audio you need another package i suggest audioplayers :
// To pause
int result = await audioPlayer.pause();
//To Stop
int result = await audioPlayer.stop();
// To Jump through
int result = await audioPlayer.seek(Duration(milliseconds: 1200));
// To Resume
int result = await audioPlayer.resume();
I am trying to play an audio by using package audioplayer but I cannot always know if a path is a local file or not. Therefore, I wonder what is the best way to determine if a path is local in Flutter?
Both File and Directory classes of dart:io library has isAbsolute method, which checks if a File or Directory path is absolute or not.
final file = File('foo/bar');
print(file.isAbsolute);
/// check if a path is local path in OS filesystem JUST in syntax not really
// final bool isAbsolutePath = !_imagePath!.contains('http://');
// final bool isAbsolutePath = File(_imagePath!).isAbsolute; // import 'dart:io';
final bool isAbsolutePath = p.isAbsolute(_imagePath!); // import 'package:path/path.dart' as p;