Introducing Android Media Player

2,856 views
3,014 views

Published on

Android Media Player on jogja android apps developer with Melon Music Player @ Jogja Digital Valley Yogyakarta. 17 Januari 2014

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,856
On SlideShare
0
From Embeds
0
Number of Embeds
869
Actions
Shares
0
Downloads
157
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • public class MyService extends Service implements MediaPlayer.OnPreparedListener {
    private static final ACTION_PLAY = "com.example.action.PLAY";
    MediaPlayer mMediaPlayer = null;
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
    mMediaPlayer = ... // initialize it here
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    }
    /** Called when MediaPlayer is ready */
    public void onPrepared(MediaPlayer player) {
    player.start();
    }
    }
  • Introducing Android Media Player

    1. 1. Android Media Player Presented By ARIF AKBARUL HUDA
    2. 2. Arif Akbarul Huda Penulis Mobile Apps Developer (android) Trainer @omayib 75278432 Facebook.com/omayib omayib@gmail.com omayib@gmail.com arul_sip@yahoo.com
    3. 3. My creation.... Available on book store Coming soon beginning 2014
    4. 4. Jogja.apk
    5. 5. Media Player Media Player
    6. 6. Media Player
    7. 7. Media Player Local resources vs internal URI vs eksternal URL
    8. 8. Some problem.... Some problem....
    9. 9. Some problem.... Low Memory
    10. 10. Some problem.... ERROR HANDLING
    11. 11. Media Player About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service Streaming • RTSP (RTP, SDP) • HTTP/HTTPS progressive streaming • HTTP/HTTPS live streaming draft protocol: • MPEG-2 TS media files only • Protocol version 3 (Android 4.0 and above) • Protocol version 2 (Android 3.x) • Not supported before Android 3.0 Resources .3gp.mp4, .m4a,.aac,.mp3,.mid, .xmf, .mxmf,.rtttl, .rtx,.wav, ….
    12. 12. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service MediaPlayer Audio Manager
    13. 13. MediaPlayer About media player Support file Basic Mediaplayer MediaPlayer class can be used to control playback of audio/video files and streams. Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service MediaPlayer
    14. 14. MediaPlayer About media player Support file Basic Mediaplayer AudioManager provides access to volume and ringer mode control Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service Audio Manager
    15. 15. MediaPlayer About media player Support file Basic Mediaplayer Audio manager ● Local resources ● Internal URIs, such as one you might obtain from a Using media player Content Resolver Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service ● External URLs (streaming)
    16. 16. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service never call it from your application's UI thread
    17. 17. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service
    18. 18. MediaPlayer
    19. 19. MediaPlayer
    20. 20. MediaPlayer
    21. 21. MediaPlayer
    22. 22. MediaPlayer
    23. 23. MediaPlayer
    24. 24. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation A MediaPlayer can consume valuable system resources. you should always take extra precautions to make sure you are not hanging on to a MediaPlayer instance longer than necessary. Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service mediaPlayer.release(); mediaPlayer = null;
    25. 25. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service Background service
    26. 26. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service Background service
    27. 27. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player On synchronous operations, errors would normally be signaled with an exception or an error code, but whenever you use asynchronous resources, you should make sure your application is notified of errors appropriately Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service public class MyService extends Service implements  MediaPlayer.OnErrorListener {     MediaPlayer mMediaPlayer;     public void initMediaPlayer() {         // ...initialize the MediaPlayer here...         mMediaPlayer.setOnErrorListener(this);     }     @Override     public boolean onError(MediaPlayer mp, int what, int extra) {         // ... react appropriately ...         // The MediaPlayer has moved to the Error state, must be reset!     } } ERROR HANDLING
    28. 28. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a foreground service When designing applications that play media in the background, the device may go Android system tries to conserve battery while the device is sleeping, the system tries to shut off any of the phone's features that are not to sleep while your service is running. Because the necessary, including the CPU and the WiFi hardware. However, if your service is playing or streaming music, you want to prevent the system from interfering with your playback. mMediaPlayer = new MediaPlayer(); // ... other initialization here ... mMediaPlayer.setWakeMode(getApplicationContext(),  PowerManager.PARTIAL_WAKE_LOCK);
    29. 29. MediaPlayer About media player Support file Basic Mediaplayer Audio manager Using media player Async preparation Manage state release Background media player Running asynchronously Handling asynchronous errors Using wake locks Running as a String songName; // assign the song name to songName PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,                 new Intent(getApplicationContext(), MainActivity.class),                 PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification(); notification.tickerText = text; ... startForeground(NOTIFICATION_ID, notification); foreground service stopForeground(true);

    ×