Java Code Examples for android.media.MediaPlayer#release()

The following examples show how to use android.media.MediaPlayer#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeepManager.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    try {
        AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        } finally {
            file.close();
        }
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
        mediaPlayer.prepare();
        return mediaPlayer;
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        mediaPlayer.release();
        return null;
    }
}
 
Example 2
Source File: BeepManager.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
  MediaPlayer mediaPlayer = new MediaPlayer();
  mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  mediaPlayer.setOnCompletionListener(this);
  mediaPlayer.setOnErrorListener(this);
  try {
    AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
    try {
      mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
    } finally {
      file.close();
    }
    mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
    mediaPlayer.prepare();
    return mediaPlayer;
  } catch (IOException ioe) {
    Log.w(TAG, ioe);
    mediaPlayer.release();
    return null;
  }
}
 
Example 3
Source File: BeepManager.java    From AirFree-Client with GNU General Public License v3.0 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    try {
        AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        } finally {
            file.close();
        }
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
        mediaPlayer.prepare();
        return mediaPlayer;
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        mediaPlayer.release();
        return null;
    }
}
 
Example 4
Source File: BeepManager.java    From Android with MIT License 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
	MediaPlayer mediaPlayer = new MediaPlayer();
	mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	mediaPlayer.setOnCompletionListener(this);
	mediaPlayer.setOnErrorListener(this);
	try {
		AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
		try {
			mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
		} finally {
			file.close();
		}
		mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
		mediaPlayer.prepare();
		return mediaPlayer;
	} catch (IOException ioe) {
		Log.w(TAG, ioe);
		mediaPlayer.release();
		return null;
	}
}
 
Example 5
Source File: BeepManager.java    From FamilyChat with Apache License 2.0 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    try {
        AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        } finally {
            file.close();
        }
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
        mediaPlayer.prepare();
        return mediaPlayer;
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        mediaPlayer.release();
        return null;
    }
}
 
Example 6
Source File: BeepManager.java    From ZXingProject with MIT License 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
	MediaPlayer mediaPlayer = new MediaPlayer();
	mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	mediaPlayer.setOnCompletionListener(this);
	mediaPlayer.setOnErrorListener(this);
	try {
		AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
		try {
			mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
		} finally {
			file.close();
		}
		mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
		mediaPlayer.prepare();
		return mediaPlayer;
	} catch (IOException ioe) {
		Log.w(TAG, ioe);
		mediaPlayer.release();
		return null;
	}
}
 
Example 7
Source File: BeepManager.java    From ZXing-Standalone-library with Apache License 2.0 6 votes vote down vote up
private MediaPlayer buildMediaPlayer(Context activity) {
  MediaPlayer mediaPlayer = new MediaPlayer();
  try {
    AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
    try {
      mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
    } finally {
      file.close();
    }
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setLooping(false);
    mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
    mediaPlayer.prepare();
    return mediaPlayer;
  } catch (IOException ioe) {
    Log.w(TAG, ioe);
    mediaPlayer.release();
    return null;
  }
}
 
Example 8
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void deleteAudioData(AudioData ad) {
    if (ad instanceof AndroidAudioData) {
        AndroidAudioData audioData = (AndroidAudioData) ad;
        if (audioData.getAssetKey() instanceof AudioKey) {
            AudioKey assetKey = (AudioKey) audioData.getAssetKey();
            if (assetKey.isStream()) {
                for (AudioNode src : musicPlaying.keySet()) {
                    if (src.getAudioData() == ad) {
                        MediaPlayer mp = musicPlaying.get(src);
                        mp.stop();
                        mp.release();
                        musicPlaying.remove(src);
                        src.setStatus(Status.Stopped);
                        src.setChannel(-1);
                        break;
                    }
                }
            } else {
                if (audioData.getId() > 0) {
                    soundPool.unload(audioData.getId());
                }
                audioData.setId(0);
            }

        }
    } else {
        throw new IllegalArgumentException("AudioData is not of type AndroidAudioData in deleteAudioData");
    }
}
 
Example 9
Source File: TextureVideoView.java    From texturevideoview with Apache License 2.0 5 votes vote down vote up
public int getAudioSessionId() {
    if (mAudioSession == 0) {
        MediaPlayer foo = new MediaPlayer();
        mAudioSession = foo.getAudioSessionId();
        foo.release();
    }
    return mAudioSession;
}
 
Example 10
Source File: BeepManager.java    From ScanZxing with Apache License 2.0 5 votes vote down vote up
/**
 * 创建MediaPlayer
 * 
 * @param activity
 * @return
 */
private MediaPlayer buildMediaPlayer(Context activity) {
	MediaPlayer mediaPlayer = new MediaPlayer();
	mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	// 监听是否播放完成
	mediaPlayer.setOnCompletionListener(this);
	mediaPlayer.setOnErrorListener(this);
	// 配置播放资源
	try {
		AssetFileDescriptor file = activity.getResources()
				.openRawResourceFd(R.raw.beep);
		try {
			mediaPlayer.setDataSource(file.getFileDescriptor(),
					file.getStartOffset(), file.getLength());
		} finally {
			file.close();
		}
		// 设置音量
		mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
		mediaPlayer.prepare();
		return mediaPlayer;
	} catch (IOException ioe) {
		Log.w(TAG, ioe);
		mediaPlayer.release();
		return null;
	}
}
 
Example 11
Source File: AudioTransCoder.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
private void prepare() throws IOException {
    extractor = new MediaExtractor();
    extractor.setDataSource(mInputFile.getAbsolutePath());
    int numTracks = extractor.getTrackCount();
    for (int i = 0; i < numTracks; i++) {
        MediaFormat format = extractor.getTrackFormat(i);
        String mine = format.getString(MediaFormat.KEY_MIME);
        if (!TextUtils.isEmpty(mine) && mine.startsWith("audio")) {
            extractor.selectTrack(i);
            if (mDurationMs == 0) {
                try {
                    mDurationMs = format.getLong(MediaFormat.KEY_DURATION) / 1000;
                } catch (Exception e) {
                    e.printStackTrace();
                    MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(mInputFile.getAbsolutePath());
                    mediaPlayer.prepare();
                    mDurationMs = mediaPlayer.getDuration();
                    mediaPlayer.release();
                }
            }

            if (mDurationMs == 0) {
                throw new IllegalStateException("We can not get duration info from input file: " + mInputFile);
            }

            decoder = MediaCodec.createDecoderByType(mine);
            decoder.configure(format, null, null, 0);
            decoder.start();
            break;
        }
    }
}
 
Example 12
Source File: BeepManager.java    From FoodOrdering with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
	if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
		// we are finished, so put up an appropriate error toast if required
		// and finish
		activity.finish();
	} else {
		// possibly media player error, so release and recreate
		mp.release();
		mediaPlayer = null;
		updatePrefs();
	}
	return true;
}
 
Example 13
Source File: BeepManager.java    From myapplication with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
    if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
        // we are finished, so put up an appropriate error toast if required
        // and finish
        activity.finish();
    } else {
        // possibly media player error, so release and recreate
        mp.release();
        mediaPlayer = null;
        updatePrefs();
    }
    return true;
}
 
Example 14
Source File: VideoInfo.java    From Android-Video-Trimmer with Apache License 2.0 5 votes vote down vote up
public static VideoInfo buildVideo(Context context, String videoPath) {
    VideoInfo info = new VideoInfo();
    info.setVideoPath(videoPath);
    try {
        MediaPlayer mp = MediaPlayer.create(context, Uri.fromFile(new File(videoPath)));
        if (mp != null) {
            info.setDuration(mp.getDuration());
            mp.release();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return info;
}
 
Example 15
Source File: BeepManager.java    From AirFree-Client with GNU General Public License v3.0 5 votes vote down vote up
@Override
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
    if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
        // we are finished, so put up an appropriate error toast if required
        // and finish
        activity.finish();
    } else {
        // possibly media player error, so release and recreate
        mp.release();
        mediaPlayer = null;
        updatePrefs();
    }
    return true;
}
 
Example 16
Source File: HelpUtils.java    From browser with GNU General Public License v2.0 5 votes vote down vote up
public static int mediaDuration(Context context,String path){
    MediaPlayer mp = MediaPlayer.create(context, Uri.parse(path));
    if(mp==null){
        return 0;
    }
    int duration = mp.getDuration();
    mp.release();
    return duration;
}
 
Example 17
Source File: SoundManager.java    From Camdroid with Apache License 2.0 5 votes vote down vote up
public void release() {
	for (int i = 0; i < this.sounds.size(); i++) {
		MediaPlayer mp = this.sounds.valueAt(i);
		Log.d(TAG, "mp: " + mp.getAudioSessionId() + " release");
		mp.release();
	}
	this.sounds.clear();
}
 
Example 18
Source File: BeepManager.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
  if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
    // we are finished, so put up an appropriate error toast if required and finish
    activity.finish();
  } else {
    // possibly media player error, so release and recreate
    mp.release();
    mediaPlayer = null;
    updatePrefs(false, true);
  }
  return true;
}
 
Example 19
Source File: PathConversion.java    From Album with Apache License 2.0 4 votes vote down vote up
@WorkerThread
@NonNull
public AlbumFile convert(String filePath) {
    File file = new File(filePath);

    AlbumFile albumFile = new AlbumFile();
    albumFile.setPath(filePath);

    File parentFile = file.getParentFile();
    albumFile.setBucketName(parentFile.getName());

    String mimeType = AlbumUtils.getMimeType(filePath);
    albumFile.setMimeType(mimeType);
    long nowTime = System.currentTimeMillis();
    albumFile.setAddDate(nowTime);
    albumFile.setSize(file.length());
    int mediaType = 0;
    if (!TextUtils.isEmpty(mimeType)) {
        if (mimeType.contains("video"))
            mediaType = AlbumFile.TYPE_VIDEO;
        if (mimeType.contains("image"))
            mediaType = AlbumFile.TYPE_IMAGE;
    }
    albumFile.setMediaType(mediaType);

    if (mSizeFilter != null && mSizeFilter.filter(file.length())) {
        albumFile.setDisable(true);
    }
    if (mMimeFilter != null && mMimeFilter.filter(mimeType)) {
        albumFile.setDisable(true);
    }

    if (mediaType == AlbumFile.TYPE_VIDEO) {
        MediaPlayer player = new MediaPlayer();
        try {
            player.setDataSource(filePath);
            player.prepare();
            albumFile.setDuration(player.getDuration());
        } catch (Exception ignored) {
        } finally {
            player.release();
        }

        if (mDurationFilter != null && mDurationFilter.filter(albumFile.getDuration())) {
            albumFile.setDisable(true);
        }
    }
    return albumFile;
}
 
Example 20
Source File: PickerActivity.java    From MultiImagePicker with MIT License 2 votes vote down vote up
public void onEvent(final Events.OnPickImageEvent pickImageEvent) {
    if (mPickOptions.videosEnabled && mPickOptions.videoLengthLimit > 0 && pickImageEvent.imageEntry.isVideo) {
        // Check to see if the selected video is too long in length
        final MediaPlayer mp = MediaPlayer.create(this, Uri.parse(pickImageEvent.imageEntry.path));
        final int duration = mp.getDuration();
        mp.release();
        if (duration > (mPickOptions.videoLengthLimit)) {
            Toast.makeText(this, getResources().getString(R.string.video_too_long).replace("$", String.valueOf(mPickOptions.videoLengthLimit / 1000)), Toast.LENGTH_SHORT).show();
            return; // Don't allow selection
        }
    }

    if (mPickOptions.pickMode == Picker.PickMode.MULTIPLE_IMAGES) {
        handleMultipleModeAddition(pickImageEvent.imageEntry);


    } else if (mPickOptions.pickMode == Picker.PickMode.SINGLE_IMAGE) {
        //Single image pick mode


        final ImagesPagerFragment pagerFragment;

        if (getSupportFragmentManager().findFragmentByTag(ImagesPagerFragment.TAG) != null) {

            pagerFragment = (ImagesPagerFragment) getSupportFragmentManager().findFragmentByTag(ImagesPagerFragment.TAG);
        } else {
            pagerFragment = new ImagesPagerFragment();
        }


        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, pagerFragment, ImagesPagerFragment.TAG)
                .addToBackStack(ImagesPagerFragment.TAG)
                .commit();


    }


    updateFab();

}