这是音频播放器
外观:Click and see how it actually looks :)
问题是:
当我打开此活动并点击随机播放的歌曲时->它会播放。我可以回去,再回去,玩其他东西,停下来,回去,再玩,...工作
但是,当我第一次打开此活动并且没有按下任何播放按钮时...而是我返回(返回按钮)并再次转到该活动,它将无法正常工作。现在点击任何按钮都会做出反应(吐司,单击,播放按钮将消失,并且会出现旋转的圆圈,表明它正在加载...),但音乐不会出现。选择其他歌曲也无济于事。一次又一次地进行此活动->没什么,就行不通。
The toast in onClick works always, and always the right way. So...
它的工作原理是:首次打开此活动时,必须至少单击歌曲上的一个按钮!你必须!因为如果您不这样做,无论如何我都不会工作。好的,只有在您关闭并重新启动应用程序后,我才能工作。
那怎么可能?尤其是当单击始终有效并且以正确的方式工作时?
就像第一次单击一样,看到活动非常重要。但这只是
jcplayerView.kill();
ArrayList<JcAudio> jcAudios = new ArrayList<>();
jcAudios.add(JcAudio.createFromURL(
(String) podcasts.keySet().toArray()[position],
(String) podcasts.values().toArray()[position]
));
jcplayerView.initPlaylist(jcAudios, null);
jcplayerView.continueAudio();
这里没有什么特别的让我了解为什么这样做的原因。
package com.sherdle.universal;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.JsonReader;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.arges.sepan.argmusicplayer.Enums.AudioType;
import com.arges.sepan.argmusicplayer.IndependentClasses.ArgAudio;
import com.arges.sepan.argmusicplayer.PlayerViews.ArgPlayerFullScreenView;
import com.arges.sepan.argmusicplayer.PlayerViews.ArgPlayerLargeView;
import com.arges.sepan.argmusicplayer.PlayerViews.ArgPlayerSmallView;
import com.example.jean.jcplayer.model.JcAudio;
import com.example.jean.jcplayer.view.JcPlayerView;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class Argplayer2Activity extends AppCompatActivity {
protected JcPlayerView jcplayerView;
protected JcPlayerView jcplayerView2;
public HashMap<String, String> podcasts;
public static int launchesTimes = 1;
public static HashMap<String, String> podcasts2;
public Context context;
protected void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.argplayer2);
podcasts = new HashMap<>();
jcplayerView = (JcPlayerView) findViewById(R.id.jcplayer);
if (launchesTimes > 1) {
ViewGroup parent = (ViewGroup) jcplayerView.getParent();
int indexOldPlayer = parent.indexOfChild(jcplayerView);
parent.removeView(jcplayerView);
jcplayerView2 = MeTest.jcPlayerView;
((ViewGroup) jcplayerView2.getParent()).removeView(jcplayerView2);
parent.addView(jcplayerView2, indexOldPlayer);
jcplayerView = jcplayerView2;
}
if (launchesTimes == launchesTimes) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
String user = getString(R.string.podcast_user);
String server = getString(R.string.podcast_server);
URL fastcastProxy = new URL("https://xxxxxxxxxxxx/proxy.php?user=" + user + "&server=" + server);
HttpsURLConnection connection = (HttpsURLConnection) fastcastProxy.openConnection();
if (connection.getResponseCode() == 200) {
InputStream body = connection.getInputStream();
InputStreamReader bodyReader = new InputStreamReader(body, "UTF-8");
JsonReader jsonReader = new JsonReader(bodyReader);
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String key = jsonReader.nextName();
String value = jsonReader.nextString();
podcasts.put(key, value);
}
jsonReader.close();
connection.disconnect();
runOnUiThread(new Runnable() {
@Override
public void run() {
ListView podcastlistView = (ListView) findViewById(R.id.podcastListView);
PodcastAdapter podcastAdapter = new PodcastAdapter();
podcastlistView.setAdapter(podcastAdapter);
}
});
}
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
});
podcasts2 = podcasts;
} else {
podcasts = podcasts2;
ListView podcastlistView = (ListView) findViewById(R.id.podcastListView);
PodcastAdapter podcastAdapter = new PodcastAdapter();
podcastlistView.setAdapter(podcastAdapter);
}
launchesTimes++;
}
@Override
protected void onDestroy() {
MeTest.setString("destroyed");
MeTest.jcPlayerView = jcplayerView;
super.onDestroy();
}
public class PodcastAdapter extends BaseAdapter {
@Override
public int getCount() {
return podcasts.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.podcast_layout, null);
TextView title = (TextView) view.findViewById(R.id.podcastTitle);
ImageView playButton = (ImageView) view.findViewById(R.id.playButtonplay);
title.setText((String) podcasts.keySet().toArray()[i]);
playButton.setOnClickListener(new PlayOnClickEventListener(i));
return view;
}
public class PlayOnClickEventListener implements View.OnClickListener {
int position;
public PlayOnClickEventListener(int position) {
this.position = position;
}
@Override
public void onClick(View view) {
jcplayerView.kill();
ArrayList<JcAudio> jcAudios = new ArrayList<>();
jcAudios.add(JcAudio.createFromURL(
(String) podcasts.keySet().toArray()[position],
(String) podcasts.values().toArray()[position]
));
jcplayerView.initPlaylist(jcAudios, null);
jcplayerView.continueAudio();
Toast.makeText(context, (String) podcasts.keySet().toArray()[position], Toast.LENGTH_LONG).show();
}
}
}
}
有什么想法吗?意见?有什么建议吗?请帮助:D。