Skip to content

Commit 293cb91

Browse files
committed
android/MediaPlayer: support more setDataSource
1 parent 131da33 commit 293cb91

12 files changed

Lines changed: 474 additions & 34 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
tag next
22
--------------------------------
3+
android: support FileDescriptor as data source
34

45
tag k0.3.3
56
--------------------------------
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2013-2014 Zhang Rui <bbcallen@gmail.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tv.danmaku.ijk.media.player;
18+
19+
public abstract class AbstractMediaPlayer implements IMediaPlayer {
20+
private OnPreparedListener mOnPreparedListener;
21+
private OnCompletionListener mOnCompletionListener;
22+
private OnBufferingUpdateListener mOnBufferingUpdateListener;
23+
private OnSeekCompleteListener mOnSeekCompleteListener;
24+
private OnVideoSizeChangedListener mOnVideoSizeChangedListener;
25+
private OnErrorListener mOnErrorListener;
26+
private OnInfoListener mOnInfoListener;
27+
28+
public final void setOnPreparedListener(OnPreparedListener listener) {
29+
mOnPreparedListener = listener;
30+
}
31+
32+
public final void setOnCompletionListener(OnCompletionListener listener) {
33+
mOnCompletionListener = listener;
34+
}
35+
36+
public final void setOnBufferingUpdateListener(
37+
OnBufferingUpdateListener listener) {
38+
mOnBufferingUpdateListener = listener;
39+
}
40+
41+
public final void setOnSeekCompleteListener(OnSeekCompleteListener listener) {
42+
mOnSeekCompleteListener = listener;
43+
}
44+
45+
public final void setOnVideoSizeChangedListener(
46+
OnVideoSizeChangedListener listener) {
47+
mOnVideoSizeChangedListener = listener;
48+
}
49+
50+
public final void setOnErrorListener(OnErrorListener listener) {
51+
mOnErrorListener = listener;
52+
}
53+
54+
public final void setOnInfoListener(OnInfoListener listener) {
55+
mOnInfoListener = listener;
56+
}
57+
58+
public void resetListeners() {
59+
mOnPreparedListener = null;
60+
mOnBufferingUpdateListener = null;
61+
mOnCompletionListener = null;
62+
mOnSeekCompleteListener = null;
63+
mOnVideoSizeChangedListener = null;
64+
mOnErrorListener = null;
65+
mOnInfoListener = null;
66+
}
67+
68+
protected final void notifyOnPrepared() {
69+
if (mOnPreparedListener != null)
70+
mOnPreparedListener.onPrepared(this);
71+
}
72+
73+
protected final void notifyOnCompletion() {
74+
if (mOnCompletionListener != null)
75+
mOnCompletionListener.onCompletion(this);
76+
}
77+
78+
protected final void notifyOnBufferingUpdate(int percent) {
79+
if (mOnBufferingUpdateListener != null)
80+
mOnBufferingUpdateListener.onBufferingUpdate(this, percent);
81+
}
82+
83+
protected final void notifyOnSeekComplete() {
84+
if (mOnSeekCompleteListener != null)
85+
mOnSeekCompleteListener.onSeekComplete(this);
86+
}
87+
88+
protected final void notifyOnVideoSizeChanged(int width, int height,
89+
int sarNum, int sarDen) {
90+
if (mOnVideoSizeChangedListener != null)
91+
mOnVideoSizeChangedListener.onVideoSizeChanged(this, width, height,
92+
sarNum, sarDen);
93+
}
94+
95+
protected final boolean notifyOnError(int what, int extra) {
96+
if (mOnErrorListener != null)
97+
return mOnErrorListener.onError(this, what, extra);
98+
return false;
99+
}
100+
101+
protected final boolean notifyOnInfo(int what, int extra) {
102+
if (mOnInfoListener != null)
103+
return mOnInfoListener.onInfo(this, what, extra);
104+
return false;
105+
}
106+
}

android/ijkplayer/player-java/src/main/java/tv/danmaku/ijk/media/player/AndroidMediaPlayer.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import android.view.Surface;
2828
import android.view.SurfaceHolder;
2929

30+
import java.io.FileDescriptor;
3031
import java.io.IOException;
3132
import java.lang.ref.WeakReference;
33+
import java.util.Map;
3234

3335
import tv.danmaku.ijk.media.player.pragma.DebugLog;
3436

35-
public class AndroidMediaPlayer extends SimpleMediaPlayer {
37+
public class AndroidMediaPlayer extends AbstractMediaPlayer {
3638
private MediaPlayer mInternalMediaPlayer;
3739
private AndroidMediaPlayerListenerHolder mInternalListenerAdapter;
3840
private String mDataSource;
@@ -68,6 +70,25 @@ public void setSurface(Surface surface) {
6870
mInternalMediaPlayer.setSurface(surface);
6971
}
7072

73+
@Override
74+
public void setDataSource(Context context, Uri uri)
75+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
76+
mInternalMediaPlayer.setDataSource(context, uri);
77+
}
78+
79+
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
80+
@Override
81+
public void setDataSource(Context context, Uri uri, Map<String, String> headers)
82+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
83+
mInternalMediaPlayer.setDataSource(context, uri, headers);
84+
}
85+
86+
@Override
87+
public void setDataSource(FileDescriptor fd)
88+
throws IOException, IllegalArgumentException, IllegalStateException {
89+
mInternalMediaPlayer.setDataSource(fd);
90+
}
91+
7192
@Override
7293
public void setDataSource(String path) throws IOException,
7394
IllegalArgumentException, SecurityException, IllegalStateException {
@@ -206,6 +227,15 @@ public MediaInfo getMediaInfo() {
206227
return sMediaInfo;
207228
}
208229

230+
@Override
231+
public void setLogEnabled(boolean enable) {
232+
}
233+
234+
@Override
235+
public boolean isPlayable() {
236+
return true;
237+
}
238+
209239
/*--------------------
210240
* misc
211241
*/

android/ijkplayer/player-java/src/main/java/tv/danmaku/ijk/media/player/BaseMediaPlayer.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
import android.annotation.TargetApi;
2020
import android.content.Context;
21+
import android.net.Uri;
2122
import android.os.Build;
2223
import android.view.Surface;
2324

24-
/**
25-
* @author bbcallen
26-
*
27-
* Optional interface default implements
28-
*/
25+
import java.io.FileDescriptor;
26+
import java.io.IOException;
27+
import java.util.Map;
28+
29+
@Deprecated
2930
public abstract class BaseMediaPlayer implements IMediaPlayer {
3031
private boolean mIsLogEnabled;
3132

@@ -71,4 +72,23 @@ public void setWakeMode(Context context, int mode) {
7172
@Override
7273
public void setSurface(Surface surface) {
7374
}
75+
76+
@Override
77+
public void setDataSource(Context context, Uri uri)
78+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
79+
setDataSource(uri.getPath());
80+
}
81+
82+
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
83+
@Override
84+
public void setDataSource(Context context, Uri uri, Map<String, String> headers)
85+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
86+
setDataSource(uri.getPath());
87+
}
88+
89+
@Override
90+
public void setDataSource(FileDescriptor fd)
91+
throws IOException, IllegalArgumentException, IllegalStateException {
92+
throw new RuntimeException("does not support setDataSource(FileDescriptor fd)");
93+
}
7494
}

android/ijkplayer/player-java/src/main/java/tv/danmaku/ijk/media/player/IMediaPlayer.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import android.annotation.TargetApi;
2020
import android.content.Context;
21+
import android.net.Uri;
2122
import android.os.Build;
2223
import android.view.Surface;
2324
import android.view.SurfaceHolder;
2425

26+
import java.io.FileDescriptor;
2527
import java.io.IOException;
28+
import java.util.Map;
2629

2730
public interface IMediaPlayer {
2831
/*
@@ -51,8 +54,18 @@ public interface IMediaPlayer {
5154

5255
void setDisplay(SurfaceHolder sh);
5356

54-
void setDataSource(String path) throws IOException,
55-
IllegalArgumentException, SecurityException, IllegalStateException;
57+
void setDataSource(Context context, Uri uri)
58+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;
59+
60+
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
61+
void setDataSource(Context context, Uri uri, Map<String, String> headers)
62+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;
63+
64+
void setDataSource(FileDescriptor fd)
65+
throws IOException, IllegalArgumentException, IllegalStateException;
66+
67+
void setDataSource(String path)
68+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;
5669

5770
String getDataSource();
5871

@@ -86,8 +99,10 @@ void setDataSource(String path) throws IOException,
8699

87100
MediaInfo getMediaInfo();
88101

102+
@Deprecated
89103
void setLogEnabled(boolean enable);
90104

105+
@Deprecated
91106
boolean isPlayable();
92107

93108
void setOnPreparedListener(OnPreparedListener listener);
@@ -144,6 +159,7 @@ interface OnInfoListener {
144159
*/
145160
void setAudioStreamType(int streamtype);
146161

162+
@Deprecated
147163
void setKeepInBackground(boolean keepInBackground);
148164

149165
int getVideoSarNum();

0 commit comments

Comments
 (0)