Search in sources :

Example 46 with MediaPlayer

use of android.media.MediaPlayer in project Fairphone by Kwamecorp.

the class OOBEActivity method setupTheVideo.

private void setupTheVideo() {
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.oobeVideoViewGroup);
    rl.setVisibility(View.VISIBLE);
    mVideo = (VideoView) findViewById(R.id.fp_oobe_video);
    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fp_buy_a_phone_start_a_movement);
    mVideo.setMediaController(null);
    mVideo.requestFocus();
    mVideo.setVideoURI(uri);
    mVideo.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
    mVideo.start();
    mVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            stopIntroVideo();
        }
    });
    // button
    mSkipVideoButton = (Button) findViewById(R.id.fp_oobe_video_skip_button);
    mSkipVideoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopIntroVideo();
        }
    });
}
Also used : OnTouchListener(android.view.View.OnTouchListener) RelativeLayout(android.widget.RelativeLayout) OnClickListener(android.view.View.OnClickListener) Uri(android.net.Uri) View(android.view.View) VideoView(android.widget.VideoView) MotionEvent(android.view.MotionEvent) MediaPlayer(android.media.MediaPlayer)

Example 47 with MediaPlayer

use of android.media.MediaPlayer in project CircleDemo by Naoki2015.

the class TextureVideoView method openVideo.

private void openVideo() {
    if (mUri == null || mSurface == null || mTargetState != STATE_PLAYING) {
        // not ready for playback just yet, will try again later
        return;
    }
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnInfoListener(this);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setDataSource(mContext, mUri);
        mMediaPlayer.setSurface(mSurface);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(false);
        mMediaPlayer.prepareAsync();
        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        mTargetState = STATE_PREPARING;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            MediaExtractor mediaExtractor = new MediaExtractor();
            mediaExtractor.setDataSource(mContext, mUri, null);
            MediaFormat format;
            mHasAudio = false;
            for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {
                format = mediaExtractor.getTrackFormat(i);
                String mime = format.getString(MediaFormat.KEY_MIME);
                if (mime.startsWith("audio/")) {
                    mHasAudio = true;
                    break;
                }
            }
        } else {
            mHasAudio = true;
        }
    } catch (IOException ex) {
        if (SHOW_LOGS)
            Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        if (mMediaPlayerCallback != null) {
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    if (mMediaPlayerCallback != null) {
                        mMediaPlayerCallback.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
                    }
                }
            });
        }
    } catch (IllegalArgumentException ex) {
        if (SHOW_LOGS)
            Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        if (mMediaPlayerCallback != null) {
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    if (mMediaPlayerCallback != null) {
                        mMediaPlayerCallback.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
                    }
                }
            });
        }
    }
}
Also used : MediaFormat(android.media.MediaFormat) IOException(java.io.IOException) MediaExtractor(android.media.MediaExtractor) MediaPlayer(android.media.MediaPlayer)

Example 48 with MediaPlayer

use of android.media.MediaPlayer in project LuaViewSDK by alibaba.

the class UDAudio method play.

/**
     * start playing audio
     *
     * @param uriOrName
     * @param loopTimes
     * @return
     */
public synchronized UDAudio play(String uriOrName, Integer loopTimes) {
    stopAndReset();
    if (uriOrName != null && uriOrName.equals(this.mUriOrName) == false) {
        //url 不同
        this.mUriOrName = uriOrName;
    }
    if (loopTimes != null) {
        this.mLoopTimes = loopTimes;
    }
    if (this.mUriOrName != null) {
        final MediaPlayer player = getMediaPlayer();
        if (player != null && player.isPlaying() == false) {
            String uri = null;
            boolean assetFileExist = false;
            if (URLUtil.isNetworkUrl(this.mUriOrName) || URLUtil.isFileUrl(this.mUriOrName) || URLUtil.isAssetUrl(this.mUriOrName)) {
                //net & file & asset
                uri = this.mUriOrName;
            } else {
                //plain text, use as file path
                uri = getLuaResourceFinder().buildFullPathInBundleOrAssets(this.mUriOrName);
                assetFileExist = AssetUtil.exists(getContext(), uri);
            }
            try {
                if (assetFileExist) {
                    final AssetFileDescriptor descriptor = getContext().getAssets().openFd(uri);
                    player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                } else {
                    player.setDataSource(uri);
                }
                player.setOnErrorListener(this);
                player.setOnCompletionListener(this);
                player.setOnPreparedListener(this);
                player.setLooping((this.mLoopTimes != null && this.mLoopTimes > 1) ? true : false);
                player.prepareAsync();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return this;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) MediaPlayer(android.media.MediaPlayer)

Example 49 with MediaPlayer

use of android.media.MediaPlayer in project Notes by MiCode.

the class AlarmAlertActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    if (!isScreenOn()) {
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
    }
    Intent intent = getIntent();
    try {
        mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
        mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
        mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) : mSnippet;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return;
    }
    mPlayer = new MediaPlayer();
    if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
        showActionDialog();
        playAlarmSound();
    } else {
        finish();
    }
}
Also used : Window(android.view.Window) Intent(android.content.Intent) MediaPlayer(android.media.MediaPlayer)

Example 50 with MediaPlayer

use of android.media.MediaPlayer in project actor-platform by actorapp.

the class AndroidPlayerActor method onPlayMessage.

protected void onPlayMessage(String fileName) {
    currentFileName = fileName;
    destroyPlayer();
    state = STATE_NONE;
    try {
        mplayer = new MediaPlayer();
        mplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mplayer.setDataSource(context, Uri.fromFile(new File(currentFileName)));
        mplayer.prepare();
        mplayer.setLooping(false);
        mplayer.start();
        mplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                self().send(new Stop());
            }
        });
        mplayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                self().send(new Error());
                return false;
            }
        });
    } catch (Exception e) {
        destroyPlayer();
        callback.onError(currentFileName);
        return;
    }
    callback.onStart(currentFileName);
    schedule(new Notify(), 500);
    state = STATE_STARTED;
}
Also used : File(java.io.File) MediaPlayer(android.media.MediaPlayer)

Aggregations

MediaPlayer (android.media.MediaPlayer)370 IOException (java.io.IOException)180 LargeTest (android.test.suitebuilder.annotation.LargeTest)60 AudioEffect (android.media.audiofx.AudioEffect)54 AudioManager (android.media.AudioManager)50 AssetFileDescriptor (android.content.res.AssetFileDescriptor)28 EnergyProbe (com.android.mediaframeworktest.functional.EnergyProbe)24 MediaRecorder (android.media.MediaRecorder)18 Uri (android.net.Uri)16 SurfaceHolder (android.view.SurfaceHolder)16 CamcorderProfile (android.media.CamcorderProfile)12 File (java.io.File)12 VideoView (android.widget.VideoView)11 Intent (android.content.Intent)8 OnPreparedListener (android.media.MediaPlayer.OnPreparedListener)7 Surface (android.view.Surface)7 View (android.view.View)7 MediaController (android.widget.MediaController)7 MediaFormat (android.media.MediaFormat)6 UtteranceProgressDispatcher (android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher)6