Skip to content

Commit a16c9a8

Browse files
🐛 Fix path with space doesn't doesn't show waveform
1 parent 0f683d2 commit a16c9a8

File tree

6 files changed

+61
-65
lines changed

6 files changed

+61
-65
lines changed

android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.simform.audio_waveforms
22

33
import android.content.Context
4-
import android.os.Build
4+
import android.net.Uri
55
import android.os.Handler
66
import android.os.Looper
7-
import androidx.annotation.RequiresApi
87
import com.google.android.exoplayer2.ExoPlayer
98
import com.google.android.exoplayer2.MediaItem
109
import com.google.android.exoplayer2.Player
@@ -35,30 +34,23 @@ class AudioPlayer(
3534
) {
3635
if (path != null) {
3736
this.noOfSamples = noOfSamples ?: 100
38-
try {
39-
waveformExtractor = WaveformExtractor(
40-
path = path,
41-
expectedPoints = this.noOfSamples,
42-
key = key,
43-
methodChannel = methodChannel,
44-
result = result,
45-
object : ExtractorCallBack {
46-
override fun onProgress(value: Float) {
47-
if (value == 1.0F) {
48-
result.success(waveformExtractor?.sampleData)
49-
}
37+
waveformExtractor = WaveformExtractor(
38+
path = path,
39+
expectedPoints = this.noOfSamples,
40+
key = key,
41+
methodChannel = methodChannel,
42+
context = appContext,
43+
result = result,
44+
extractorCallBack = object : ExtractorCallBack {
45+
override fun onProgress(value: Float) {
46+
if (value == 1.0F) {
47+
result.success(waveformExtractor?.sampleData)
5048
}
5149
}
52-
)
53-
waveformExtractor?.startDecode()
54-
waveformExtractor?.stop()
55-
} catch (e: Exception) {
56-
result.error(
57-
Constants.LOG_TAG,
58-
"Can not extract waveform data from provided audio file path",
59-
e.toString()
60-
)
61-
}
50+
}
51+
)
52+
waveformExtractor?.startDecode()
53+
waveformExtractor?.stop()
6254

6355
}
6456
}
@@ -68,10 +60,9 @@ class AudioPlayer(
6860
path: String?,
6961
volume: Float?
7062
) {
71-
72-
//TODO: meta data of song
7363
if (path != null) {
74-
val mediaItem = MediaItem.fromUri(path)
64+
val uri = Uri.parse(path)
65+
val mediaItem = MediaItem.fromUri(uri)
7566
player = ExoPlayer.Builder(appContext).build()
7667
player?.addMediaItem(mediaItem)
7768
player?.prepare()

android/src/main/kotlin/com/simform/audio_waveforms/WaveformExtractor.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.simform.audio_waveforms
22

3+
import android.content.Context
34
import android.media.AudioFormat
45
import android.media.MediaCodec
56
import android.media.MediaExtractor
67
import android.media.MediaFormat
8+
import android.net.Uri
79
import android.os.Build
810
import android.os.Handler
911
import android.os.Looper
@@ -19,8 +21,9 @@ class WaveformExtractor(
1921
private val key: String,
2022
private val methodChannel: MethodChannel,
2123
private val result: MethodChannel.Result,
22-
private val extractorCallBack: ExtractorCallBack
23-
) {
24+
private val extractorCallBack: ExtractorCallBack,
25+
private val context: Context,
26+
) {
2427
private val handler = Handler(Looper.getMainLooper())
2528
private var decoder: MediaCodec? = null
2629
private var extractor: MediaExtractor? = null
@@ -41,7 +44,8 @@ class WaveformExtractor(
4144
private fun getFormat(path: String): MediaFormat? {
4245
val mediaExtractor = MediaExtractor()
4346
this.extractor = mediaExtractor
44-
mediaExtractor.setDataSource(path)
47+
val uri = Uri.parse(path)
48+
mediaExtractor.setDataSource(context,uri,null)
4549
val trackCount = mediaExtractor.trackCount
4650
repeat(trackCount) {
4751
val format = mediaExtractor.getTrackFormat(it)
@@ -233,12 +237,6 @@ class WaveformExtractor(
233237
extractor?.release()
234238
finishCount.countDown()
235239
}
236-
237-
fun cancel() {
238-
if (!started) return
239-
handler.post { stop() }
240-
finishCount.await()
241-
}
242240
}
243241

244242
fun MediaCodec.BufferInfo.isEof() = flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0

example/lib/chat_bubble.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,24 @@ class _WaveBubbleState extends State<WaveBubble> {
111111
);
112112
// Extracting waveform separately if index is odd.
113113
if (widget.index?.isOdd ?? false) {
114-
controller.extractWaveformData(
115-
path: widget.path ?? file!.path,
116-
noOfSamples: playerWaveStyle.getSamplesForWidth(widget.width ?? 200),
117-
);
114+
controller
115+
.extractWaveformData(
116+
path: widget.path ?? file!.path,
117+
noOfSamples:
118+
playerWaveStyle.getSamplesForWidth(widget.width ?? 200),
119+
)
120+
.then((waveformData) => debugPrint(waveformData.toString()));
118121
}
119122
}
120123

121124
@override
122125
void dispose() {
123-
super.dispose();
124126
playerStateSubscription.cancel();
125127
if (widget.isLastWidget) {
126128
controller.stopAllPlayers();
127129
}
128130
controller.dispose();
131+
super.dispose();
129132
}
130133

131134
@override

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ class _HomeState extends State<Home> with WidgetsBindingObserver {
220220

221221
if (path != null) {
222222
isRecordingCompleted = true;
223-
debugPrint("Recorded file size: ${File(path).lengthSync()}");
224223
debugPrint(path);
224+
debugPrint("Recorded file size: ${File(path).lengthSync()}");
225225
}
226226
} else {
227-
await recorderController.record();
227+
await recorderController.record(path: path!);
228228
}
229229
} catch (e) {
230230
debugPrint(e.toString());

ios/Classes/AudioPlayer.swift

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
1717
self.playerKey = playerKey
1818
flutterChannel = channel
1919
}
20-
20+
2121
func extractWaveformData(path: String?, result: @escaping FlutterResult, noOfSamples: Int?) {
2222
if(!(path ?? "").isEmpty) {
2323
do {
24-
let audioUrl = URL.init(fileURLWithPath: path!)
25-
waveformExtractor = try WaveformExtractor(url: audioUrl, flutterResult: result, channel: flutterChannel)
24+
let audioUrl = URL.init(string: path!)
25+
if(audioUrl == nil){
26+
result(FlutterError(code: Constants.audioWaveforms, message: "Failed to initialise Url from provided audio file", details: "If path contains `file://` try removing it"))
27+
return
28+
}
29+
waveformExtractor = try WaveformExtractor(url: audioUrl!, flutterResult: result, channel: flutterChannel)
2630
if(waveformExtractor != nil) {
2731
let data = waveformExtractor!.extractWaveform(samplesPerPixel: noOfSamples, playerKey: playerKey)
2832
waveformExtractor!.cancel()
@@ -32,31 +36,33 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
3236
}
3337
}
3438
} catch {
35-
result(FlutterError(code: Constants.audioWaveforms, message: "Failded to decode audio file", details: nil))
39+
result(FlutterError(code: Constants.audioWaveforms, message: "Failed to decode audio file", details: nil))
3640
}
3741
} else {
3842
result(FlutterError(code: Constants.audioWaveforms, message: "Audio file path can't be empty or null", details: nil))
3943
}
4044
}
41-
45+
4246
func preparePlayer(path: String?, volume: Double?, result: @escaping FlutterResult) {
4347
if(!(path ?? "").isEmpty) {
44-
let audioUrl = URL.init(fileURLWithPath: path!)
48+
let audioUrl = URL.init(string: path!)
49+
if(audioUrl == nil){
50+
result(FlutterError(code: Constants.audioWaveforms, message: "Failed to initialise Url from provided audio file", details: "If path contains `file://` try removing it"))
51+
return
52+
}
4553
do {
46-
player = try AVAudioPlayer(contentsOf: audioUrl)
54+
player = try AVAudioPlayer(contentsOf: audioUrl!)
4755
} catch {
48-
result(FlutterError(code: "", message: "Failed to prepare recording", details: nil))
56+
result(FlutterError(code: Constants.audioWaveforms, message: "Failed to prepare player", details: nil))
4957
}
50-
5158
player?.prepareToPlay()
5259
player?.volume = Float(volume ?? 1.0)
5360
result(true)
5461
} else {
5562
result(FlutterError(code: Constants.audioWaveforms, message: "Audio file path can't be empty or null", details: nil))
5663
}
57-
5864
}
59-
65+
6066
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
6167
successfully flag: Bool) {
6268
var finishType = 2
@@ -76,9 +82,9 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
7682
finishType = 2
7783
}
7884
plugin.flutterChannel.invokeMethod(Constants.onDidFinishPlayingAudio, arguments: [Constants.finishType: finishType, Constants.playerKey: playerKey])
79-
85+
8086
}
81-
87+
8288
func startPlyer(result: @escaping FlutterResult, finishMode: Int?) {
8389
if(finishMode != nil && finishMode == 0) {
8490
self.finishMode = FinishMode.loop
@@ -92,22 +98,21 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
9298
startListening()
9399
result(true)
94100
}
95-
101+
96102
func pausePlayer(result: @escaping FlutterResult) {
97103
stopListening()
98104
player?.pause()
99105
result(true)
100106
}
101-
107+
102108
func stopPlayer(result: @escaping FlutterResult) {
103109
stopListening()
104110
player?.stop()
105111
player = nil
106112
timer = nil
107113
result(true)
108114
}
109-
110-
115+
111116
func getDuration(_ type: DurationType, _ result: @escaping FlutterResult) throws {
112117
if type == .Current {
113118
let ms = (player?.currentTime ?? 0) * 1000
@@ -117,12 +122,12 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
117122
result(Int(ms))
118123
}
119124
}
120-
125+
121126
func setVolume(_ volume: Double?, _ result: @escaping FlutterResult) {
122127
player?.volume = Float(volume ?? 1.0)
123128
result(true)
124129
}
125-
130+
126131
func seekTo(_ time: Int?, _ result: @escaping FlutterResult) {
127132
if(time != nil) {
128133
player?.currentTime = Double(time! / 1000)
@@ -131,7 +136,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
131136
result(false)
132137
}
133138
}
134-
139+
135140
func startListening() {
136141
if #available(iOS 10.0, *) {
137142
timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: { _ in
@@ -142,7 +147,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
142147
// Fallback on earlier versions
143148
}
144149
}
145-
150+
146151
func stopListening() {
147152
timer?.invalidate()
148153
timer = nil

lib/src/controllers/player_controller.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ class PlayerController extends ChangeNotifier {
147147
required String path,
148148
int noOfSamples = 100,
149149
}) async {
150-
path = Uri.parse(path).path;
151150
final result = await AudioWaveformsInterface.instance.extractWaveformData(
152151
key: playerKey,
153152
path: path,

0 commit comments

Comments
 (0)