• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
  • Tim Holloway
  • Carey Brown
  • salvin francis

Media Player Android Sample

Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This tutorial will help you how to create a simple Audio Player application.Normally,when you make a player application based on Activity, it will stop when you exit the application. So, now you will know about a technique using Service to perform playing sound. You can play music when your application is closed.

1. MediaPlayer class

MediaPlayer class can be used to control playback of audio/video files and streams. And here is some method is used to control a mediaplayer object.

mp.reset():Resets the MediaPlayer to its uninitialized state.
setDataSource(String path): Sets the data source (file-path or http/rtsp URL) to use.
mp.prepare():Prepares the player for playback, synchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.
mp.start():Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.
mp.getDuration():Gets the duration of the file(Return the duration in milliseconds)
mp.isPlaying():Checks whether the MediaPlayer is playing.
mp.stop():Stops playback after playback has been stopped or paused.
mp.release():Releases resources associated with this MediaPlayer object.

2. Service class

Service class: A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package’s AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().

3. SongProvider class

Download Sample Source code

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great App.

Could you please help me getting this app run audio files from raw/assets folders instead of sdcard ?

thanks in advance.
No holds barred. And no bars holed. Except this tiny ad:
professionally read, modify and write PDF files from Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!