Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

MediaPlayer tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2010, 03:17 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default MediaPlayer tutorial

The MediaPlayer is used to play audio files.
MediaPlayer supports formats like: mp3, midi, wave and ogg. The full list is available here: Android Supported Media Formats | Android Developers

In order to play an audio file we first need to load the file:
Code:
MediaPlayer1.Load(File.DirAssets, "IsawHerStandingThere.mid")
In this case the file was added with the file manager and therefore File.DirAssets is used.

Now we can start playing the file with:
Code:
MediaPlayer1.Play
and pause the playback with:
Code:
MediaPlayer.Pause
Calling Play will resume from the same position.
Note that you can only call Pause while MediaPlayer is playing.

You can call MediaPlayer.Load at any point (after initialization), and load a new file.
MediaPlayer should be declared in Process_Globals otherwise a new instance will be created each time the activity is recreated.

The program attached is a small program that allows the user to see the playback progress and to change the position.



This is the code:
Code:
Sub Process_Globals
    
Dim MediaPlayer1 As MediaPlayer
    
Dim timer1 As Timer
End Sub

Sub Globals
    
Dim barPosition As SeekBar
    
Dim barVolume As SeekBar
    
Dim lblPosition As Label
    
Dim Looping As ToggleButton
End Sub

Sub Activity_Create(FirstTime As Boolean
    
If FirstTime Then
        MediaPlayer1.Initialize( )
        MediaPlayer1.Load(
File.DirAssets, "IsawHerStandingThere.mid")
        Timer1.Initialize(
"timer1"1000)
    
End If
    Activity.LoadLayout(
"1")
    Looping_CheckedChange(Looping.Checked) 
'set the default value
End Sub

Sub Activity_Resume
    MediaPlayer1.Play
    timer1.Enabled = 
True
    timer1_Tick 
'don't wait one second for the UI to update.
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
If MediaPlayer1.IsPlaying Then MediaPlayer1.Pause
    timer1.Enabled = 
False
End Sub

Sub timer1_Tick
    
If MediaPlayer1.IsPlaying Then
        barPosition.Value = MediaPlayer1.Position / MediaPlayer1.Duration * 
100
        lblPosition.Text = 
"Position: " & ConvertToTimeFormat(MediaPlayer1.Position) & _
            
" (" & ConvertToTimeFormat(MediaPlayer1.Duration) & ")"
    
End If
End Sub
'converts milliseconds to m:ss format.
Sub ConvertToTimeFormat(ms As IntAs String
    
Dim seconds, minutes As Int
    seconds = 
Round(ms / 1000)
    minutes = 
Floor(seconds / 60)
    seconds = seconds 
Mod 60
    
Return NumberFormat(minutes, 10) & ":" & NumberFormat(seconds, 20'ex: 3:05
End Sub

Sub barVolume_ValueChanged (Value As Int, UserChanged As Boolean)
    MediaPlayer1.SetVolume(barVolume.Value / 
100, barVolume.Value / 100)
End Sub

Sub barPosition_ValueChanged (Value As Int, UserChanged As Boolean)
    
If UserChanged = False Then Return 'the value was changed programmatically
    MediaPlayer1.Position = Value / 100 * MediaPlayer1.Duration
    
If MediaPlayer1.IsPlaying = False Then 'this can happen when the playback reached the end and the user changes the position
        MediaPlayer1.Play
    
End If
    timer1_Tick 
'immediately update the progress label
End Sub

Sub Looping_CheckedChange(Checked As Boolean)
    MediaPlayer1.Looping = Checked
End Sub
We are initializing MediaPlayer only once when the application starts.
Playing starts when the activity resumes, which happens right after the Create event.

A timer is used to check the playback position every second and update the label and seek bar.
Note that the seekbar ValueChanged position includes a boolean value named UserChanged which you can use to differentiate between changes done by the user (by dragging the thumb) and changes done programmatically.

The Looping property determines whether playback will restart automatically when it reaches the end.
Attached Files
File Type: zip MediaPlayer.zip (9.5 KB, 648 views)
Reply With Quote
  #2 (permalink)  
Old 11-14-2010, 04:34 PM
Basic4ppc Expert
 
Join Date: May 2008
Location: Berkshire, UK
Posts: 810
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Quote:
Originally Posted by Erel View Post
Note that the seekbar ValueChanged position includes a boolean value named UserChanged which you can use to differentiate between changes done by the user (by dragging the thumb) and changes done programmatically.
Aha; that would have saved all those boolean flags I had to invent for the same purpose in Basic4PPC programs!

Mike.
Reply With Quote
  #3 (permalink)  
Old 11-14-2010, 04:59 PM
Basic4ppc Expert
 
Join Date: May 2008
Location: Italy
Posts: 599
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Wow Eril - you are really spoiling us with goodies

I actually feel guilty asking you this after all things you have managed to include in B4A so far, but I am wondering if you are going to extend later on the Media-Library with video-support as well?
__________________
rgds,
moster67
Reply With Quote
  #4 (permalink)  
Old 11-14-2010, 05:51 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Thanks. I believe that the video functionality will be added in the future.
Reply With Quote
  #5 (permalink)  
Old 11-21-2010, 06:37 AM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

Can this sample play a mp3 file on server (such as www.mydomain.com/abc.mp3) ?
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #6 (permalink)  
Old 11-21-2010, 07:03 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

For now you will need to first download the file and then play it locally.
Reply With Quote
  #7 (permalink)  
Old 11-21-2010, 08:12 AM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

I hope B4A will support it. Thanks Erel.
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #8 (permalink)  
Old 12-08-2010, 11:55 AM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 978
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default Duration

When playing mp3 files the duration is about 6.7 times the correct duration, I haven't tested other types yet.

edit: Now I'm more confused because I found that for that album I have the same problem with the PPC player, and for other albums there is no problem.

I guess it needs more checking... maybe there are differences in the mp3 files based on the compression level ?
__________________
David Erez
Ramat Hasharon, Israel

Last edited by derez : 12-08-2010 at 12:15 PM.
Reply With Quote
  #9 (permalink)  
Old 12-08-2010, 04:20 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Maybe there is a problem with the meta data stored in this specific album.
Reply With Quote
  #10 (permalink)  
Old 12-15-2010, 02:39 PM
Basic4ppc Veteran
 
Join Date: Feb 2008
Posts: 274
Default

So is MediaPlayer the part of Android ? Not an application that can be removed from rooted device wishing to have better player (and application using it hangs)?
__________________
LG P500, Android v.2.3
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
ListView tutorial Erel Basic4android Getting started & Tutorials 55 05-17-2012 09:58 PM
SQL Tutorial Erel Tutorials 32 02-07-2011 09:14 AM
Graphics tutorial Erel Tutorials 2 12-23-2009 06:43 AM
New graphics tutorial Erel Announcements 1 12-11-2009 02:56 PM
New GPS tutorial Erel Announcements 2 11-05-2007 11:39 AM


All times are GMT. The time now is 10:17 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0