gpt4 book ai didi

android - RecyclerView + MediaPlayer + 切换按钮 + 字符串 Uri

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:18:22 24 4
gpt4 key购买 nike

背景:我正在开发一款健身应用。到目前为止一切正常,但是当我在 android 中处理音频文件 MediaPlayer 时出现了问题。

我检查了资源并找到了 ListView 但在 RecyclerView + MediaPlayer 上找不到任何内容。

我想知道如何在使用 RecyclerView + Toggle Button + String Uri (Offline - Raw folder) 时让它工作

问题:现在它在每次点击事件时播放第一个 .mp3 文件(对于英语:R.raw.sample_one_eng 播放,对于印地语:R.raw.sample_one_hindi 播放)。我认为它没有考虑到 int position 。

稍后,我想把它放到网上(可能是谷歌云),因为音频文件 (.mp3) 使我的应用程序变得非常沉重。对此的任何想法也将不胜感激(快速缓冲等)。

ListExercises.java

public class ListExercises extends AppCompatActivity {

List<ExerciseAudio> exerciseList = new ArrayList<>();
RecyclerView.LayoutManager layoutManager;
RecyclerView recyclerView;
RecyclerViewAdapterAud adapter;
PlayClickHandler clickHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_exercises);

initData();

recyclerView = (RecyclerView) findViewById(R.id.list_ex);
adapter = new RecyclerViewAdapterAud(exerciseList, getBaseContext());
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}

private void initData() {

exerciseList.add(new ExerciseAudio(R.drawable.sample_one, "Sample Exercise One", "Sans One",
R.raw.sample_one_eng,
R.raw.sample_one_hindi));

exerciseList.add(new ExerciseAudio(R.drawable.sample_two, "Sample Exercise Two", "Sans Two",
R.raw.sample_two_eng,
R.raw.sample_two_hindi));

exerciseList.add(new ExerciseAudio(R.drawable.sample_three, "Sample Exercise Three", "Sans Three",
R.raw.sample_three_eng,
R.raw.sample_three_hindi));

exerciseList.add(new ExerciseAudio(R.drawable.sample_four, "Sample Exercise Four", "Sans Four",
R.raw.sample_four_eng,
R.raw.sample_four_hindi));
}
}

ViewExercise.java

public class ViewExercise extends AppCompatActivity {

int image_id, eng_aud_url, hindi_aud_url;
String name, sans_name;

ArrayList<String> arrayList = new ArrayList<>();

TextView timer, title, sansName;
ImageView detail_image;
ToggleButton tg_btn_speaker_eng, tg_btn_speaker_hindi;

MediaPlayer mp;

int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_exercise);

Field[] field = R.raw.class.getFields();
for (int i = 0; i < field.length; i++){
arrayList.add(field[i].getName());
}

title = (TextView) findViewById(R.id.title);
sansName = (TextView) findViewById(R.id.sans_name);
detail_image = (ImageView) findViewById(R.id.detail_image);

tg_btn_speaker_eng = (ToggleButton) findViewById(R.id.tg_btn_speaker_eng);
tg_btn_speaker_hindi = (ToggleButton) findViewById(R.id.tg_btn_speaker_hindi);

tg_btn_speaker_eng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//toggleEnglish(tg_btn_speaker_eng.isChecked());
int positionEng = 0;
playSongEng(positionEng);
}
});

tg_btn_speaker_hindi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//toggleHindi(tg_btn_speaker_hindi.isChecked());
int positionHindi = 0;
playSongHindi(positionHindi);
}
});

if (getIntent() != null) {
image_id = getIntent().getIntExtra("image_id", -1);
name = getIntent().getStringExtra("name");
sans_name = getIntent().getStringExtra("sanskrit_name");

detail_image.setImageResource(image_id);
title.setText(name);
sansName.setText(sans_name);
}
}


public void playSongEng(int i) {
//mp.reset();
int resId = getResources().getIdentifier(arrayList.get(i), "raw", getPackageName());
mp = MediaPlayer.create(getApplicationContext(), resId);
mp.start();
}

public void playSongHindi(int i) {
//mp.reset();
int resId = getResources().getIdentifier(arrayList.get(i), "raw", getPackageName());
mp = MediaPlayer.create(getApplicationContext(), resId);
mp.start();
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mp != null)
mp.release();
}
}

这是它的样子!

编辑(以避免混淆):刚刚注意到,该图像名称应该是List Exercises 而不是 View Exercises,它对应于 ListExercises.java

List Exercise - RecyclerView

而这个对应的是ViewExercise.java

View Exercise

谢谢!

最佳答案

试试这个代码..将以下依赖项添加到应用程序级别的 gradle 文件中..

    compile 'com.google.android.exoplayer:exoplayer:r2.3.0'

之后使用下面的代码..

public class VideoPlayerActivity extends AppCompatActivity implements ExoPlayer.EventListener {

private SimpleExoPlayer mSimpleExoPlayer;

private SimpleExoPlayerView mSimpleExoPlayerView;

private Handler mMainHandler;
private AdaptiveTrackSelection.Factory mAdaptiveTrackSelectionFactory;
private TrackSelector mTrackSelector;
private LoadControl mLoadControl;
private DefaultBandwidthMeter mBandwidthMeter;
private DataSource.Factory mDataSourceFactory;
private SimpleCache mSimpleCache;
private DataSource.Factory mFactory;
private MediaSource mVideoSource;
private LoopingMediaSource mLoopingMediaSource;
private ProgressBar mProgressBar;
private String videoUrl="http://sample.vodobox.net/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
mSimpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.videoPlayer);
mProgressBar = (ProgressBar) findViewById(R.id.amPrgbrLoading);
}

/**
* this method play audio and video with hls streaming.
*/
private void playMedia() {
mMainHandler = new Handler();
mBandwidthMeter = new DefaultBandwidthMeter();
mAdaptiveTrackSelectionFactory = new AdaptiveTrackSelection.Factory(mBandwidthMeter);
mTrackSelector = new DefaultTrackSelector(mAdaptiveTrackSelectionFactory);

mLoadControl = new DefaultLoadControl();
mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, mTrackSelector, mLoadControl);

mSimpleExoPlayerView.setPlayer(mSimpleExoPlayer);
mSimpleExoPlayerView.setControllerVisibilityListener(new PlaybackControlView.VisibilityListener() {
@Override
public void onVisibilityChange(int visibility) {
mSimpleExoPlayerView.showController();
}
});
mDataSourceFactory = new DefaultDataSourceFactory(this,Util.getUserAgent(this, "com.exoplayerdemo"), mBandwidthMeter);
mSimpleCache = new SimpleCache(this.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10));
mFactory = new CacheDataSourceFactory(mSimpleCache, mDataSourceFactory,0);
mVideoSource = new HlsMediaSource(Uri.parse(videoUrl),
mFactory, mMainHandler, new AdaptiveMediaSourceEventListener() {
@Override
public void onLoadStarted(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs) {
//Toast.makeText(VideoPlayerActivity.this, "Load Started", Toast.LENGTH_SHORT).show();
}

@Override
public void onLoadCompleted(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded) {
//Toast.makeText(VideoPlayerActivity.this, "Load Completed", Toast.LENGTH_SHORT).show();
}

@Override
public void onLoadCanceled(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded) {

}

@Override
public void onLoadError(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded, IOException error, boolean wasCanceled) {
videoUrl=""; // define second url
}

@Override
public void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long mediaEndTimeMs) {
// Toast.makeText(VideoPlayerActivity.this, "Up stream", Toast.LENGTH_SHORT).show();
}

@Override
public void onDownstreamFormatChanged(int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaTimeMs) {
// Toast.makeText(VideoPlayerActivity.this, "Down Stream", Toast.LENGTH_SHORT).show();
}
});

mLoopingMediaSource = new LoopingMediaSource(mVideoSource);
mSimpleExoPlayer.prepare(mLoopingMediaSource);
mSimpleExoPlayer.setPlayWhenReady(true);
mSimpleExoPlayer.addListener(new ExoPlayer.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {

}

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

}

@Override
public void onLoadingChanged(boolean isLoading) {

}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

if (playbackState == ExoPlayer.STATE_BUFFERING) {
mProgressBar.setVisibility(View.VISIBLE);
} else {
mProgressBar.setVisibility(View.GONE);
}

}

@Override
public void onPlayerError(ExoPlaybackException error) {

}

@Override
public void onPositionDiscontinuity() {

}
});



}

@Override
protected void onResume() {
super.onResume();
playMedia();
}

@Override
protected void onStop() {
super.onStop();
stopMedia();
}

@Override
protected void onPause() {
super.onPause();
stopMedia();
}

private void stopMedia() {
mSimpleExoPlayer.stop();
mSimpleExoPlayer.release();
}



@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {

}

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

}

@Override
public void onLoadingChanged(boolean isLoading) {

}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

}

@Override
public void onPlayerError(ExoPlaybackException error) {

}

@Override
public void onPositionDiscontinuity() {

}

在这段用于视频的代码中,您可以传递您的音频文件路径..

xml代码..

        <com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

关于android - RecyclerView + MediaPlayer + 切换按钮 + 字符串 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com