Sound class is not working correctly in androd.

34 views
Skip to first unread message

Jungkyu

unread,
Jan 27, 2018, 2:16:51 PM1/27/18
to kivy-...@googlegroups.com
HI.

First Im sorry for my poor english.

Im testing very simple audio app in android with kivy for making my own app.

I got the source from git which a sinior made.

Unfortunitely, the app seems not working correctly in some cases 

1. sound.length, sound.get_pos() are not working with midi file(.mid) in window, linux. (not test in android)
2. sound.length, sound.get_pos() are not working with wav file in android, but window, linux are OK.
3. mp3 file is not working in android  (this is really strange... but same code can play in wav. of course sound.length, sound.get_pos() are not working)

In my opinion, the sound class seems not fixed correctly.

I refere to follow post (https://github.com/kivy/kivy/issues/2776)

I hope someone can give me help.

next is the testing code

------------------------------------------------------------------------------------------------------------------------------------------------------------
from kivy.app import App
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.boxlayout import BoxLayout
from kivy.core.audio import SoundLoader
from kivy.properties import StringProperty, ObjectProperty, NumericProperty
from kivy.clock import Clock
from kivy.uix.slider import Slider


class AudioButton(ToggleButton):

filename = StringProperty('../hymn_audio/001.wav')
# filename = StringProperty('/storage/emulated/0/woorich/hymn_audio/001.wav')
sound = ObjectProperty(None, allownone=True)
volume = NumericProperty(1.0)
maxPos = NumericProperty(1.0)
up_event = ObjectProperty(None, allownone=True)
slider = ObjectProperty(None, allownone=True)
stopCount = NumericProperty(0)

def on_state(self, widget, value):
if value == 'down':
self.sound = SoundLoader.load(self.filename)
self.maxPos = self.sound.length
self.stopCount = 0
self.sound.play()
self.slider.value = 0.0
self.up_event = Clock.schedule_interval(self.sl_update, 1)
else:
self.up_event.cancel()
self.sound.stop()
self.slider.value = 0.0
self.sound.unload()
self.sound = None

def set_pos(self, pos):
if self.sound:
self.sound.seek(pos)

# If you make a volume slide, use this
def set_volume(self, volume):
self.volume = volume
if self.sound:
self.sound.volume = volume

def sl_update(self, dt):
self.slider.value = self.sound.get_pos()
print(self.sound.length)
print(self.sound.get_pos())
print(dt)
if self.slider.value == 0:
self.stopCount += 1
if self.stopCount >= 2:
self.state = 'normal'

def get_slider(self, slider):
self.slider = slider

class AudioSlider(Slider):
audioBtn = ObjectProperty(None, allownone=True)

def on_touch_move(self, touch):
self.audioBtn.set_pos(self.value)

def get_audioBtn(self, audioBtn):
self.audioBtn = audioBtn

class AudioBackground(BoxLayout):
audioBtn = ObjectProperty(None, allownone=True)
slider = ObjectProperty(None, allownone=True)

class Audio2App(App):

def build(self):
root = AudioBackground()
root.audioBtn.get_slider(root.slider)
return root

if __name__ == '__main__':
Audio2App().run()

---------------------------------------------------------------------------

# -*- coding: utf-8 -*-

<AudioBackground>:
audioBtn: aBtn
slider: posSldr

AudioButton:
id: aBtn
text: 'Stop and release'
size_hint_x: 0.2

Slider:
id: posSldr
orientation: "horizontal"
size_hint_x: 0.6
min: 0.0
max: aBtn.maxPos
value: 0.0
value_track: True
value_track_color: 1, 0, 0, 1
on_touch_move: aBtn.set_pos(self.value)
Label:
text: str(aBtn.stopCount)

<AudioButton>:
size_hint: 0.2,1
text_size: self.size
font_size: '12sp'
valign: 'middle'





Reply all
Reply to author
Forward
0 new messages