Search in sources :

Example 1 with MediaDataSource

use of android.media.MediaDataSource in project Zom-Android by zom.

the class AudioWife method initPlayer.

/**
 **
 * Initialize and prepare the audio player
 ***
 */
private void initPlayer(Context ctx, final Uri mediaUri, String mimeType) throws IOException {
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    if (mediaUri.getScheme() != null) {
        if (mediaUri.getScheme().equals("vfs")) {
            final info.guardianproject.iocipher.File fileMediaEncrypted = new info.guardianproject.iocipher.File(mediaUri.getPath());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mMediaPlayer.setDataSource(new MediaDataSource() {

                    final info.guardianproject.iocipher.RandomAccessFile fis = new info.guardianproject.iocipher.RandomAccessFile(fileMediaEncrypted, "r");

                    @Override
                    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
                        if (position > getSize())
                            return -1;
                        fis.seek(position);
                        byte[] outBuffer = new byte[size];
                        int readSize = fis.read(outBuffer, 0, size);
                        System.arraycopy(outBuffer, 0, buffer, offset, size);
                        return readSize;
                    }

                    @Override
                    public long getSize() throws IOException {
                        return fis.length();
                    }

                    @Override
                    public void close() throws IOException {
                        if (fis != null)
                            fis.close();
                    }
                });
            } else {
                HttpMediaStreamer streamer = new HttpMediaStreamer(fileMediaEncrypted, mimeType);
                Uri uri = streamer.getUri();
                mMediaPlayer.setDataSource(ctx, uri);
            }
        } else if (mediaUri.getScheme().equals("content")) {
            mMediaPlayer.setDataSource(ctx, mediaUri);
        } else if (mediaUri.getScheme().equals("file")) {
            mMediaPlayer.setDataSource(ctx, mediaUri);
        }
    } else {
        mMediaPlayer.setDataSource(mediaUri.getPath());
    }
    try {
        mMediaPlayer.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    mMediaPlayer.setOnCompletionListener(mOnCompletion);
}
Also used : File(info.guardianproject.iocipher.File) HttpMediaStreamer(org.awesomeapp.messenger.util.HttpMediaStreamer) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) MediaDataSource(android.media.MediaDataSource) File(info.guardianproject.iocipher.File) MediaPlayer(android.media.MediaPlayer)

Example 2 with MediaDataSource

use of android.media.MediaDataSource in project Zom-Android by zom.

the class AudioPlayer method initPlayer.

public void initPlayer() throws Exception {
    final File fileStream = new File(mFileName);
    if (mediaPlayer != null) {
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
    }
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    if (fileStream.exists()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mediaPlayer.setDataSource(new MediaDataSource() {

                info.guardianproject.iocipher.RandomAccessFile fis;

                @Override
                public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
                    if (fis == null)
                        fis = new info.guardianproject.iocipher.RandomAccessFile(fileStream, "r");
                    if (position > getSize())
                        return -1;
                    fis.seek(position);
                    byte[] outBuffer = new byte[size];
                    int readSize = fis.read(outBuffer, 0, size);
                    System.arraycopy(outBuffer, 0, buffer, offset, size);
                    return readSize;
                }

                @Override
                public long getSize() throws IOException {
                    return fileStream.length();
                }

                @Override
                public void close() throws IOException {
                    if (fis != null)
                        fis.close();
                }
            });
        } else {
            streamer = new HttpMediaStreamer(fileStream, mMimeType);
            Uri uri = streamer.getUri();
            mediaPlayer.setDataSource(mContext, uri);
        }
    } else {
        mediaPlayer.setDataSource(mFileName);
    }
    mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            Log.d("AudioPlayer", "there was an error loading music: " + i + " " + i1);
            return true;
        }
    });
    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            mPrepared = true;
            mDuration = mediaPlayer.getDuration();
            mInfoView.setText((getDuration() / 1000) + "secs");
            if (mPlayOnPrepare)
                play();
        }
    });
    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            if (mVisualizer != null)
                mVisualizer.setEnabled(false);
        }
    });
// mediaPlayer.prepareAsync();
}
Also used : OnPreparedListener(android.media.MediaPlayer.OnPreparedListener) HttpMediaStreamer(org.awesomeapp.messenger.util.HttpMediaStreamer) IOException(java.io.IOException) Uri(android.net.Uri) RandomAccessFile(info.guardianproject.iocipher.RandomAccessFile) OnCompletionListener(android.media.MediaPlayer.OnCompletionListener) MediaDataSource(android.media.MediaDataSource) File(info.guardianproject.iocipher.File) RandomAccessFile(info.guardianproject.iocipher.RandomAccessFile) MediaPlayer(android.media.MediaPlayer)

Aggregations

MediaDataSource (android.media.MediaDataSource)2 MediaPlayer (android.media.MediaPlayer)2 Uri (android.net.Uri)2 File (info.guardianproject.iocipher.File)2 IOException (java.io.IOException)2 HttpMediaStreamer (org.awesomeapp.messenger.util.HttpMediaStreamer)2 OnCompletionListener (android.media.MediaPlayer.OnCompletionListener)1 OnPreparedListener (android.media.MediaPlayer.OnPreparedListener)1 RandomAccessFile (info.guardianproject.iocipher.RandomAccessFile)1