當前位置: 首頁>>代碼示例>>Java>>正文


Java MediaPlayer.setDataSource方法代碼示例

本文整理匯總了Java中android.media.MediaPlayer.setDataSource方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaPlayer.setDataSource方法的具體用法?Java MediaPlayer.setDataSource怎麽用?Java MediaPlayer.setDataSource使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.media.MediaPlayer的用法示例。


在下文中一共展示了MediaPlayer.setDataSource方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: initializeMediaPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
private void initializeMediaPlayer() {
    _logger.Debug("initializeMediaPlayer");

    _radioPlayer = new MediaPlayer();

    try {
        _radioPlayer.setDataSource(_radioStream.GetUrl());
        _logger.Debug(String.format(Locale.getDefault(), "Set DataSource to %s", _radioStream.GetUrl()));
    } catch (Exception e) {
        _logger.Error(e.toString());
        Toasty.error(_context, "An error appeared settings url for radio player!", Toast.LENGTH_LONG).show();
    }

    _radioPlayer.setOnBufferingUpdateListener((mediaPlayer, percent) -> _logger.Info(String.format(Locale.getDefault(), "Buffered to %d%%", percent)));
    _radioPlayer.setOnPreparedListener(mediaPlayer -> {
        _logger.Debug("onPreparedListener...");
        _radioPlayer.start();
    });
}
 
開發者ID:GuepardoApps,項目名稱:LucaHome-MediaServer,代碼行數:20,代碼來源:CenterViewController.java

示例2: prepareMediaPlayerFromPoint

import android.media.MediaPlayer; //導入方法依賴的package包/類
private void prepareMediaPlayerFromPoint(int progress) {
  //set mediaPlayer to start from middle of the audio file

  mMediaPlayer = new MediaPlayer();

  try {
    mMediaPlayer.setDataSource(item.getFilePath());
    mMediaPlayer.prepare();
    mSeekBar.setMax(mMediaPlayer.getDuration());
    mMediaPlayer.seekTo(progress);

    mMediaPlayer.setOnCompletionListener(mp -> stopPlaying());
  } catch (IOException e) {
    Log.e(LOG_TAG, "prepare() failed");
  }

  //keep screen on while playing audio
  getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
 
開發者ID:Arjun-sna,項目名稱:Android-AudioRecorder-App,代碼行數:20,代碼來源:PlaybackFragment.java

示例3: buildMediaPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
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;
	}
}
 
開發者ID:TonnyL,項目名稱:Espresso,代碼行數:22,代碼來源:BeepManager.java

示例4: buildMediaPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
private MediaPlayer buildMediaPlayer(Context activity) {
  MediaPlayer mediaPlayer = new MediaPlayer();
  mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  mediaPlayer.setOnCompletionListener(this);
  mediaPlayer.setOnErrorListener(this);

  AssetFileDescriptor file = activity.getResources().openRawResourceFd(keepass2android.plugin.qr.R.raw.beep);
  try {
    mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
    file.close();
    mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
    mediaPlayer.prepare();
  } catch (IOException ioe) {
    Log.w(TAG, ioe);
    mediaPlayer = null;
  }
  return mediaPlayer;
}
 
開發者ID:PhilippC,項目名稱:keepass2android,代碼行數:19,代碼來源:BeepManager.java

示例5: onPlayClick

import android.media.MediaPlayer; //導入方法依賴的package包/類
public void onPlayClick(View v) {
    if (!mPlaying) {
        try {
            mPlayer = new MediaPlayer();
            AssetFileDescriptor fd = getResources().openRawResourceFd(R.raw.space);
            mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            mPlayer.setOnCompletionListener(mOnCompletionListener);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.prepare();
            mPlayer.start();
            mPlaying = true;
            setState(STATE_PLAYING);
        } catch (IOException e) {
            Log.e(TAG, "error trying to play a sound");
        }
    } else {
        mPlayer.setOnCompletionListener(null);
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
        mPlaying = false;
        setState(STATE_IDLE);
    }
}
 
開發者ID:nasa,項目名稱:astrobee_android,代碼行數:25,代碼來源:MainActivity.java

示例6: buildMediaPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
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;
  }
}
 
開發者ID:yun2win,項目名稱:tvConnect_android,代碼行數:23,代碼來源:BeepManager.java

示例7: startPlaying

import android.media.MediaPlayer; //導入方法依賴的package包/類
public void startPlaying() {
    mPlayer = new MediaPlayer();
    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Log.d("VOICE", "Ended");
            playingRecord = false;
            if (listener != null) {
                listener.onCompleted();
            }
        }
    });
    try {
        mPlayer.setDataSource(mFileFullName);
        mPlayer.prepare();
        mPlayer.start();
    } catch (Exception e) {
        Log.e(TAG, "prepare() failed");
    }
}
 
開發者ID:WorldBank-Transport,項目名稱:RoadLab-Pro,代碼行數:21,代碼來源:VoiceRecordPlayer.java

示例8: buildMediaPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
private MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    try {
        int rid = activity.getResources().getIdentifier("beep", "raw", activity.getPackageName());
        if (rid == 0) {
            return null;
        }
        AssetFileDescriptor file = activity.getResources().openRawResourceFd(rid);
        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;
    }
}
 
開發者ID:snice,項目名稱:androidscan,代碼行數:26,代碼來源:BeepManager.java

示例9: onPlaybackClick

import android.media.MediaPlayer; //導入方法依賴的package包/類
public void onPlaybackClick(View v) {
    File f = new File(getExternalFilesDir(null), "recording.mp4");

    if (!mPlaying) {
        try {
            mPlayer = new MediaPlayer();
            mPlayer.setDataSource(f.getAbsolutePath());
            mPlayer.setOnCompletionListener(mOnCompletionListener);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.prepare();
            mPlayer.start();
            mPlaying = true;
            setState(STATE_PLAYING_BACK);
        } catch (IOException e) {
            Log.e(TAG, "error trying to playback recording");
        }
    } else {
        mPlayer.setOnCompletionListener(null);
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;

        mPlaying = false;
        setState(STATE_IDLE);
    }
}
 
開發者ID:nasa,項目名稱:astrobee_android,代碼行數:27,代碼來源:MainActivity.java

示例10: createMusicFromFile

import android.media.MediaPlayer; //導入方法依賴的package包/類
public static Music createMusicFromFile(final MusicManager pMusicManager, final File pFile) throws IOException {
	final MediaPlayer mediaPlayer = new MediaPlayer();

	mediaPlayer.setDataSource(new FileInputStream(pFile).getFD());
	mediaPlayer.prepare();

	final Music music = new Music(pMusicManager, mediaPlayer);
	pMusicManager.add(music);

	return music;
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:12,代碼來源:MusicFactory.java

示例11: playSound

import android.media.MediaPlayer; //導入方法依賴的package包/類
private void playSound(Context context, Uri alert) {
    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (IOException e) {
        System.out.println("OOPS");
    }
}
 
開發者ID:akshah1997,項目名稱:KJSCE-Hackathon,代碼行數:16,代碼來源:AlarmReceiver.java

示例12: initializeMediaPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
private void initializeMediaPlayer() {
    _radioPlayer = new MediaPlayer();

    try {
        _radioPlayer.setDataSource(_radioStream.GetUrl());
    } catch (Exception e) {
        Logger.getInstance().Error(TAG, e.getMessage());
        Toasty.error(_context, "An error appeared settings url for radio player!", Toast.LENGTH_LONG).show();
    }

    _radioPlayer.setOnBufferingUpdateListener((mediaPlayer, percent) -> Logger.getInstance().Information(TAG, String.format(Locale.getDefault(), "Buffered to %d%%", percent)));
    _radioPlayer.setOnPreparedListener(mediaPlayer -> _radioPlayer.start());
}
 
開發者ID:GuepardoApps,項目名稱:LucaHome-AndroidApplication,代碼行數:14,代碼來源:CenterViewController.java

示例13: startPlayMusic

import android.media.MediaPlayer; //導入方法依賴的package包/類
/**
 * 開始播放音樂
 * @param path 音樂絕對路徑
 */
private void startPlayMusic(String path){
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnPreparedListener(preparedListener);
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.prepareAsync();
    }catch (Exception e) {
        LogUtils.error(e.toString());
    }
}
 
開發者ID:mainh,項目名稱:MainCalendar,代碼行數:16,代碼來源:FilePicker.java

示例14: onCreate

import android.media.MediaPlayer; //導入方法依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mediaPlayer = new MediaPlayer();
    filepath = getArguments().getString(FILEPATH);
    imageUrl = getArguments().getString(IMAGEURL);
    playState = false;
    try {
        mediaPlayer.setDataSource(filepath);
        mediaPlayer.prepare();
    } catch (IOException e) {
        Log.i("nib", filepath);
        ToastUtil.showShort(R.string.unable_to_play);
    }
}
 
開發者ID:hgs1217,項目名稱:Paper-Melody,代碼行數:16,代碼來源:ListenFragment.java

示例15: initAudioPlayer

import android.media.MediaPlayer; //導入方法依賴的package包/類
private void initAudioPlayer() throws IOException {
        audioPlay = new MediaPlayer();
//        audioPlay.setOnPreparedListener(prepareListener);
        audioPlay.setOnCompletionListener(endAudioListener);
        audioPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
        audioPlay.setDataSource(audioFilePath);
        audioPlay.prepare();
    }
 
開發者ID:gvsucis,項目名稱:mobile-app-dev-book,代碼行數:9,代碼來源:AudioActivity.java


注:本文中的android.media.MediaPlayer.setDataSource方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。