当前位置: 首页>>代码示例>>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;未经允许,请勿转载。