跳过48帧!应用程序可能在其主线程上做了太多工作

时间:2018-01-04 15:52:47

标签: android android-mediaplayer

我在ListView中检索歌曲列表但是当应用程序在模拟器上运行时,屏幕为空白。我没有得到它只显示以下错误的歌曲列表。

  

musicmania E / RecyclerView:没有连接适配器;跳过布局01-04   05:33:07.534 28855-28855 / com.example.murarilal.musicmania

     

E / RecyclerView:没有连接适配器;跳过布局01-04   05:33:07.540 28855-28855 / com.example.murarilal.musicmania

     

I / Choreographer:跳过48帧!该应用程序可能也在做   在其主要线程上做了很多工作。 01-04 05:33:07.566   28855-28874 / com.example.murarilal.musicmania D / EGL_emulation:   eglMakeCurrent:0x9e2050c0:ver 2 0(tinfo 0x9e2032d0)01-04   05:33:07.903 28855-28862 / com.example.murarilal.musicmania W / zygote:

     

暂停所有线程:17.506ms

SongsTab活动

public class SongsTab extends Fragment {
    ArrayList<songInfo> _songs = new ArrayList<songInfo>();
    RecyclerView recyclerView;
    SeekBar seekBar;
    songAdapter songAdapter1;
    MediaPlayer mediaPlayer;
    Handler myHandler = new Handler();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.songs, container, false);
        recyclerView = (RecyclerView)v. findViewById(R.id.recyclerView);
        seekBar = (SeekBar) v.findViewById(R.id.seekBar);
        songAdapter1 = new songAdapter(getActivity(),_songs);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(songAdapter1);
        recyclerView.addItemDecoration(dividerItemDecoration);
        return v;
    }


    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadSongs();
        // add your code here which executes after the execution of onCreateView() method.
        songAdapter1.setOnItemClickListener(new songAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, View view, final songInfo obj, int position) {
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;

                }else {

                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mediaPlayer = new MediaPlayer();
                                mediaPlayer.setDataSource(obj.getSongUrl());
                                mediaPlayer.prepareAsync();
                                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mp) {
                                        mp.start();
                                        seekBar.setProgress(0);
                                        seekBar.setMax(mediaPlayer.getDuration());
                                        Log.d("Prog", "run: " + mediaPlayer.getDuration());
                                    }
                                });
                                Log.i("devesh","vikas");



                            }catch (Exception e){}
                        }

                    };
                    myHandler.postDelayed(runnable,100);

                }
            }
        });
        checkUserPermission();

        Thread t = new runThread();
        t.start();



    }
    public class runThread extends Thread {

        @Override
        public void run() {

            while (true) {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d("Runwa", "run: " + 1);
                if (mediaPlayer != null) {
                    seekBar.post(new Runnable() {
                        @Override
                        public void run() {
                            seekBar.setProgress(mediaPlayer.getCurrentPosition());
                        }
                    });

                    Log.d("Runwa", "run: " + mediaPlayer.getCurrentPosition());
                }
            }
        }

    }

    private void checkUserPermission(){
        if(Build.VERSION.SDK_INT>=23){
            if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED){
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},123);
                return;
            }
        }
       // loadSongs();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 123:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    loadSongs();
                }else{
                    Toast.makeText(getContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                    checkUserPermission();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        }

    }

    public void loadSongs(){

        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
        Cursor cursor = getContext().getContentResolver().query(uri,null,selection,null,null);
        if(cursor != null){
            if(cursor.moveToFirst()){
                do{
                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                    songInfo s = new songInfo(name,artist,url);
                    _songs.add(s);
                    songAdapter1.notifyDataSetChanged();
                    recyclerView.setAdapter(songAdapter1);
                }while (cursor.moveToNext());
            }

            cursor.close();

        }

    }
}

MainActivity



public class MainActivity extends AppCompatActivity {

    public ViewPagerAdapter pagerAdapter;
    private ViewPager mViewPager;
    private Toolbar toolbar;
    private TabLayout tabLayout;
    public static View rootView;
    public static int tabNo;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        mViewPager.setAdapter(pagerAdapter);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {


            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabNo = tab.getPosition();
                mViewPager.setCurrentItem(tabNo);//setting current selected item over viewpager

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();

            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
//              View rootView;

            switch (getArguments().getInt(ARG_SECTION_NUMBER))
            {
                case 1: {
                    rootView = inflater.inflate(R.layout.songs, container, false);

                    break;
                }
                case 2: {
                    rootView = inflater.inflate(R.layout.album, container, false);
                    break;
                }

                case 3: {
                    rootView = inflater.inflate(R.layout.genres, container, false);
                    break;
                }
                case 4: {
                    rootView = inflater.inflate(R.layout.artist, container, false);
                    break;
                }

            }
            return rootView;


        }

    }
}

2 个答案:

答案 0 :(得分:0)

你不能使用Thread.sleep,而是使用处理程序或倒计时器

答案 1 :(得分:0)

我相信loadSongs()是在主线程中发生的,它正在进行

songAdapter1.notifyDataSetChanged();
recyclerView.setAdapter(songAdapter1);

你不应该一次又一次地setAdapater而只是notifyDataSetChange