mirror of
https://github.com/samsonjs/media.git
synced 2026-03-28 09:55:48 +00:00
null AssetFileDescriptors support added in ContentDataSource
This commit is contained in:
parent
66c461e65b
commit
795e3be440
4 changed files with 37 additions and 2 deletions
|
|
@ -1,8 +1,16 @@
|
|||
package com.google.android.exoplayer2.upstream;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.net.Uri;
|
||||
|
||||
final class AndroidDataSourceConstants {
|
||||
static final long SAMPLE_MP4_BYTES = 101597;
|
||||
static final String SAMPLE_MP4_PATH = "/mp4/sample.mp4";
|
||||
static final String TEST_DATA_PROVIDER_AUTHORITY = "exoplayer";
|
||||
static final Uri NULL_DESCRIPTOR_URI = new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(TEST_DATA_PROVIDER_AUTHORITY)
|
||||
.build();
|
||||
|
||||
private AndroidDataSourceConstants() {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,40 @@
|
|||
package com.google.android.exoplayer2.upstream;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.test.InstrumentationTestCase;
|
||||
|
||||
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.NULL_DESCRIPTOR_URI;
|
||||
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.SAMPLE_MP4_BYTES;
|
||||
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.SAMPLE_MP4_PATH;
|
||||
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.TEST_DATA_PROVIDER_AUTHORITY;
|
||||
|
||||
public class ContentDataSourceTest extends InstrumentationTestCase {
|
||||
|
||||
public void testContentDataSource() throws Exception {
|
||||
public void testValidContentDataSource() throws Exception {
|
||||
Context context = getInstrumentation().getContext();
|
||||
ContentDataSource dataSource = new ContentDataSource(context);
|
||||
Uri contentUri = Uri.parse("content://exoplayer" + SAMPLE_MP4_PATH);
|
||||
Uri contentUri = new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(TEST_DATA_PROVIDER_AUTHORITY)
|
||||
.path(SAMPLE_MP4_PATH).build();
|
||||
DataSpec dataSpec = new DataSpec(contentUri);
|
||||
long sourceLengthBytes = dataSource.open(dataSpec);
|
||||
|
||||
assertEquals(SAMPLE_MP4_BYTES, sourceLengthBytes);
|
||||
}
|
||||
|
||||
public void testNullContentDataSource() throws Exception {
|
||||
Context context = getInstrumentation().getContext();
|
||||
ContentDataSource dataSource = new ContentDataSource(context);
|
||||
DataSpec dataSpec = new DataSpec(NULL_DESCRIPTOR_URI);
|
||||
|
||||
try {
|
||||
dataSource.open(dataSpec);
|
||||
fail("Expected exception not thrown.");
|
||||
} catch (ContentDataSource.ContentDataSourceException e) {
|
||||
// Expected.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ public class TestDataProvider extends ContentProvider {
|
|||
@Nullable
|
||||
@Override
|
||||
public AssetFileDescriptor openAssetFile(@NonNull final Uri uri, @NonNull final String mode) throws FileNotFoundException {
|
||||
if (uri.equals(AndroidDataSourceConstants.NULL_DESCRIPTOR_URI)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Context context = getContext();
|
||||
assertNotNull(context);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import android.net.Uri;
|
|||
import com.google.android.exoplayer2.C;
|
||||
import java.io.EOFException;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
|
@ -71,6 +72,9 @@ public final class ContentDataSource implements DataSource {
|
|||
try {
|
||||
uri = dataSpec.uri;
|
||||
assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
|
||||
if (assetFileDescriptor == null) {
|
||||
throw new FileNotFoundException("Could not open file descriptor for: " + uri);
|
||||
}
|
||||
inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
|
||||
long skipped = inputStream.skip(dataSpec.position);
|
||||
if (skipped < dataSpec.position) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue