I am creating an android app with java in android studio. It is an application that displays tweets around 10 km when you tap google map. problem I want to display a video when displaying the acquired tweets in Listview. ・ The video is displayed even though it is not the corresponding tweet ・ Drops when displaying a video -The video does not play even if the list view is returned. The problem arises. I'm playing a video using SurfaceView and media player. Thank you for the solution.
The following 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"); // Exclude word String strLong = intent.getStringExtra ("strLong"); // Specify account 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);
// Make the Window transparent getWindow().setFormat(PixelFormat.TRANSPARENT); // Set a callback for SurfaceView 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() {
/ ** Called when SurfaceView is created * / @Override public void surfaceCreated(SurfaceHolder holder) { // public final String path = "file path or URL"; try {
// Generate Media Player mMediaPlayer = new MediaPlayer(); // Load the video file into Media Player // Log.d("mediaplayertest",path1);
mMediaPlayer.setDataSource(path1);
// Display the loaded video file on the screen 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) {
}
/ ** Called when SurfaceView is closed * / @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mMediaPlayer != null) { mMediaPlayer.release(); mMediaPlayer = null;
mSurfaceView.setVisibility(INVISIBLE);
path1 = null;
}
}
};
}
Recommended Posts