Used to play sounds, designed to use with medium sized sounds or streams

The Android MediaPlayer has a complex state diagram that you'll need to respect if you want your MediaPlayer to work fine, see the android doc

Introduced properties

fun both_volume(left_volume: Float, right_volume: Float)

android :: MediaPlayer :: both_volume

Sets the left volume and the right volume of this player
fun data_source(path: String): Music

android :: MediaPlayer :: data_source

Sets the datasource (file-path or http/rtsp URL) to use
fun data_source_fd(fd: NativeAssetFileDescriptor): Music

android :: MediaPlayer :: data_source_fd

Sets the data source (NativeFileDescriptor) to use
fun destroy

android :: MediaPlayer :: destroy

Releases the resources associated with this MediaPlayer
fun error: nullable Error

android :: MediaPlayer :: error

Error gestion
protected fun error=(error: nullable Error)

android :: MediaPlayer :: error=

Error gestion
init from_id(context: NativeActivity, id: Int)

android :: MediaPlayer :: from_id

Init the mediaplayer with a sound resource id
fun is_prepared=(is_prepared: Bool)

android :: MediaPlayer :: is_prepared=

Used to control the state of the mediaplayer
fun load_sound(id: Int, context: NativeActivity): Music

android :: MediaPlayer :: load_sound

Load a sound for a given resource id
fun looping: Bool

android :: MediaPlayer :: looping

Checks whether the MediaPlayer is looping or non-looping
fun looping=(b: Bool)

android :: MediaPlayer :: looping=

Sets the player to be looping or non-looping
fun pause

android :: MediaPlayer :: pause

Pauses playback
fun playing: Bool

android :: MediaPlayer :: playing

Checks whether the mediaplayer is playing
fun prepare

android :: MediaPlayer :: prepare

Prepares the player for playback, synchronously
fun reset

android :: MediaPlayer :: reset

Reset MediaPlayer to its initial state
fun sound: nullable Music

android :: MediaPlayer :: sound

The sound associated with this mediaplayer
fun sound=(sound: nullable Music)

android :: MediaPlayer :: sound=

The sound associated with this mediaplayer
fun start

android :: MediaPlayer :: start

Starts or resumes playback
fun stop

android :: MediaPlayer :: stop

Stops playback after playback has been stopped or paused
fun stream_type=(stream_type: Int)

android :: MediaPlayer :: stream_type=

Sets the audio stream type for this media player
fun volume=(volume: Float)

android :: MediaPlayer :: volume=

Sets the volume on this player

Redefined properties

redef type SELF: MediaPlayer

android $ MediaPlayer :: SELF

Type of this instance, automatically specialized in every class
redef init init

android $ MediaPlayer :: init

Create a new MediaPlayer, but no sound is attached, you'll need

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun both_volume(left_volume: Float, right_volume: Float)

android :: MediaPlayer :: both_volume

Sets the left volume and the right volume of this player
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun data_source(path: String): Music

android :: MediaPlayer :: data_source

Sets the datasource (file-path or http/rtsp URL) to use
fun data_source_fd(fd: NativeAssetFileDescriptor): Music

android :: MediaPlayer :: data_source_fd

Sets the data source (NativeFileDescriptor) to use
fun destroy

android :: MediaPlayer :: destroy

Releases the resources associated with this MediaPlayer
fun error: nullable Error

android :: MediaPlayer :: error

Error gestion
protected fun error=(error: nullable Error)

android :: MediaPlayer :: error=

Error gestion
init from_id(context: NativeActivity, id: Int)

android :: MediaPlayer :: from_id

Init the mediaplayer with a sound resource id
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun is_prepared=(is_prepared: Bool)

android :: MediaPlayer :: is_prepared=

Used to control the state of the mediaplayer
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun load_sound(id: Int, context: NativeActivity): Music

android :: MediaPlayer :: load_sound

Load a sound for a given resource id
fun looping: Bool

android :: MediaPlayer :: looping

Checks whether the MediaPlayer is looping or non-looping
fun looping=(b: Bool)

android :: MediaPlayer :: looping=

Sets the player to be looping or non-looping
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun pause

android :: MediaPlayer :: pause

Pauses playback
fun playing: Bool

android :: MediaPlayer :: playing

Checks whether the mediaplayer is playing
fun prepare

android :: MediaPlayer :: prepare

Prepares the player for playback, synchronously
fun reset

android :: MediaPlayer :: reset

Reset MediaPlayer to its initial state
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun sound: nullable Music

android :: MediaPlayer :: sound

The sound associated with this mediaplayer
fun sound=(sound: nullable Music)

android :: MediaPlayer :: sound=

The sound associated with this mediaplayer
fun start

android :: MediaPlayer :: start

Starts or resumes playback
fun stop

android :: MediaPlayer :: stop

Stops playback after playback has been stopped or paused
fun stream_type=(stream_type: Int)

android :: MediaPlayer :: stream_type=

Sets the audio stream type for this media player
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun volume=(volume: Float)

android :: MediaPlayer :: volume=

Sets the volume on this player
package_diagram android::MediaPlayer MediaPlayer core::Object Object android::MediaPlayer->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

android $ MediaPlayer
# Used to play sounds, designed to use with medium sized sounds or streams
# The Android MediaPlayer has a complex state diagram that you'll need to
# respect if you want your MediaPlayer to work fine, see the android doc
class MediaPlayer
	private var nmedia_player: NativeMediaPlayer is noinit

	# Used to control the state of the mediaplayer
	private var is_prepared = false is writable

	# The sound associated with this mediaplayer
	var sound: nullable Music = null is writable

	# Error gestion
	var error: nullable Error = null

	# Create a new MediaPlayer, but no sound is attached, you'll need
	# to use `load_sound` before using it
	init do self.nmedia_player = (new NativeMediaPlayer).new_global_ref

	# Init the mediaplayer with a sound resource id
	init from_id(context: NativeActivity, id: Int) do
		self.nmedia_player = new NativeMediaPlayer.create(context, id)
		if self.nmedia_player.is_java_null then
			self.error = new Error("Failed to create the MediaPlayer")
			self.sound = new Music.priv_init(id, self, self.error)
		end
		self.sound = new Music.priv_init(id, self, null)
	end

	# Load a sound for a given resource id
	fun load_sound(id: Int, context: NativeActivity): Music do
		# FIXME: maybe find a better way to handle this situation
		# If two different music are loaded with the same `MediaPlayer`,
		# a new `NativeMediaPlayer` will be created for the secondd music
		# and the nit program will loose the handle to the previous one
		# If the previous music is playing, we need to stop it
		if playing then
			stop
			reset
			destroy
		end

		self.nmedia_player = new NativeMediaPlayer.create(context, id)
		if self.nmedia_player.is_java_null then
			self.error = new Error("Failed to load a sound")
			self.sound = new Music.priv_init(id, self, new Error("Sound loading failed"))
			return self.sound.as(not null)
		else
			if self.error != null then self.error = null
			self.sound = new Music.priv_init(id, self, null)
			self.is_prepared = true
			return self.sound.as(not null)
		end
	end

	# Starts or resumes playback
	# REQUIRE `self.sound != null`
	fun start do
		if self.error != null then return
		if not is_prepared then prepare
		nmedia_player.start
	end

	# Stops playback after playback has been stopped or paused
	# REQUIRE `self.sound != null`
	fun stop do
		if self.error != null then return
		is_prepared = false
		nmedia_player.stop
	end

	# Prepares the player for playback, synchronously
	# REQUIRE `self.sound != null`
	fun prepare do
		if self.error != null then return
		assert sound != null
		nmedia_player.prepare
		is_prepared = true
	end

	# Pauses playback
	# REQUIRE `self.sound != null`
	fun pause do
		if self.error != null then return
		assert sound != null
		nmedia_player.pause
	end

	# Checks whether the mediaplayer is playing
	fun playing: Bool do return nmedia_player.playing

	# Releases the resources associated with this MediaPlayer
	fun destroy do
		nmedia_player.release
		self.sound = null
	end

	# Reset MediaPlayer to its initial state
	fun reset do nmedia_player.reset

	# Sets the datasource (file-path or http/rtsp URL) to use
	fun data_source(path: String): Music do
		sys.jni_env.push_local_frame(1)
		var retval = nmedia_player.data_source_path(path.to_java_string)
		sys.jni_env.pop_local_frame
		if retval == 0 then
			self.error = new Error("could not load the sound " + path)
			self.sound = new Music.priv_init(null, self, self.error)

		else
			self.sound = new Music.priv_init(null, self, null)
		end
		return self.sound.as(not null)
	end
	# Sets the data source (NativeFileDescriptor) to use
	fun data_source_fd(fd: NativeAssetFileDescriptor): Music do
		if not fd.is_java_null then
			if nmedia_player.data_source_fd(fd.file_descriptor, fd.start_offset, fd.length) == 0 then
				self.error = new Error("could not load the sound")
				self.sound = new Music.priv_init(null, self, self.error)
			else
				self.sound = new Music.priv_init(null, self, null)
			end
			return self.sound.as(not null)
		else
			var error = new Error("could not load the sound")
			return new Music.priv_init(null, self, error)
		end
	end

	# Checks whether the MediaPlayer is looping or non-looping
	fun looping: Bool do return nmedia_player.looping

	# Sets the player to be looping or non-looping
	fun looping=(b: Bool) do nmedia_player.looping = b

	# Sets the volume on this player
	fun volume=(volume: Float) do nmedia_player.volume = volume

	# Sets the left volume and the right volume of this player
	fun both_volume(left_volume, right_volume: Float) do nmedia_player.both_volume(left_volume, right_volume)

	# Sets the audio stream type for this media player
	fun stream_type=(stream_type: Int) do nmedia_player.stream_type = stream_type
end
lib/android/audio.nit:364,1--508,3