Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the IjkMediaPlayer play the Android raw resource files? #1013

Closed
yjwfn opened this issue Mar 10, 2016 · 11 comments
Closed

Can the IjkMediaPlayer play the Android raw resource files? #1013

yjwfn opened this issue Mar 10, 2016 · 11 comments

Comments

@yjwfn
Copy link

yjwfn commented Mar 10, 2016

I have a video in Android raw directory.

Like this:
android.resource://com.lianlian.fitness/raw/2131165184

Can The IjkMediaPlayer play the Uri?

@bbcallen
Copy link
Contributor

No.
ijkplayer doesn't know what "android.resource" is.

@linwea
Copy link

linwea commented Mar 11, 2016

how to modify the dir let ijkplayer play?

@bbcallen
Copy link
Contributor

Implement IMediaDataSource

@yjwfn
Copy link
Author

yjwfn commented Mar 14, 2016

@bbcallen

I called the setDataSource by:

  if(mUri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)) {
               AssetFileDescriptor fileDescriptor =  getContext().getContentResolver().openAssetFileDescriptor(mUri, "r");
               IMediaDataSource dataSource = new RawDataSourceProvider(fileDescriptor);
               if(fileDescriptor != null)
                   mMediaPlayer.setDataSource(dataSource);
                else{
                   release(false);
                   return ;
               }
            }

I implement IMediaDataSource by myself but ijkplayer does't working.Are there any problems?

class RawDataSourceProvider implements IMediaDataSource{

    AssetFileDescriptor mDescriptor;

    public RawDataSourceProvider(AssetFileDescriptor descriptor) {
        this.mDescriptor = descriptor;
    }

    @Override
    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {

        InputStream is = mDescriptor.createInputStream();
        if(is != null) {
           return is.read(buffer, offset, size);
        }

        return -1;
    }

    @Override
    public long getSize() throws IOException {
        return mDescriptor.getLength();
    }

    @Override
    public void close() throws IOException {
        mDescriptor.close();
    }
}

@bbcallen
Copy link
Contributor

#765
#916

@linwea
Copy link

linwea commented Mar 15, 2016

change
return mDescriptor.getLength();
to
return inputStream.available();

@yjwfn
Copy link
Author

yjwfn commented Mar 15, 2016

@linwea Does it normally work for you ?

@linwea
Copy link

linwea commented Mar 16, 2016

@yjwfn yes

@dengfuyao
Copy link

播放本地资源要怎么填URI,找不到啊,不科学

@AHuminskyi
Copy link

AHuminskyi commented Jul 27, 2018

I use this: (on kotlin)
class AssetMediaSource(val context: Context, val path: String) : IMediaDataSource {

private val inputStream: InputStream by lazy { context.assets.open(path, AssetManager.ACCESS_RANDOM) }
private var position: Long = 0

override fun readAt(position: Long, buffer: ByteArray?, offset: Int, size: Int): Int {
    if(size <= 0) {
        return size
    }
    if(this.position != position) {
        inputStream.reset()
        inputStream.skip(position)
        this.position = position
    }
    Log.d("AssetMediaSource",  "postion: $position offset: $offset, size: $size")
    val n = inputStream.read(buffer, offset, size)
    this.position += n
    return n
}

override fun getSize(): Long {
    val size = inputStream.available().toLong()
    Log.d("AssetMediaSource", "getSize(): $size")
    return size
}

override fun close() {
    Log.d("AssetMediaSource", "close()")
    inputStream.close()
}

}

@yhnu
Copy link

yhnu commented Nov 20, 2018

mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants