Help to fix errors of Audio music player !

  • Replies:2
José António Melo Moreira
  • Forum posts: 4

Feb 28, 2021, 11:31:18 AM via Website

Hello good morning for everyone I´m a beginner on the Android Studio, and i need a help for you to place well the app, that means working perfectly. It´s a good app, and it could serve me as a example to understand its funcionality.

I´m building an audio music player app., but on my MainActivity.java file ,

In the code i faced with some mistakes, see below the images that i sent you. How could you fix them?

MainActivity.java

package com.example.musicaudioplayer;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentPagerAdapter;

import androidx.viewpager.widget.ViewPager;

import android.content.Context;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import java.util.jar.Manifest;

public class MainActivity extends AppCompatActivity {

public static final int REQUEST_CODE=1;

ArrayList<MusicFiles>musicFiles;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    permission();

    initViewpager();

}


private void permission() {

    if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)

    != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{

                        Manifest.permission.WRITE_EXTERNAL_STORAGE}

                , REQUEST_CODE);

    }

            else

    {

        Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();

        musicFiles = getAllAudio(this);

    }

}

@Override

    public void onRequestPermissionsResult (int requestCode,@NonNull String[]permissions,

                                            @NonNull int[] grantResults){

    super.onRequestPermissionsResult(requestCode,permissions,grantResults);

    if (requestCode==REQUEST_CODE)

    {

        if(grantResults[0] == PackageManager.PERMISSION_GRANTED){

            {

              //Do whatever you want permission related

                Toast.makeText(this, "Permission Granted !", Toast.LENGTH_SHORT).show();

                musicFiles =getAllAudio(this);


            }

            else

            {

                ActivityCompat.requestPermissions(MainActivity.this, new String []{Manifest.permission.WRITE_EXTERNAL_STORAGE,

                        REQUEST_CODE);

                }

                }

        }


private void initViewpager(){

    ViewPager viewPager =findViewById(R.id.viewpager);

    TabLayout tabLayout = findViewById(R.id.tab_layout);

    ViewpagerAdapter viewPagerAdapter = new ViewpagerAdapter(getSupportFragmentManager());

    viewPagerAdapter.addFragments(new SongsFragment(), "Songs");

    viewPagerAdapter.addFragments(new AlbumFragment(),"Album");

    viewPager.setAdapter(viewPagerAdapter);

    tabLayout.setupWithViewPager(viewPager);

}

public static class ViewpagerAdapter extends FragmentPagerAdapter{


    private ArrayList<Fragment> fragments;

    private ArrayList<String> titles;

    public ViewpagerAdapter (@NonNull FragmentManager fm){

        super(fm);

        this.fragments = new ArrayList<>();

        this.titles = new ArrayList<>();

    }

    void addFragments (Fragment fragment, String title){

        fragments.add(fragment);

        titles.add(title);


    }


    @NonNull

    @Override

    public Fragment getItem (int position){

        return fragments.get(position);

    }

    @Override

    public int getCount(){

        return fragments.size();

    }


    @Nullable

    @Override

    public CharSequence getPageTitle(int position){

        return titles.get(position);

    }

}

}

}

}

public static ArrayList getAllAudio(Context context){

ArrayList tempAudioList = new ArrayList<>();

Uri uri= MediaStore.Audio.EXTERNAL_CONTENT_URI;

String[] projection = (

        MediaStore.Audio.Media.ALBUM,

        MediaStore.Audio.Media.TITLE,

        MediaStore.Audio.Media.DURATION,

        MediaStore.Audio.Media.DATA, // FOR PATH

        MediaStore.Audio.Media.ARTIST,

};

    Cursor cursor = context.getContentResolver().query(uri, projection, null,null,null);

    if (cursor!=null)

            {

            while(cursor.moveToNext())

            {

            String album = cursor.getString(0);

            String title = cursor.getString(1);

            String duration = cursor.getString(2);

            String path = cursor.getString(3);

            String artist = cursor.getString(4);


            MusicFiles musicFiles = new MusicFiles(path, title,artist,album,duration);

            //take log.e for check

            Log.e("Path: " + path, "Album : " + album);

            tempAudioList.add(musicFiles)

                }

            cursor.close();

             }

    return tempAudioList;

            }

            }
Reply
James Watson
  • Forum posts: 1,584

Mar 1, 2021, 4:33:06 AM via Website

What's wrong with your code? ......(smug)

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
José António Melo Moreira
  • Forum posts: 4

Mar 1, 2021, 8:48:27 AM via Website

Hello James, good morning.

Replying your question of the forum it´s difficult to me answer correctly on the forum, because i tried to send images files and the NextPit don´t allow to send this format.

If you remember i sended the following message to the forum (see below):

Hello good morning for everyone I´m a beginner on the Android Studio, and i need a help for you to place well the app, that means working perfectly. It´s a good app, and it could serve me as a example to understand its funcionality.

I´m building an audio music player app., but on my MainActivity.java file ,

In the code i faced with some mistakes, see below the images that i sent you. How could you fix them?

....

So I could you send me your email, to send you all the code and the error images associated.

My email is: Josemmoreira1@gmail.com.

What´s your email?

Best regards

José Moreira

Helpful?
Reply