Ich erstelle eine Android App mit Java in Android Studio. Es ist eine Anwendung, die Tweets etwa 10 km anzeigt, wenn Sie auf Google Map tippen. Problem Ich möchte ein Video anzeigen, wenn die erfassten Tweets in Listview angezeigt werden. ・ Das Video wird angezeigt, obwohl es nicht der entsprechende Tweet ist ・ Wird beim Anzeigen eines Videos gelöscht -Das Video wird nicht abgespielt, auch wenn die Listenansicht zurückgegeben wird. Das Problem entsteht. Ich spiele ein Video mit SurfaceView und Mediaplayer ab. Vielen Dank für die Lösung.
Der folgende Code public class MapsSubActivity extends ListActivity {
private TweetAdapter01 mAdapter;
private Twitter mTwitter;
public SurfaceView mSurfaceView;
public SurfaceHolder mHolder;
public MediaPlayer mMediaPlayer;
public String path1 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new TweetAdapter01(this);
reloadTimeLine();
setListAdapter(mAdapter);
}
private void reloadTimeLine() {
AsyncTask<Void, Void, List<Status>> task = new AsyncTask<Void, Void, List<twitter4j.Status>>() {
//経度緯度の受け渡し Intent intent = getIntent(); String strLat = intent.getStringExtra ("strLat"); // Ausgeschlossenes Wort String strLong = intent.getStringExtra ("strLong"); // Konto angeben double dLat = Double.parseDouble(strLat); double dLong = Double.parseDouble(strLong);
public final String API_KEY = "XXXXXXX";
public final String API_SECRET = "XXXXXXX";
public final String ACCESS_TOKEN = "XXXXXXX";
public final String ACCESS_TOKEN_SECRET = "XXXXXXX";
@Override
protected List<twitter4j.Status> doInBackground(Void... params) {
try {
String strQuery = null;
Twitter tw = new TwitterFactory().getInstance();
tw.setOAuthConsumer(API_KEY, API_SECRET);
AccessToken at = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
tw.setOAuthAccessToken(at);
ArrayList<twitter4j.Status> list = new ArrayList<twitter4j.Status>();
Query query = new Query();
query.setCount(100);
GeoLocation geo = new GeoLocation(dLat, dLong);
query.setGeoCode(geo, 10.0, Query.KILOMETERS);
query.setQuery(strQuery);
QueryResult result = tw.search(query);
String str = String.valueOf(result.getCount());
for (twitter4j.Status tweet : result.getTweets()) {
list.add(tweet);
}
if (result.hasNext()) {
query = result.nextQuery();
}
return list;
} catch(TwitterException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<twitter4j.Status> result) {
if (result != null) {
mAdapter.clear();
for (twitter4j.Status status : result) {
mAdapter.add(status);
Log.d("testE","zoku");
}
getListView().setSelection(0);
} else {
}
}
};
task.execute();
}
private class TweetAdapter01 extends ArrayAdapter<Status> {
private LayoutInflater mInflater;
public TweetAdapter01(Context context) {
super(context, android.R.
layout.simple_list_item_1);
mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(R.layout.list_item_searchsub, null);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_searchsub, null);
}
Status item = getItem(position);
SmartImageView icon = (SmartImageView) convertView.findViewById(R.id.icon);
icon.setImageUrl(item.getUser().getProfileImageURL());
Log.d("testItem",item.getUser().getProfileImageURL());
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(item.getUser().getName());
TextView screenName = (TextView) convertView.findViewById(R.id.screen_name);
screenName.setText("@" + item.getUser().getScreenName());
TextView text = (TextView) convertView.findViewById(R.id.text);
text.setText(item.getText());
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
SmartImageView image1 = (SmartImageView) convertView.findViewById(R.id.image1);
SmartImageView image2 = (SmartImageView) convertView.findViewById(R.id.image2);
SmartImageView image3 = (SmartImageView) convertView.findViewById(R.id.image3);
SmartImageView image4 = (SmartImageView) convertView.findViewById(R.id.image4);
image1.setVisibility(INVISIBLE);
image2.setVisibility(INVISIBLE);
image3.setVisibility(INVISIBLE);
image4.setVisibility(INVISIBLE);
ExtendedMediaEntity[] mediaEntitys = item.getExtendedMediaEntities();
List list = new ArrayList();
for( int i = 0; i < mediaEntitys.length; i ++ ){
ExtendedMediaEntity extendedMediaEntity = mediaEntitys[i];
ExtendedMediaEntity.Variant[] variant = extendedMediaEntity.getVideoVariants();
if (extendedMediaEntity.getType().equals("video")) {
list.add(variant[i].getUrl());
}
else{list.add(extendedMediaEntity.getMediaURLHttps());}
}
String[] medias = ( String[] )list.toArray( new String[0] );
for (int i=0;i < medias.length;i++) {
if(i==0) {
if (medias[i].contains("video")) {
mSurfaceView.setVisibility(VISIBLE);
// Fenster transparent machen getWindow().setFormat(PixelFormat.TRANSPARENT); // Rückruf auf SurfaceView setzen path1 = null; path1 = medias[i]; mHolder = mSurfaceView.getHolder(); mHolder.addCallback(mCallback);
}
//
else {
image1.setVisibility(VISIBLE);
image1.setImageUrl(medias[i]);
Log.d("test201804", medias[i]);
}
}
if(i==1){
image2.setVisibility(VISIBLE);
image2.setImageUrl(medias[i]);
Log.d("test201804",medias[i]);
}
if(i==2){
image3.setVisibility(VISIBLE);
image3.setImageUrl(medias[i]);
Log.d("test201804",medias[i]);
}
if(i==3){
image4.setVisibility(VISIBLE);
image4.setImageUrl(medias[i]);
Log.d("test201804",medias[i]);
}
}
return convertView;
}
}
private final SurfaceHolder.Callback mCallback = new SurfaceHolder.Callback() {
/ ** Wird aufgerufen, wenn SurfaceView erstellt wird * / @Override public void surfaceCreated(SurfaceHolder holder) { // public final String path = "Dateipfad oder URL"; try {
// Media Player generieren mMediaPlayer = new MediaPlayer(); // Laden Sie die Videodatei in Media Player // Log.d("mediaplayertest",path1);
mMediaPlayer.setDataSource(path1);
// Die geladene Videodatei auf dem Bildschirm anzeigen mMediaPlayer.setDisplay(holder); mMediaPlayer.prepare(); mMediaPlayer.setOnPreparedListener(new MediaPlayer. OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mMediaPlayer.start(); } }); // mMediaPlayer.start(); } catch (IllegalArgumentException e) { e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
//
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/ ** Wird aufgerufen, wenn SurfaceView geschlossen ist * / @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mMediaPlayer != null) { mMediaPlayer.release(); mMediaPlayer = null;
mSurfaceView.setVisibility(INVISIBLE);
path1 = null;
}
}
};
}
Recommended Posts