Namespaces
Variants
Actions
(Redirected from How to play a sound file)

Archived:How to play a sound file in PySymbian

Jump to: navigation, search
Archived.png
Aquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.
Article Metadata

Tested with
Devices(s): Nokia N95, Nokia E90

Compatibility
Platform(s): S60 1st Edition, S60 2nd Edition, S60 3rd Edition

Article
Keywords: audio
Created: cyke64 (15 Mar 2007)
Last edited: hamishwillee (08 May 2013)

Contents

Overview

This snippet shows how to play an audio file in Python and presents some of the options available.

Preconditions

Note: The method current_volume is not available for S60 1st Edition.

Valid file formats must be used. Their availability may vary from one device generation to another and they tipically include WAV, AMR, MP3, AAC, RA.

The optional parameter that is passed to the play() method of a Sound object represents the number of times the file will be played (1 by default). It can be an integer or the KMdaRepeatForever data item, which is for continous playback.

Source code

import appuifw, e32, audio
 
 
app_lock = e32.Ao_lock()
#Define the exit function
def quit():
#Close the Sound object
s.close()
app_lock.signal()
appuifw.app.exit_key_handler = quit
 
file_path = u"C:\\Data\\Sounds\\Digital\\mysound.mp3"
#The path of the file that is to be played
#Open the sound file
s = audio.Sound.open(file_path)
#Play it indefinitely
s.play(audio.KMdaRepeatForever)
 
#Wait for the user to request the exit
app_lock.wait()

Postconditions

The sound file is played for the specified number of times.

Known issue

Playing a Sound object in a function doesn't work because it goes out of scope when the function returns. If the Sound object goes out of scope, the sound stops playing. This will not work:

import audio
 
 
def play_sound(f):
s = audio.Sound.open(f)
s.play()
 
play_sound(u"C:\\Data\\Sounds\\Digital\\mysound.mp3")

The solution is to make the Sound object a global variable so that it stays in scope when the function returns. This will work:

import audio
 
 
def play_sound(f):
global s
s = audio.Sound.open(f)
s.play()
 
play_sound(u"C:\\Data\\Sounds\\Digital\\mysound.mp3")

Additional information

The Sound object method stop() is used to stop playback. The Sound object method duration() returns the file's duration in microseconds. The Sound object methods current_position() and set_position(microseconds) return and set the current buffer position, respectively.

The Sound object methods current_volume(), set_volume(volume) and max_volume() return and set the current volume and return the maximum volume of the device, respectively.

The Sound object method state() returns the current state of the object, which can be one of the following:

  • ENotReady

The Sound object has been constructed but no audio file is open.

  • EOpen

An audio file is open but no playing or recording operation is in progress.

  • EPlaying

An audio file is playing.

  • ERecording

An audio file is being recorded.

There is currently no method that pauses the playback of an audio file. The solution is:

  1. Use current_position() to find out the current position
  2. Use stop() to stop the playback
  3. Use set_position() to set the position to the one that was retrieved at step 1
  4. Use play() to resume playing the file as if it had been paused
This page was last modified on 8 May 2013, at 15:00.
187 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved