[JAVA] I have a question. I get an error when playing a video in Listview on android.

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

I have a question. I get an error when playing a video in Listview on android.
I get an error when adding a dependency
I found no way to get the error code when I received an exception on Android
I have a question about Android studio.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
I get a 404 error when testing forms authentication with Spring Security
I get a Ruby version error when I try to start Rails.
I got a Permission Denied error when I put Laravel in Docker
[Android] I want to get the listener from the button in ListView
[Visual Studio Code] I get a syntax error when debugging when using rbenv
I want to display an error message when registering in the database
I made a calculator app on Android
A reminder when an aapt.exe error occurs
When I renew the certificate with CircleCI × fastlane, I get an exit status: 65 error.
Beginner: I get an error on line 30. I don't know where to fix it.
(Capistrano) After deploying, I get a We're sorry… error in the production environment.
When using a list in Java, java.awt.List comes out and an error occurs
I got an error when I ran rake routes.
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
[Ruby on Rails] I get a warning when running RSpec because gem'chromedriver-helper' is deprecated.
What to do if you get a port error when docker-compose up on Mac
I got an error when trying to install sbt to build a Scala development environment
A memo when "I do not get a certificate error with a self-signed certificate using Java's Keytool"
I tried using a database connection in Android development
I get Mysql2 :: Error :: ConnectionError in the production environment
I got a cannot resolve symbol in Android Studio
I got an error "undefined method` create'" in RSpec
When I bcrypt with node + docker, I got an error
I made a library for displaying tutorials on Android.
What I learned when building a server in Java
I have a question about keyboard input of numbers
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
When I run docker-compose exec in crontab, I get "the input device is not a TTY"
[Android] Why a conflict version error occurs when loading some library on the Android app side
When starting Eclipse debug, I get a `ERROR: JDWP Transport dt_socket failed` error and cannot start.