Search in sources :

Example 16 with MediaPlayer

use of android.media.MediaPlayer in project platform_frameworks_base by android.

the class SimplePlayer method playOrPause.

public void playOrPause() {
    if (mMediaPlayer == null || !mMediaPlayer.isPlaying()) {
        if (mMediaPlayer == null) {
            try {
                mMediaPlayer = new MediaPlayer();
                if (mSession != 0) {
                    mMediaPlayer.setAudioSessionId(mSession);
                    Log.d(TAG, "mMediaPlayer.setAudioSessionId(): " + mSession);
                }
                if (mFileName.equals("")) {
                    Log.d(TAG, "Playing from resource");
                    AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(mFileResId);
                    mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    afd.close();
                } else {
                    Log.d(TAG, "Playing file: " + mFileName);
                    mMediaPlayer.setDataSource(mFileName);
                }
                mMediaPlayer.setAudioStreamType(mStreamType);
                mMediaPlayer.prepare();
                mMediaPlayer.setLooping(true);
            } catch (IOException ex) {
                Log.e(TAG, "mMediaPlayercreate failed:", ex);
                mMediaPlayer = null;
            } catch (IllegalArgumentException ex) {
                Log.e(TAG, "mMediaPlayercreate failed:", ex);
                mMediaPlayer = null;
            } catch (SecurityException ex) {
                Log.e(TAG, "mMediaPlayercreate failed:", ex);
                mMediaPlayer = null;
            }
            if (mMediaPlayer != null) {
                mMediaPlayer.setAuxEffectSendLevel(mSendLevel);
                mMediaPlayer.attachAuxEffect(mEffectId);
                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    public void onCompletion(MediaPlayer mp) {
                        updatePlayPauseButton();
                    }
                });
                mSessionText.setText("Session: " + Integer.toString(mMediaPlayer.getAudioSessionId()));
            }
        }
        if (mMediaPlayer != null) {
            mMediaPlayer.start();
        }
    } else {
        mMediaPlayer.pause();
    }
    updatePlayPauseButton();
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 17 with MediaPlayer

use of android.media.MediaPlayer in project platform_frameworks_base by android.

the class MediaSource method setupMediaPlayer.

/** Creates a media player, sets it up, and calls prepare */
private synchronized boolean setupMediaPlayer(boolean useUrl) {
    mPrepared = false;
    mGotSize = false;
    mPlaying = false;
    mPaused = false;
    mCompleted = false;
    mNewFrameAvailable = false;
    if (mLogVerbose)
        Log.v(TAG, "Setting up playback.");
    if (mMediaPlayer != null) {
        // Clean up existing media players
        if (mLogVerbose)
            Log.v(TAG, "Resetting existing MediaPlayer.");
        mMediaPlayer.reset();
    } else {
        // Create new media player
        if (mLogVerbose)
            Log.v(TAG, "Creating new MediaPlayer.");
        mMediaPlayer = new MediaPlayer();
    }
    if (mMediaPlayer == null) {
        throw new RuntimeException("Unable to create a MediaPlayer!");
    }
    // Set up data sources, etc
    try {
        if (useUrl) {
            if (mLogVerbose)
                Log.v(TAG, "Setting MediaPlayer source to URI " + mSourceUrl);
            if (mContext == null) {
                mMediaPlayer.setDataSource(mSourceUrl);
            } else {
                mMediaPlayer.setDataSource(mContext, Uri.parse(mSourceUrl.toString()));
            }
        } else {
            if (mLogVerbose)
                Log.v(TAG, "Setting MediaPlayer source to asset " + mSourceAsset);
            mMediaPlayer.setDataSource(mSourceAsset.getFileDescriptor(), mSourceAsset.getStartOffset(), mSourceAsset.getLength());
        }
    } catch (IOException e) {
        mMediaPlayer.release();
        mMediaPlayer = null;
        if (useUrl) {
            throw new RuntimeException(String.format("Unable to set MediaPlayer to URL %s!", mSourceUrl), e);
        } else {
            throw new RuntimeException(String.format("Unable to set MediaPlayer to asset %s!", mSourceAsset), e);
        }
    } catch (IllegalArgumentException e) {
        mMediaPlayer.release();
        mMediaPlayer = null;
        if (useUrl) {
            throw new RuntimeException(String.format("Unable to set MediaPlayer to URL %s!", mSourceUrl), e);
        } else {
            throw new RuntimeException(String.format("Unable to set MediaPlayer to asset %s!", mSourceAsset), e);
        }
    }
    mMediaPlayer.setLooping(mLooping);
    mMediaPlayer.setVolume(mVolume, mVolume);
    // Bind it to our media frame
    Surface surface = new Surface(mSurfaceTexture);
    mMediaPlayer.setSurface(surface);
    surface.release();
    // Connect Media Player to callbacks
    mMediaPlayer.setOnVideoSizeChangedListener(onVideoSizeChangedListener);
    mMediaPlayer.setOnPreparedListener(onPreparedListener);
    mMediaPlayer.setOnCompletionListener(onCompletionListener);
    // Connect SurfaceTexture to callback
    mSurfaceTexture.setOnFrameAvailableListener(onMediaFrameAvailableListener);
    if (mLogVerbose)
        Log.v(TAG, "Preparing MediaPlayer.");
    mMediaPlayer.prepareAsync();
    return true;
}
Also used : IOException(java.io.IOException) IllegalArgumentException(java.lang.IllegalArgumentException) MediaPlayer(android.media.MediaPlayer) Surface(android.view.Surface)

Example 18 with MediaPlayer

use of android.media.MediaPlayer in project platform_frameworks_base by android.

the class CodecTest method playToEnd.

public static boolean playToEnd(String filePath) {
    Log.v(TAG, "shortMediaStop - " + filePath);
    //This test is only for the short media file
    int duration = 200000;
    int updateDuration = 0;
    int currentPosition = 0;
    boolean isPlaying = false;
    MediaPlayer mp = new MediaPlayer();
    try {
        Thread.sleep(5000);
        mp.setDataSource(filePath);
        Log.v(TAG, "start playback");
        mp.prepare();
        //duration = mp.getDuration();
        mp.start();
        Thread.sleep(50000);
    } catch (Exception e) {
    }
    isPlaying = mp.isPlaying();
    currentPosition = mp.getCurrentPosition();
    //updateDuration = mp.getDuration();
    Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
    mp.stop();
    mp.release();
    //Log.v(TAG, "Update duration = " + updateDuration);
    if (currentPosition > duration || isPlaying)
        return false;
    else
        return true;
}
Also used : IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 19 with MediaPlayer

use of android.media.MediaPlayer in project platform_frameworks_base by android.

the class MediaPlayerInvokeTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    mPlayer = new MediaPlayer();
}
Also used : MediaPlayer(android.media.MediaPlayer)

Example 20 with MediaPlayer

use of android.media.MediaPlayer in project platform_frameworks_base by android.

the class VideoDumpView method onResume.

@Override
public void onResume() {
    Log.d(TAG, "onResume");
    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.setDataSource(VideoDumpConfig.VIDEO_URI);
        class RGBFilter implements FilenameFilter {

            public boolean accept(File dir, String name) {
                return (name.endsWith(VideoDumpConfig.IMAGE_SUFFIX));
            }
        }
        File dump_dir = new File(VideoDumpConfig.ROOT_DIR);
        File[] dump_files = dump_dir.listFiles(new RGBFilter());
        for (File dump_file : dump_files) {
            dump_file.delete();
        }
        File image_list = new File(VideoDumpConfig.ROOT_DIR + VideoDumpConfig.IMAGES_LIST);
        image_list.delete();
        mImageListWriter = new BufferedWriter(new FileWriter(image_list));
    } catch (java.io.IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }
    queueEvent(new Runnable() {

        public void run() {
            mRenderer.setMediaPlayer(mMediaPlayer);
            mRenderer.setImageListWriter(mImageListWriter);
        }
    });
    super.onResume();
}
Also used : FilenameFilter(java.io.FilenameFilter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) MediaPlayer(android.media.MediaPlayer) BufferedWriter(java.io.BufferedWriter)

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