|
| 1 | +// Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package org.chromium.media; |
| 6 | + |
| 7 | +import android.content.Context; |
| 8 | +import android.media.MediaPlayer; |
| 9 | +import android.media.MediaPlayer.TrackInfo; |
| 10 | +import android.net.Uri; |
| 11 | +import android.view.Surface; |
| 12 | + |
| 13 | +import org.chromium.base.Log; |
| 14 | + |
| 15 | +import java.io.FileDescriptor; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +/** |
| 20 | +* Use Android system MediaPlayer by default. |
| 21 | +*/ |
| 22 | +public class ExMediaPlayer { |
| 23 | + private static final String TAG = "ExMediaPlayer"; |
| 24 | + |
| 25 | + private MediaPlayer mPlayer; |
| 26 | + |
| 27 | + protected MediaPlayer getLocalPlayer() { |
| 28 | + if (mPlayer == null) { |
| 29 | + mPlayer = new MediaPlayer(); |
| 30 | + Log.d(TAG, "Create a Android System Media Player"); |
| 31 | + } |
| 32 | + return mPlayer; |
| 33 | + } |
| 34 | + |
| 35 | + public void setSurface(Surface surface) { |
| 36 | + getLocalPlayer().setSurface(surface); |
| 37 | + } |
| 38 | + |
| 39 | + public void setDataSource(Context context, Uri uri, Map<String, String> headers) |
| 40 | + throws Exception { |
| 41 | + getLocalPlayer().setDataSource(context, uri, headers); |
| 42 | + } |
| 43 | + |
| 44 | + public void setDataSource(FileDescriptor fd, long offset, long length) throws IOException { |
| 45 | + getLocalPlayer().setDataSource(fd, offset, length); |
| 46 | + } |
| 47 | + |
| 48 | + public void setDataSource(Context context, Uri uri) throws IOException { |
| 49 | + getLocalPlayer().setDataSource(context, uri); |
| 50 | + } |
| 51 | + |
| 52 | + public void prepareAsync() throws IllegalStateException { |
| 53 | + getLocalPlayer().prepareAsync(); |
| 54 | + } |
| 55 | + |
| 56 | + public TrackInfo[] getTrackInfo() throws RuntimeException { |
| 57 | + return getLocalPlayer().getTrackInfo(); |
| 58 | + } |
| 59 | + |
| 60 | + public boolean isPlaying() { |
| 61 | + return getLocalPlayer().isPlaying(); |
| 62 | + } |
| 63 | + |
| 64 | + public int getVideoWidth() { |
| 65 | + return getLocalPlayer().getVideoWidth(); |
| 66 | + } |
| 67 | + |
| 68 | + public int getVideoHeight() { |
| 69 | + return getLocalPlayer().getVideoHeight(); |
| 70 | + } |
| 71 | + |
| 72 | + public int getCurrentPosition() { |
| 73 | + return getLocalPlayer().getCurrentPosition(); |
| 74 | + } |
| 75 | + |
| 76 | + public int getDuration() { |
| 77 | + return getLocalPlayer().getDuration(); |
| 78 | + } |
| 79 | + |
| 80 | + public void release() { |
| 81 | + getLocalPlayer().release(); |
| 82 | + } |
| 83 | + |
| 84 | + public void setVolume(float volume1, float volume2) { |
| 85 | + getLocalPlayer().setVolume((float) volume1, (float) volume2); |
| 86 | + } |
| 87 | + |
| 88 | + public void start() { |
| 89 | + getLocalPlayer().start(); |
| 90 | + } |
| 91 | + |
| 92 | + public void pause() { |
| 93 | + getLocalPlayer().pause(); |
| 94 | + } |
| 95 | + |
| 96 | + public void seekTo(int msec) { |
| 97 | + getLocalPlayer().seekTo(msec); |
| 98 | + } |
| 99 | + |
| 100 | + public void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener listener) { |
| 101 | + getLocalPlayer().setOnBufferingUpdateListener(listener); |
| 102 | + } |
| 103 | + |
| 104 | + public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) { |
| 105 | + getLocalPlayer().setOnCompletionListener(listener); |
| 106 | + } |
| 107 | + |
| 108 | + public void setOnErrorListener(MediaPlayer.OnErrorListener listener) { |
| 109 | + getLocalPlayer().setOnErrorListener(listener); |
| 110 | + } |
| 111 | + |
| 112 | + public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) { |
| 113 | + getLocalPlayer().setOnPreparedListener(listener); |
| 114 | + } |
| 115 | + |
| 116 | + public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener) { |
| 117 | + getLocalPlayer().setOnSeekCompleteListener(listener); |
| 118 | + } |
| 119 | + |
| 120 | + public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) { |
| 121 | + getLocalPlayer().setOnVideoSizeChangedListener(listener); |
| 122 | + } |
| 123 | +} |
0 commit comments