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

The following examples show how to use android.media.MediaPlayer#create() . 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: AuthenticationActivity.java    From ml-authentication with Apache License 2.0 6 votes vote down vote up
@Override
public void onResume()
{
    super.onResume();
    ppF = new PreProcessorFactory(getApplicationContext());
    numberOfTries = 0;
    animalOverlay = animalOverlayHelper.getAnimalOverlay("");
    if (animalOverlay != null) {
        mediaPlayerAnimalSound = MediaPlayer.create(this, getResources().getIdentifier(animalOverlay.getSoundFile(), AuthenticationInstructionHelper.RESOURCES_RAW_FOLDER, getPackageName()));
    }
    preview.enableView();
    mediaPlayerTabletPlacement = AuthenticationInstructionHelper.getMediaPlayerTabletPlacement(getApplicationContext());
    mediaPlayerTabletPlacement.start();
    mediaPlayerTabletPlacementOverlay = AuthenticationInstructionHelper.getMediaPlayerTabletPlacementOverlay(getApplicationContext());
    tensorFlowLoadingThread.start();
    startTimeFallback = new Date().getTime();
    startTimeAuthenticationAnimation = new Date().getTime();
}
 
Example 2
Source File: MainActivity.java    From Android-Audio-Recorder with Apache License 2.0 6 votes vote down vote up
public void scan(File dir) {
    setNotifyOnChange(false);
    clear();
    durations.clear();

    List<File> ff = storage.scan(dir);

    for (File f : ff) {
        if (f.isFile()) {
            MediaPlayer mp = MediaPlayer.create(getContext(), Uri.fromFile(f));
            if (mp != null) {
                int d = mp.getDuration();
                mp.release();
                durations.put(f, d);
                add(f);
            } else {
                Log.e(TAG, f.toString());
            }
        }
    }

    sort(new SortFiles());
    notifyDataSetChanged();
}
 
Example 3
Source File: ResourcesManager.java    From sopa with Apache License 2.0 6 votes vote down vote up
public static void prepareManager(Engine engine, GameActivity activity, Camera camera,
    VertexBufferObjectManager vbom, ResourceLoader resourceLoader, StoryService storyService,
    LevelService levelService, SettingsService settingsService, JustPlayScoreService justPlayScoreService) {

    getInstance().engine = engine;
    getInstance().activity = activity;
    getInstance().camera = camera;
    getInstance().vbom = vbom;
    getInstance().resourceLoader = resourceLoader;
    getInstance().storyService = storyService;
    getInstance().levelService = levelService;
    getInstance().musicService = new MusicService(MediaPlayer.create(activity.getApplicationContext(), R.raw.theme),
            true);
    getInstance().settingsService = settingsService;
    getInstance().justPlayScoreService = justPlayScoreService;
}
 
Example 4
Source File: MediaManager.java    From a with GNU General Public License v3.0 5 votes vote down vote up
public static void playSilentSound(Context mContext) {
    try {
        // Stupid Android 8 "Oreo" hack to make media buttons work
        MediaPlayer mMediaPlayer = MediaPlayer.create(mContext, R.raw.silent_sound);
        mMediaPlayer.setOnCompletionListener(MediaPlayer::release);
        mMediaPlayer.start();
    } catch (Exception ignored) {
    }
}
 
Example 5
Source File: EntityFragment.java    From homeassist with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = getArguments();
    mGroup = CommonUtil.inflate(bundle.getString("group"), Group.class);

    setHasOptionsMenu(true);

    mClickDown = MediaPlayer.create(getContext(), R.raw.geek_click_down);
    mClickUp = MediaPlayer.create(getContext(), R.raw.geek_click_up);
    mClickDown.setVolume(0.2f, 0.2f);
    mClickUp.setVolume(0.2f, 0.2f);
}
 
Example 6
Source File: MonitorVolumeKeyPress.java    From watchpresenter with Apache License 2.0 5 votes vote down vote up
public void onCreate(){
    super.onCreate();
    settings = getSharedPreferences(Constants.SETTINGS_NAME,MODE_PRIVATE);
    vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
    Log.d(LOGCAT, "Service Started!");
    objPlayer = MediaPlayer.create(this, com.zuluindia.watchpresenter.R.raw.silence);
    objPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
    objPlayer.setLooping(true);
    timer = new Timer();
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    midVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)/2;
    resetVolume();
}
 
Example 7
Source File: TypeTextView.java    From BookReader with Apache License 2.0 5 votes vote down vote up
private void playAudio(int audioResId) {
    try {
        stopAudio();
        mMediaPlayer = MediaPlayer.create(mContext, audioResId);
        mMediaPlayer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: AudioCue.java    From speechutils with Apache License 2.0 5 votes vote down vote up
private boolean playSound(int sound) {
    MediaPlayer mp = MediaPlayer.create(mContext, sound);
    // create can return null, e.g. on Android Wear
    if (mp == null) {
        return false;
    }
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mp.setOnCompletionListener(MediaPlayer::release);
    mp.start();
    return true;
}
 
Example 9
Source File: SoundNotifier.java    From SmoothClicker with MIT License 5 votes vote down vote up
/**
 * Makes the device play a sound / a ring
 */
public void ring( ){
    MediaPlayer mp = MediaPlayer.create(mContext, R.raw.new_click);
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mp.setLooping(false);
    mp.start();
}
 
Example 10
Source File: SoundPullEventListener.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private void playSound(int resId) {
	// Stop current player, if there's one playing
	if (null != mCurrentMediaPlayer) {
		mCurrentMediaPlayer.stop();
		mCurrentMediaPlayer.release();
	}

	mCurrentMediaPlayer = MediaPlayer.create(mContext, resId);
	if (null != mCurrentMediaPlayer) {
		mCurrentMediaPlayer.start();
	}
}
 
Example 11
Source File: SFXManager.java    From FixMath with Apache License 2.0 5 votes vote down vote up
public void NewBackgroundPlay(){
    if(!isMuteSFX) {
        newBackground = MediaPlayer.create(context, R.raw.new_background_sfx);
        newBackground.setOnCompletionListener(this);
        newBackground.setOnPreparedListener(this);

    }
}
 
Example 12
Source File: Sonido.java    From android with GNU General Public License v2.0 5 votes vote down vote up
public void onClick(View view) {
	
	int recursoId = 0;
	
	Toast.makeText(this, spCanciones.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
	
	// Asigna el id del recurso de sonido seleccionado
	if (spCanciones.getSelectedItem().toString().equals("thunder")) {
		recursoId = R.raw.track1;
	}
	else {
		recursoId = R.raw.track2;
	}
	
	switch (view.getId()) {
	case R.id.btPlay:
		// Reproduce el sonido
		mp = MediaPlayer.create(this, recursoId);
		mp.setLooping(true);
		mp.start();
		break;
	case R.id.btStop:
		// Detiene la reproducci�n en curso
		if (mp != null) {
			mp.stop();
			mp.release();
			mp = null;
		}
		break;
	default:
		break;
	}
}
 
Example 13
Source File: Game.java    From FlappyCow with MIT License 5 votes vote down vote up
/**
 * Initializes the player with the nyan cat song
 * and sets the position to 0.
 */
public void initMusicPlayer(){
    if(musicPlayer == null){
        // to avoid unnecessary reinitialisation
        musicPlayer = MediaPlayer.create(this, R.raw.nyan_cat_theme);
        musicPlayer.setLooping(true);
        musicPlayer.setVolume(MainActivity.volume, MainActivity.volume);
    }
    musicPlayer.seekTo(0);    // Reset song to position 0
}
 
Example 14
Source File: TypeTextView.java    From fangzhuishushenqi with Apache License 2.0 5 votes vote down vote up
private void playAudio(int audioResId) {
    try {
        stopAudio();
        mMediaPlayer = MediaPlayer.create(mContext, audioResId);
        mMediaPlayer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static synchronized MediaPlayer playSoundUri(String soundUri) {
    try {
        JoH.getWakeLock("joh-playsound", 10000);
        final MediaPlayer player = MediaPlayer.create(xdrip.getAppContext(), Uri.parse(soundUri));
        player.setLooping(false);
        player.start();
        return player;
    } catch (Exception e) {
        Log.wtf(TAG, "Failed to play audio: " + soundUri + " exception:" + e);
        return null;
    }
}
 
Example 16
Source File: BackGroundMusicService.java    From android2048 with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    //建立音频播放
    player = MediaPlayer.create(this, R.raw.login_back);

    //监听音乐是否播放完成
    player.setOnCompletionListener(this);
}
 
Example 17
Source File: MainActivity.java    From CatchPiggy with GNU General Public License v3.0 4 votes vote down vote up
protected void initView() {
    LogUtil.setDebugOn(true);
    LogUtil.setIsShowClassName(false);

    mRootView = findViewById(R.id.root_view);
    mLoadingView = findViewById(R.id.loading_view);
    mHomeView = findViewById(R.id.home_view);
    mHomeView.setOnButtonClickListener(new HomeView.OnButtonClickListener() {
        @Override
        public void onPigstyModeButtonClicked() {
            showFixPigstyModeLevelSelectView();
        }

        @Override
        public void onClassicModeButtonClicked() {
            showClassicModeLevelSelectView();
        }

        @Override
        public boolean onSoundButtonClicked() {
            //控制背景音乐 静音和恢复
            boolean isMusicStopped = false;
            if (mPlayer != null) {
                if (mPlayer.isPlaying()) {
                    mPlayer.pause();
                    isMusicStopped = true;
                } else {
                    mPlayer.start();
                }
            }
            return isMusicStopped;
        }

        @Override
        public void onExitButtonClicked() {
            onBackPressed();
        }
    });
    //初始化背景音乐
    mPlayer = MediaPlayer.create(this, R.raw.background_music);
    mPlayer.setOnPreparedListener(MediaPlayer::start);
    mPlayer.setLooping(true);
    mPlayer.setOnErrorListener((mp, what, extra) -> {
        mPlayer = null;
        return false;
    });
}
 
Example 18
Source File: DetailChannelActivity.java    From TDTChannels-APP with MIT License 4 votes vote down vote up
private void loadRadio(String streamURL) {
    Toast.makeText(this, getString(R.string.channel_detail_reproducing_radio), Toast.LENGTH_SHORT).show();
    mediaPlayer = MediaPlayer.create(this, Uri.parse(streamURL));
    mediaPlayer.start();
}
 
Example 19
Source File: MyPreferenceFragment.java    From AnotherRSS with The Unlicense 4 votes vote down vote up
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
    if (s.equals("nightmode_use")) {
        boolean night = sharedPreferences.getBoolean("nightmode_use", false);
        int startH = sharedPreferences.getInt("nightmode_use_start", AnotherRSS.Config.DEFAULT_NIGHT_START);
        int stopH = sharedPreferences.getInt("nightmode_use_stop", AnotherRSS.Config.DEFAULT_NIGHT_STOP);
        if (night && AnotherRSS.inTimeSpan(startH, stopH)) {
            umm.setNightMode(UiModeManager.MODE_NIGHT_YES);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            umm.setNightMode(UiModeManager.MODE_NIGHT_NO);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().recreate();
        }
    } else if (s.equals("notify_sound")) {
        int noteSnd = Integer.parseInt(
                getPreferenceManager().getSharedPreferences().getString(
                        "notify_sound",
                        AnotherRSS.Config.DEFAULT_notifySound
                )
        );
        switch (noteSnd) {
            case 1:
                Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                mp = MediaPlayer.create(getActivity(), sound);
                break;
            case 2:
                mp = MediaPlayer.create(getActivity(), R.raw.notifysnd);
                break;
            case 3:
                mp = MediaPlayer.create(getActivity(), R.raw.dideldoing);
                break;
            case 4:
                mp = MediaPlayer.create(getActivity(), R.raw.doding);
                break;
            case 5:
                mp = MediaPlayer.create(getActivity(), R.raw.ploing);
                break;
            default:
                break;
        }
        if (mp != null && !mp.isPlaying()) {
            mp.start();
        }
    }
}
 
Example 20
Source File: Sound.java    From Tick with MIT License 4 votes vote down vote up
private void setNextMediaPlayer() {
    mNextPlayer = MediaPlayer.create(mContext, R.raw.ticking);
    mMediaPlayer.setNextMediaPlayer(mNextPlayer);
    mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
}